Merge branch 'dev' into docker-ubuntu24

This commit is contained in:
Ralf Vogler 2025-06-05 23:06:50 +02:00
commit 14f9311a64
36 changed files with 2068 additions and 1338 deletions

20
.cspell.json Normal file
View file

@ -0,0 +1,20 @@
{
"ignorePaths": [
"**/data/**",
"docker.yml",
"Dockerfile",
".jscpd.json",
"**/node_modules/**",
"**/vscode-extension/**",
"**/.git/**",
"**/.pnpm-lock.json",
".vscode",
"megalinter",
"package-lock.json",
"report"
],
"language": "en",
"noConfigSearch": true,
"words": ["megalinter", "oxsecurity", "ralf", "vogler", "DOCKERHUB"],
"version": "0.2"
}

View file

@ -1,6 +1,12 @@
node_modules/ node_modules/
data/ data/
*.env *.env
megalinter-reports/
# the above is just copied from .gitignore - violating DRY...
# however, generally, we want .dockerignore to be a *strict* superset of .gitignore, so we can't just `ln -s .gitignore .dockerignore`
# could generate this in CI and append the extra entries from below, but then it wouldn't work for local builds without running some script...
# also, the rules are slightly different: https://zzz.buzz/2018/05/23/differences-of-rules-between-gitignore-and-dockerignore/
.gitignore .gitignore
.github/ .github/

View file

@ -9,6 +9,12 @@ updates:
directory: "/" directory: "/"
schedule: schedule:
interval: "weekly" interval: "weekly"
ignore:
- dependency-name: "*eslint*"
# - dependency-name: "@stylistic/eslint-plugin-js"
groups:
dev-dependencies:
dependency-type: "development"
# commit-message: # commit-message:
# prefix: "npm" # prefix: "npm"
# include: "scope" # include: "scope"

View file

@ -1,6 +1,5 @@
{ {
"$schema": "https://docs.renovatebot.com/renovate-schema.json", "$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [ "enabled": false,
"config:recommended" "extends": ["config:recommended"]
]
} }

View file

@ -1,75 +1,119 @@
name: Build and push Docker image (amd64, arm64 to hub.docker.com and ghcr.io) name: Build and push Docker image (amd64, arm64 to hub.docker.com and ghcr.io)
on: on:
workflow_dispatch: # allow manual trigger workflow_dispatch: # allows manual trigger
# https://github.com/orgs/community/discussions/26276 push: # push on branch
push: branches: [main, dev]
branches: paths: # ignore changes to .md files
- "main" - "**"
- "v*" - "!*.md"
tags:
- "v*"
paths: # ignore changes to certain files
- '**'
- '!*.md'
# - '!.github/**' # - '!.github/**'
pull_request: # runs when opened/reopned or when the head branch is updated, see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request pull_request: # runs when opened/reopened or when the head branch is updated
branches:
- "main" # only PRs against main permissions:
contents: read
packages: write
env:
BRANCH: ${{ github.head_ref || github.ref_name }} # head_ref/base_ref are only set for PRs, for branches ref_name will be used
jobs: jobs:
docker: docker:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- - name: Checkout
name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
-
name: Set environment variables - name: Set environment variables
run: | run: |
BRANCH="${GITHUB_REF#refs/heads/}"
echo "BRANCH=$BRANCH" >> "$GITHUB_ENV"
echo "NOW=$(date -R)" >> "$GITHUB_ENV" # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S' echo "NOW=$(date -R)" >> "$GITHUB_ENV" # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S'
if [[ "$BRANCH" == "main" ]]; then if [[ "$BRANCH" == "main" ]]; then
echo "IMAGE_TAG=latest" >> "$GITHUB_ENV" echo "IMAGE_TAG=latest" >> "$GITHUB_ENV"
else else
echo "IMAGE_TAG=$BRANCH" >> "$GITHUB_ENV" echo "IMAGE_TAG=$BRANCH" >> "$GITHUB_ENV"
fi fi
- - name: Extract metadata for Docker (tags, labels)
name: Set up QEMU id: meta
uses: docker/setup-qemu-action@v3 uses: docker/metadata-action@v5
- with:
name: Set up Docker Buildx images: |
uses: docker/setup-buildx-action@v3 ${{ secrets.DOCKERHUB_USERNAME }}/free-games-claimer
- ghcr.io/${{ github.actor }}/free-games-claimer
name: Login to Docker Hub tags: |
type=ref,event=branch
type=ref,event=pr
# use docker tag 'latest' for the default branch (default is to only use it for the latest git tag)
type=raw,value=latest,enable={{is_default_branch}}
labels: |
org.opencontainers.image.created={{commit_date 'YYYY-MM-DDTHH:mm:ss.SSS[Z]'}}
env:
# otherwise labels are not shown on GitHub due to multi-arch image: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#adding-a-description-to-multi-arch-images
# https://github.com/docker/metadata-action#annotations
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index
- name: Login to Docker Hub
uses: docker/login-action@v3 uses: docker/login-action@v3
if: github.event_name != 'pull_request' # TODO if DOCKERHUB_* are set? # if: ${{ secrets.DOCKERHUB_USERNAME != '' && secrets.DOCKERHUB_TOKEN != '' }} # does not work: Unrecognized named-value: 'secrets' - https://www.cloudtruth.com/blog/skipping-jobs-in-github-actions-when-secrets-are-unavailable-securely-inject-configuration-secrets-into-github
if: github.event_name != 'pull_request' # don't try to login since PRs don't have access to secrets and need to set them in their fork
with: with:
username: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- - name: Login to GitHub Container Registry
name: Login to GitHub Container Registry
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
registry: ghcr.io registry: ghcr.io
username: ${{ github.actor }} # actor is user that opened PR, was repository_owner before username: ${{ github.actor }} # actor is user that opened PR, was repository_owner before
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push - name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v6 uses: docker/build-push-action@v6
# if: github.event_name != 'pull_request' # still want to build image if: ${{ env.IMAGE_TAG != '' }}
with: with:
context: . context: .
push: ${{ github.event_name != 'pull_request' }} # TODO push for forks? push: ${{ github.event_name != 'pull_request' }}
# push: ${{ secrets.DOCKERHUB_USERNAME != '' }} # here we can access secrets
# TODO speed up by building in parallel? https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
platforms: linux/amd64,linux/arm64
build-args: | build-args: |
COMMIT=${{ github.sha }} COMMIT=${{ github.sha }}
BRANCH=${{ env.BRANCH }} BRANCH=${{ env.BRANCH }}
NOW=${{ env.NOW }} NOW=${{ env.NOW }}
platforms: linux/amd64,linux/arm64 # ,linux/arm/v7 tags: ${{ steps.meta.outputs.tags }}
# TODO docker tag only if DOCKERHUB_* are set? labels: ${{ steps.meta.outputs.labels }}
tags: | annotations: ${{ steps.meta.outputs.annotations }}
${{ secrets.DOCKERHUB_USERNAME }}/free-games-claimer:${{env.IMAGE_TAG}} # tags: |
ghcr.io/${{ github.actor }}/free-games-claimer:${{env.IMAGE_TAG}} # ${{ secrets.DOCKERHUB_USERNAME }}/free-games-claimer:${{env.IMAGE_TAG}}
# ghcr.io/${{ github.actor }}/free-games-claimer:${{env.IMAGE_TAG}}
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
# https://gist.github.com/MichaelSimons/fb588539dcefd9b5fdf45ba04c302db6
- name: Docker (un)compressed sizes
run: |
REP=ghcr.io/${{ github.actor }}/free-games-claimer
IMG=$REP:${{env.IMAGE_TAG}}
log() { echo -e "\n\$ $*"; "$@"; }
emd() { echo "$*" | tee -a "$GITHUB_STEP_SUMMARY"; }
cmd() { echo "\$ $*" | tee -a "$GITHUB_STEP_SUMMARY"; "$@" | tee -a "$GITHUB_STEP_SUMMARY"; }
emd '```console'
download-size() { docker manifest inspect -v "$1" | jq -c 'if type == "array" then .[] else . end' | jq -r '[ ( .Descriptor.platform | [ .os, .architecture, .variant, ."os.version" ] | del(..|nulls) | join("/") ), ( [ ( .OCIManifest // .SchemaV2Manifest ).layers[].size ] | add ) ] | join(" ")' | numfmt --to iec --format '%.2f' --field 2 | sort | column -t ; }
cmd download-size "$IMG"
## don't need the following in job summary, but nice to have in full log
log docker buildx history inspect
# log docker buildx du
## not needed locally, but in CI with buildx `docker image ls` just lists moby/buildkit and tonistiigi/binfmt since the multi-arch build is pushed to registry instead of loaded locally, so we need to pull it first (cached anyway)
log docker pull "$IMG"
# log docker image rm moby/buildkit # failed
# log docker image rm tonistiigi/binfmt
# emd '# Uncompressed size (max, not size on disk due to sharing):'
# cmd docker image ls # below has more details
emd '# uncompressed size = unique + shared:'
local-size() { docker system df -v | grep "$1" -B1; }
cmd local-size "$REP"
emd '```'
continue-on-error: true

61
.github/workflows/js.yml vendored Normal file
View file

@ -0,0 +1,61 @@
name: "JS: deps, lint, tests"
# Run on push in any branch and changes in PRs.
on:
push:
paths:
- "**.js"
- "**.ts"
- "package.json"
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
runs-on: ubuntu-latest
permissions:
security-events: write # required for sarif upload
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
- name: bun install
run: bun install
# check size of dependencies
# all tools gave different results locally
- name: dep-size node_modules
run: du -sh node_modules | tee -a "$GITHUB_STEP_SUMMARY"
- name: dep-size howfat -d (inc. dev) - ignores size of transitive deps
run: bunx howfat -d --reporter table --sort size-
- name: dep-size howfat -d -p (inc. dev, peer) - includes size of transitive deps per dep
run: bunx howfat -d -p --reporter table --sort size-
- name: dep-size qnm (flat list as in node_modules)
run: |
emd() { echo "$*" | tee -a "$GITHUB_STEP_SUMMARY"; }
cmd() { echo "\$ $*" | tee -a "$GITHUB_STEP_SUMMARY"; "$@" | tee -a "$GITHUB_STEP_SUMMARY"; }
emd '```console'
cmd bunx qnm doctor
emd '```'
# - name: dep-size cost-of-modules # this says total 8.37MB while du says 75MB...
# run: bunx cost-of-modules --include-dev --no-install
# https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github#example-workflow-that-runs-the-eslint-analysis-tool
- name: eslint (sarif output)
# https://github.com/microsoft/sarif-js-sdk/issues/91
# @microsoft/eslint-formatter-sarif uses eslint@8.57.1 instead of my local eslint@9.27.0 despite it not needing it? -> just download the sarif.js file instead of having dep in package.json (only needed here anyway)
run: |
wget https://raw.githubusercontent.com/microsoft/sarif-js-sdk/refs/heads/main/packages/eslint-formatter-sarif/sarif.js -O node_modules/sarif.cjs
bun i utf8 lodash jschardet
bun eslint . --format node_modules/sarif.cjs -o results.sarif
continue-on-error: true
- name: upload eslint sarif output for Security tab and inline results
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
category: eslint
- name: bun lint
# eslint exits 1 if it finds anything to report
run: bun lint

View file

@ -1,36 +0,0 @@
# https://github.com/marketplace/actions/super-linter#get-started
name: Lint
on: # yamllint disable-line rule:truthy
push: null
pull_request: null
permissions: {}
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
# To report GitHub Actions status checks
statuses: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
# super-linter needs the full git history to get the
# list of files that changed across commits
fetch-depth: 0
- name: Super-linter
uses: super-linter/super-linter/slim@v7.3.0 # x-release-please-version
# TODO need to create problem matchers for each linter? https://github.com/rhysd/actionlint/blob/v1.7.7/docs/usage.md#problem-matchers
env:
# To report GitHub Actions status checks
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# TODO automatically fix linting issues and commit them for PRs
# fix-lint-issues: # https://github.com/marketplace/actions/super-linter#github-actions-workflow-example-pull-request

208
.github/workflows/mega-linter.yml vendored Normal file
View file

@ -0,0 +1,208 @@
# MegaLinter GitHub Action configuration file
# More info at https://megalinter.io
# See .mega-linter.yml for actual config and examples how to run this locally.
---
name: MegaLinter
# Run on push in any branch and changes in PRs.
on:
push:
pull_request:
types: [opened, synchronize, reopened]
# Comment env block if you do not want to apply fixes
env:
# Apply linter fixes configuration
#
# When active, APPLY_FIXES must also be defined as environment variable
# (in github/workflows/mega-linter.yml or other CI tool)
APPLY_FIXES: all
# Decide which event triggers application of fixes in a commit or a PR
# (pull_request, push, all)
APPLY_FIXES_EVENT: pull_request
# If APPLY_FIXES is used, defines if the fixes are directly committed (commit)
# or posted in a PR (pull_request)
APPLY_FIXES_MODE: commit
concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true
jobs:
megalinter:
name: MegaLinter
runs-on: ubuntu-latest
# Give the default GITHUB_TOKEN write permission to commit and push, comment
# issues, and post new Pull Requests; remove the ones you do not need
permissions:
contents: write
issues: write
pull-requests: write
security-events: write # needed for SARIF upload
steps:
# Git Checkout
- name: Checkout Code
uses: actions/checkout@v4
with:
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
# If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to
# improve performance
fetch-depth: 0
# MegaLinter
- name: MegaLinter
# You can override MegaLinter flavor used to have faster performances
# More info at https://megalinter.io/latest/flavors/
# uses: oxsecurity/megalinter@v8 # default (127 linters)
uses: oxsecurity/megalinter/flavors/cupcake@v8.7.0 # most common, was recommended in output (88 linters)
id: ml
# All available variables are described in documentation
# https://megalinter.io/latest/config-file/
env:
# Validates all source when push on main, else just the git diff with
# main. Override with true if you always want to lint all sources
#
# To validate the entire codebase, set to:
# VALIDATE_ALL_CODEBASE: true
#
# To validate only diff with main, set to:
# VALIDATE_ALL_CODEBASE: >-
# ${{
# github.event_name == 'push' &&
# github.ref == 'refs/heads/main'
# }}
VALIDATE_ALL_CODEBASE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Uncomment to use ApiReporter (Grafana)
# API_REPORTER: true
# API_REPORTER_URL: ${{ secrets.API_REPORTER_URL }}
# API_REPORTER_BASIC_AUTH_USERNAME: ${{ secrets.API_REPORTER_BASIC_AUTH_USERNAME }}
# API_REPORTER_BASIC_AUTH_PASSWORD: ${{ secrets.API_REPORTER_BASIC_AUTH_PASSWORD }}
# API_REPORTER_METRICS_URL: ${{ secrets.API_REPORTER_METRICS_URL }}
# API_REPORTER_METRICS_BASIC_AUTH_USERNAME: ${{ secrets.API_REPORTER_METRICS_BASIC_AUTH_USERNAME }}
# API_REPORTER_METRICS_BASIC_AUTH_PASSWORD: ${{ secrets.API_REPORTER_METRICS_BASIC_AUTH_PASSWORD }}
# API_REPORTER_DEBUG: false
# ADD YOUR CUSTOM ENV VARIABLES HERE TO OVERRIDE VALUES OF
# .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
# Upload MegaLinter artifacts
- name: Archive production artifacts
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: MegaLinter reports
include-hidden-files: "true"
path: |
megalinter-reports
mega-linter.log
# Create pull request if applicable
# (for now works only on PR from same repository, not from forks)
- name: Create Pull Request with applied fixes
uses: peter-evans/create-pull-request@v6
id: cpr
if: >-
steps.ml.outputs.has_updated_sources == 1 &&
(
env.APPLY_FIXES_EVENT == 'all' ||
env.APPLY_FIXES_EVENT == github.event_name
) &&
env.APPLY_FIXES_MODE == 'pull_request' &&
(
github.event_name == 'push' ||
github.event.pull_request.head.repo.full_name == github.repository
) &&
!contains(github.event.head_commit.message, 'skip fix')
with:
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
commit-message: "[MegaLinter] Apply linters automatic fixes"
title: "[MegaLinter] Apply linters automatic fixes"
labels: bot
- name: Create PR output
if: >-
steps.ml.outputs.has_updated_sources == 1 &&
(
env.APPLY_FIXES_EVENT == 'all' ||
env.APPLY_FIXES_EVENT == github.event_name
) &&
env.APPLY_FIXES_MODE == 'pull_request' &&
(
github.event_name == 'push' ||
github.event.pull_request.head.repo.full_name == github.repository
) &&
!contains(github.event.head_commit.message, 'skip fix')
run: |
echo "PR Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "PR URL - ${{ steps.cpr.outputs.pull-request-url }}"
# Push new commit if applicable
# (for now works only on PR from same repository, not from forks)
- name: Prepare commit
if: >-
steps.ml.outputs.has_updated_sources == 1 &&
(
env.APPLY_FIXES_EVENT == 'all' ||
env.APPLY_FIXES_EVENT == github.event_name
) &&
env.APPLY_FIXES_MODE == 'commit' &&
github.ref != 'refs/heads/main' &&
(
github.event_name == 'push' ||
github.event.pull_request.head.repo.full_name == github.repository
) &&
!contains(github.event.head_commit.message, 'skip fix')
run: sudo chown -Rc $UID .git/
- name: Commit and push applied linter fixes
uses: stefanzweifel/git-auto-commit-action@v5
if: >-
steps.ml.outputs.has_updated_sources == 1 &&
(
env.APPLY_FIXES_EVENT == 'all' ||
env.APPLY_FIXES_EVENT == github.event_name
) &&
env.APPLY_FIXES_MODE == 'commit' &&
github.ref != 'refs/heads/main' &&
(
github.event_name == 'push' ||
github.event.pull_request.head.repo.full_name == github.repository
) &&
!contains(github.event.head_commit.message, 'skip fix')
with:
branch: >-
${{
github.event.pull_request.head.ref ||
github.head_ref ||
github.ref
}}
commit_message: "[MegaLinter] Apply linters fixes"
commit_user_name: megalinter-bot
commit_user_email: 129584137+megalinter-bot@users.noreply.github.com
# https://megalinter.io/latest/reporters/SarifReporter/
- name: Upload MegaLinter scan results to GitHub Security tab
if: success() || failure()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "megalinter-reports/megalinter-report.sarif"
category: mega-linter
# https://github.blog/news-insights/product-news/supercharging-github-actions-with-job-summaries/
- name: Add job summary
if: success() || failure()
run: cat "megalinter-reports/megalinter-report.md" >> "$GITHUB_STEP_SUMMARY"
# logs and artifacts are retained for 90 days, workflow run history is retained for 400 days... https://docs.github.com/en/actions/administering-github-actions/usage-limits-billing-and-administration#artifact-and-log-retention-policy

View file

@ -1,37 +0,0 @@
on:
# Trigger analysis when pushing in main or pull requests, and when creating a pull request.
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
name: Sonar
jobs:
sonarcloud:
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting. Otherwise sonarcloud will show a warning.
fetch-depth: 0
-
uses: actions/setup-node@v4
with:
cache: 'npm'
-
name: Install dev dependencies which includde ESLint + plugins
run: npm install --only=dev
-
name: Run ESLint
continue-on-error: true
run: npx eslint . -f json -o eslint_report.json
-
name: Fix ESLint paths
run: sed -i 's+/home/runner/work/free-games-claimer/free-games-claimer+/github/workspace+g' eslint_report.json
-
name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

38
.github/workflows/sonarqube.yml vendored Normal file
View file

@ -0,0 +1,38 @@
name: SonarQube Scan
# Run on push in any branch and changes in PRs.
on:
push:
paths:
- "**.js"
- "**.ts"
- "package.json"
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting. Otherwise sonarcloud will show a warning.
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
- name: bun install
run: bun install
- name: eslint (json output)
continue-on-error: true
run: bun eslint . -f json -o eslint_report.json
- name: fix paths for SonarCloud
run: sed -i 's+/home/runner/work/free-games-claimer/free-games-claimer+/github/workspace+g' eslint_report.json
- name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@v5.2.0
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules/ node_modules/
data/ data/
*.env *.env
megalinter-reports/

15
.jscpd.json Normal file
View file

@ -0,0 +1,15 @@
{
"threshold": 0,
"reporters": ["html", "markdown"],
"ignore": [
"**/node_modules/**",
"**/.git/**",
"**/.rbenv/**",
"**/.venv/**",
"**/*cache*/**",
"**/.github/**",
"**/.idea/**",
"**/report/**",
"**/*.svg"
]
}

84
.mega-linter.yml Normal file
View file

@ -0,0 +1,84 @@
# Configuration file for MegaLinter
#
# See all available variables at https://megalinter.io/latest/config-file/ and in
# linters documentation
# See .github/workflows/mega-linter.yml for GitHub config.
# Run this locally via Docker:
# npx mega-linter-runner -r v8 -f cupcake # run as configured here
# npx mega-linter-runner -r v8 -f cupcake -e "'ENABLE=MARKDOWN,YAML'" -e "APPLY_FIXES=none" # only enable certain groups and disable automatic fixes (note that the '' are required for multiple values)
# npx mega-linter-runner -r v8 -f cupcake -e "ENABLE_LINTERS=MARKDOWN_MARKDOWN_LINK_CHECK" # run a specific linter
# https://github.com/oxsecurity/megalinter#cli-lint-mode most linters will respect .gitignore, but the ones running in 'project' mode will not and may take forever if not configured right
# all, none, or list of linter keys
APPLY_FIXES: all
# If you use ENABLE variable, all other languages/formats/tooling-formats will
# be disabled by default
# ENABLE:
# If you use ENABLE_LINTERS variable, all other linters will be disabled by
# default
# ENABLE_LINTERS:
# DISABLE:
# - COPYPASTE # Uncomment to disable checks of excessive copy-pastes
# - SPELL # Uncomment to disable checks of spelling mistakes
SHOW_ELAPSED_TIME: true
# Uncomment if you want MegaLinter to detect errors but not block CI to pass
# DISABLE_ERRORS: true
# ---
# Custom config:
PRINT_ALPACA: false
JAVASCRIPT_DEFAULT_STYLE: prettier # disables JAVASCRIPT_STANDARD in favor of JAVASCRIPT_PRETTIER - disabled below since I prefer my local eslint
# DISABLE: # groups of linters/formatters
# - REPOSITORY # ignore this for now (at least locally) since all project-based and need extra config like .gitignore
# npx mega-linter-runner -r v8 -f cupcake -e "ENABLE_LINTERS=MARKDOWN_MARKDOWN_LINK_CHECK" # run a specific linter locally
DISABLE_LINTERS: # times are for running locally with 30GB swap, 65% pressure and several GB in data/ (relevant for project-mode linters that don't respect .gitignore)
- MARKDOWN_MARKDOWN_LINK_CHECK # 30s, only reported 0 (e.g. for localhost) or 403 (forbidden) for working links to settings or due to DDoS/bot protections
- JAVASCRIPT_STANDARD # don't like standard format
- JAVASCRIPT_PRETTIER # prefer my local eslint config
- REPOSITORY_TRIVY_SBOM # 11s, don't need SBOM
DISABLE_ERRORS_LINTERS: # error -> warning
- DOCKERFILE_HADOLINT # mostly wants to pin versions for apt and pip installs and merge consecutive RUN instructions
- COPYPASTE_JSCPD # default threshold is 0% duplicates -> can make this error once sep. scripts are refactored
- SPELL_CSPELL # needs config in .cspell.json, but looks annoying since it also flags apt packages
- SPELL_LYCHEE # dead link checking, 9/332 errors all false positives (Forbidden etc.)
- JAVASCRIPT_ES # this uses old eslint 8.57.1 instead of local 9.26.0 and complains about stuff that newer version has no problem with
- REPOSITORY_CHECKOV # docker healthcheck not needed for CLI
- REPOSITORY_KICS # wants to pin GitHub Actions to commit sha etc.
- REPOSITORY_TRIVY # docker healthcheck not needed for CLI
# Customizations via CLI arguments:
# https://github.com/prantlf/jsonlint#command-line-interface
JSON_JSONLINT_ARGUMENTS: --comments --trailing-commas --no-duplicate-keys
# https://prettier.io/docs/options#trailing-commas
# JSON_PRETTIER_ARGUMENTS: --trailing-comma all --parser jsonc # need to change parser too since the default json parser still strips trailing commas
# -> let prettier remove trailing commas since e.g. npm will fail to JSON.parse package.json otherwise...
# megalinter still expects the old .eslintrc file... https://github.com/oxsecurity/megalinter/issues/3570#issuecomment-2138193684
JAVASCRIPT_ES_CONFIG_FILE: eslint.config.js
JAVASCRIPT_ES_COMMAND_REMOVE_ARGUMENTS: ["--no-eslintrc"] # not a valid option for eslint with flat config
# worked, but behaved differently than local `npm run lint` and complained about while(true) with break - probably due old version 8.57.1 (same with -r beta) instead of my local 9.26.0
# https://github.com/oxsecurity/megalinter#cli-lint-mode
REPOSITORY_SECRETLINT_ARGUMENTS: --secretlintignore .gitignore
# https://www.checkov.io/2.Basics/CLI%20Command%20Reference.html
REPOSITORY_CHECKOV_ARGUMENTS: --skip-path node_modules --skip-path data
# CI will comment on PRs etc., but for running locally (or downloading the results), we want more than the default megalinter-reports/megalinter.log as an overview:
JSON_REPORTER: true # mega-linter-report.json
MARKDOWN_SUMMARY_REPORTER: true # megalinter-report.md
SARIF_REPORTER: true # mega-linter-report.sarif - results for supported lintes should be shown in GitHub Security tab - https://megalinter.io/latest/reporters/SarifReporter/

View file

@ -6,5 +6,5 @@
"source.fixAll.eslint": "explicit" "source.fixAll.eslint": "explicit"
}, },
"eslint.experimental.useFlatConfig": true, "eslint.experimental.useFlatConfig": true,
"eslint.codeActionsOnSave.rules": null, "eslint.codeActionsOnSave.rules": null
} }

View file

@ -1,6 +1,17 @@
# Contribute # Contribute
## Building and publishing docker images ## Code: how to create a pull request
Setup the secrets for DOCKERHUB_USERNAME and [DOCKERHUB_TOKEN](https://hub.docker.com/settings/security) in https://github.com/YOUR_USERNAME/free-games-claimer/settings/secrets/actions to be able to run the docker.yml workflows.
Check if under Workflow Permissions in https://github.com/YOUR_USERNAME/free-games-claimer/settings/actions the radio button is set to "Read and write permissions". In case that's not set the push to ghcr.io will fail. 1. Fork it ( <https://github.com/vogler/free-games-claimer/fork> ).
1. Create your feature branch (`git checkout -b my-new-feature`).
1. Stage your files (`git add .`).
1. Commit your changes (`git commit -am 'Add some feature'`).
1. Push to the branch (`git push origin my-new-feature`).
1. Create a new pull request ( <https://github.com/vogler/free-games-claimer/compare> ).
## Building and publishing docker images
Setup the secrets for DOCKERHUB_USERNAME and [DOCKERHUB_TOKEN](https://hub.docker.com/settings/security) in `https://github.com/YOUR_USERNAME/free-games-claimer/settings/secrets/actions` to be able to run the docker.yml workflows.
Check if under Workflow Permissions in `https://github.com/YOUR_USERNAME/free-games-claimer/settings/actions` the radio button is set to "Read and write permissions", otherwise the push to ghcr.io will fail.

View file

@ -2,64 +2,66 @@
FROM ubuntu:noble FROM ubuntu:noble
# Configuration variables are at the end! # Configuration variables are at the end!
ARG DEBIAN_FRONTEND=noninteractive
# https://github.com/hadolint/hadolint/wiki/DL4006 # https://github.com/hadolint/hadolint/wiki/DL4006
SHELL ["/bin/bash", "-o", "pipefail", "-c"] SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ARG DEBIAN_FRONTEND=noninteractive
# Install up-to-date node & npm, deps for virtual screen & noVNC, firefox, pipx for apprise. # Install nodejs and deps for virtual display, noVNC, chromium, and pipx for installing apprise.
RUN apt-get update \ RUN apt-get update \
&& apt-get install -y curl wget gpg ca-certificates \ && apt-get install -y --no-install-recommends curl ca-certificates gnupg \
&& mkdir -p /etc/apt/keyrings \ && mkdir -p /etc/apt/keyrings \
&& curl -sL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \ # Node.js
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" >> /etc/apt/sources.list.d/nodesource.list \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
# TurboVNC & VirtualGL instead of Xvfb+X11vnc
&& curl --proto "=https" --tlsv1.2 -fsSL https://packagecloud.io/dcommander/virtualgl/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/VirtualGL.gpg \
&& curl --proto "=https" --tlsv1.2 -fsSL https://packagecloud.io/dcommander/turbovnc/gpgkey | gpg --dearmor -o /etc/apt/trusted.gpg.d/TurboVNC.gpg \
&& curl --proto "=https" --tlsv1.2 -fsSL https://raw.githubusercontent.com/VirtualGL/repo/main/VirtualGL.list > /etc/apt/sources.list.d/VirtualGL.list \
&& curl --proto "=https" --tlsv1.2 -fsSL https://raw.githubusercontent.com/TurboVNC/repo/main/TurboVNC.list > /etc/apt/sources.list.d/TurboVNC.list \
# update lists and install
&& apt-get update \ && apt-get update \
&& apt-get install --no-install-recommends -y \ && apt-get install --no-install-recommends -y \
nodejs \ virtualgl turbovnc ratpoison \
xvfb \
x11vnc \
tini \
novnc websockify \ novnc websockify \
tini \
nodejs \
dos2unix \ dos2unix \
pipx \ pipx \
# && npx playwright install-deps firefox \ # RUN npx patchright install-deps chromium
# When running playwright without deps, it said to install the below (which should be what install-deps above does) # ^ installing deps manually instead saved ~130MB:
&& apt-get install --no-install-recommends -y \ && apt-get install -y --no-install-recommends \
libnss3 \
libnspr4 \
libatk1.0-0 \
libatk-bridge2.0-0 \
libcups2 \
libxkbcommon0 \
libatspi2.0-0 \
libxcomposite1 \ libxcomposite1 \
libxcursor1 \ libgbm1 \
libgtk-3-0t64 \
libpangocairo-1.0-0 \
libpango-1.0-0 \ libpango-1.0-0 \
libatk1.0-0t64 \
libcairo-gobject2 \
libcairo2 \ libcairo2 \
libgdk-pixbuf-2.0-0 \
libasound2t64 \ libasound2t64 \
# needed for TurboVNC if not installing xfce4:
libxdamage1 \
&& apt-get autoremove -y \ && apt-get autoremove -y \
# https://www.perplexity.ai/search/what-files-do-i-need-to-remove-imjwdphNSUWK98WzsmQswA
&& apt-get clean \ && apt-get clean \
&& rm -rf \ && rm -rf \
/var/lib/apt/lists/* \
/var/cache/* \
/var/tmp/* \
/tmp/* \ /tmp/* \
/usr/share/doc/* \ /usr/share/doc/* \
/var/cache/* \ && ln -s /usr/share/novnc/vnc_auto.html /usr/share/novnc/index.html \
/var/lib/apt/lists/* \ && pipx install apprise
/var/tmp/*
#
# RUN node --version
# RUN npm --version
# TODO This was vnc_auto.html before which no longer exists, but only vnc_lite.html and vnc.html which we link now:
RUN ln -s /usr/share/novnc/vnc.html /usr/share/novnc/index.html
RUN pipx install apprise
WORKDIR /fgc WORKDIR /fgc
COPY package*.json ./ COPY package*.json ./
# Playwright installs patched firefox to ~/.cache/ms-playwright/firefox-* # --no-shell to avoid installing chromium_headless_shell (307MB) since headless mode could be detected without patching the browser itself
# Requires some system deps to run (see inlined install-deps above). RUN npm install && npx patchright install chromium --no-shell && du -h -d1 ~/.cache/ms-playwright
RUN npm install
# Old: PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD + install firefox (had to be done after `npm install` to get the correct version). Now: playwright-firefox as npm dep and `npm install` will only install that.
# From 1.38 Playwright will no longer install browser automatically for playwright, but apparently still for playwright-firefox: https://github.com/microsoft/playwright/releases/tag/v1.38.0
# RUN npx playwright install firefox
COPY . . COPY . .
@ -67,36 +69,36 @@ COPY . .
RUN dos2unix ./*.sh && chmod +x ./*.sh RUN dos2unix ./*.sh && chmod +x ./*.sh
COPY docker-entrypoint.sh /usr/local/bin/ COPY docker-entrypoint.sh /usr/local/bin/
# set by .github/workflows/docker.yml
ARG COMMIT="" ARG COMMIT=""
ARG BRANCH="" ARG BRANCH=""
ARG NOW="" ARG NOW=""
# need as env vars to log in docker-entrypoint.sh
ENV COMMIT=${COMMIT} ENV COMMIT=${COMMIT}
ENV BRANCH=${BRANCH} ENV BRANCH=${BRANCH}
ENV NOW=${NOW} ENV NOW=${NOW}
LABEL org.opencontainers.image.title="free-games-claimer" \ # added by docker/metadata-action using data from GitHub
org.opencontainers.image.name="free-games-claimer" \ # LABEL org.opencontainers.image.title="free-games-claimer" \
org.opencontainers.image.description="Automatically claims free games on the Epic Games Store, Amazon Prime Gaming and GOG" \ # org.opencontainers.image.url="https://github.com/vogler/free-games-claimer" \
org.opencontainers.image.url="https://github.com/vogler/free-games-claimer" \ # org.opencontainers.image.source="https://github.com/vogler/free-games-claimer"
org.opencontainers.image.source="https://github.com/vogler/free-games-claimer" \
org.opencontainers.image.revision=${COMMIT} \
org.opencontainers.image.ref.name=${BRANCH} \
org.opencontainers.image.base.name="ubuntu:jammy" \
org.opencontainers.image.version="latest"
# Configure VNC via environment variables: # Configure VNC via environment variables:
ENV VNC_PORT 5900 ENV VNC_PORT=5900
ENV NOVNC_PORT 6080 ENV NOVNC_PORT=6080
EXPOSE 5900 EXPOSE 5900
EXPOSE 6080 EXPOSE 6080
# Configure Xvfb via environment variables: # Configure Xvfb via environment variables:
ENV WIDTH 1920 ENV WIDTH=1920
ENV HEIGHT 1080 ENV HEIGHT=1080
ENV DEPTH 24 ENV DEPTH=24
# Show browser instead of running headless # Show browser instead of running headless
ENV SHOW 1 ENV SHOW=1
# mega-linter (KICS, Trivy) complained about it missing - usually this checks some API endpoint, for a container that runs ~1min a healthcheck doesn't make that much sense since playwright has timeouts for everything. Could react to SIGUSR1 and check something in JS - for now we just check that node is running and noVNC is reachable...
HEALTHCHECK --interval=5s --timeout=5s CMD pgrep node && curl --fail http://localhost:6080 || exit 1
# Script to setup display server & VNC is always executed. # Script to setup display server & VNC is always executed.
ENTRYPOINT ["docker-entrypoint.sh"] ENTRYPOINT ["docker-entrypoint.sh"]

145
README.md
View file

@ -1,16 +1,21 @@
# free-games-claimer
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=vogler_free-games-claimer&metric=code_smells)](https://sonarcloud.io/project/overview?id=vogler_free-games-claimer)
<p align="center"> <p align="center">
<img alt="logo-free-games-claimer" src="https://user-images.githubusercontent.com/493741/214588518-a4c89998-127e-4a8c-9b1e-ee4a9d075715.png" /> <img alt="logo-free-games-claimer" src="https://user-images.githubusercontent.com/493741/214588518-a4c89998-127e-4a8c-9b1e-ee4a9d075715.png" />
</p> </p>
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=vogler_free-games-claimer&metric=code_smells)](https://sonarcloud.io/project/overview?id=vogler_free-games-claimer)
# free-games-claimer
Claims free games periodically on Claims free games periodically on
- <img src="https://github.com/user-attachments/assets/82e9e9bf-b6ac-4f20-91db-36d2c8429cb6" width="32" align="middle" /> [Epic Games Store](https://www.epicgames.com/store/free-games) - <img alt="logo epic-games" src="https://github.com/user-attachments/assets/82e9e9bf-b6ac-4f20-91db-36d2c8429cb6" width="32" align="middle" /> [Epic Games Store](https://www.epicgames.com/store/free-games) - currently only without Docker
- <img src="https://github.com/user-attachments/assets/7627a108-20c6-4525-a1d8-5d221ee89d6e" width="32" align="middle" /> [Amazon Prime Gaming](https://gaming.amazon.com) - <img alt="logo prime-gaming" src="https://github.com/user-attachments/assets/7627a108-20c6-4525-a1d8-5d221ee89d6e" width="32" align="middle" /> [Amazon Prime Gaming](https://gaming.amazon.com)
- <img src="https://github.com/user-attachments/assets/49040b50-ee14-4439-8e3c-e93cafd7c3a5" width="32" align="middle" /> [GOG](https://www.gog.com) - <img alt="logo gog" src="https://github.com/user-attachments/assets/49040b50-ee14-4439-8e3c-e93cafd7c3a5" width="32" align="middle" /> [GOG](https://www.gog.com)
- <img src="https://github.com/user-attachments/assets/3582444b-f23b-448d-bf31-01668cd0313a" width="32" align="middle" /> [Unreal Engine (Assets)](https://www.unrealengine.com/marketplace/en-US/assets?count=20&sortBy=effectiveDate&sortDir=DESC&start=0&tag=4910) ([experimental](https://github.com/vogler/free-games-claimer/issues/44), same login as Epic Games)
<!-- - <img src="https://www.freepnglogos.com/uploads/xbox-logo-picture-png-14.png" width="32"/> [Xbox Live Games with Gold](https://www.xbox.com/en-US/live/gold#gameswithgold) ([experimental](https://github.com/vogler/free-games-claimer/issues/19)) --> <!-- - <img src="https://www.freepnglogos.com/uploads/xbox-logo-picture-png-14.png" width="32"/> [Xbox Live Games with Gold](https://www.xbox.com/en-US/live/gold#gameswithgold) ([experimental](https://github.com/vogler/free-games-claimer/issues/19)) -->
and some other free stuff (WIP):
- AliExpress coins (reduce prices)
- Google Play points - WIP
- Assets on fab.com (previously unrealengine.com, same login as Epic Games) - WIP
- Microsoft Rewards: points can be spent on e.g. Xbox Game Pass - WIP
<!-- - <img alt="logo unrealengine" src="https://github.com/user-attachments/assets/3582444b-f23b-448d-bf31-01668cd0313a" width="32" align="middle" /> [Unreal Engine (Assets)](https://www.unrealengine.com/marketplace/en-US/assets?count=20&sortBy=effectiveDate&sortDir=DESC&start=0&tag=4910) ([experimental](https://github.com/vogler/free-games-claimer/issues/44), same login as Epic Games) -->
Pull requests welcome :) Pull requests welcome :)
@ -18,39 +23,48 @@ Pull requests welcome :)
_Works on Windows/macOS/Linux._ _Works on Windows/macOS/Linux._
Raspberry Pi (3, 4, Zero 2): [requires 64-bit OS](https://github.com/vogler/free-games-claimer/issues/3) like Raspberry Pi OS or Ubuntu (Raspbian won't work since it's 32-bit). Raspberry Pi (and other SBC): [requires 64-bit OS](https://github.com/vogler/free-games-claimer/issues/3) like Raspberry Pi OS or Ubuntu (Raspbian won't work), but not recommended since it's too slow and may be unreliable.
## How to run ## How to run this?
Easy option: [install Docker](https://docs.docker.com/get-docker/) (or [podman](https://podman-desktop.io/)) and run this command in a terminal:
``` ### Container
You can [install Docker](https://docs.docker.com/get-docker/) and run this command in a terminal:
```sh
docker run --rm -it -p 6080:6080 -v fgc:/fgc/data --pull=always ghcr.io/vogler/free-games-claimer docker run --rm -it -p 6080:6080 -v fgc:/fgc/data --pull=always ghcr.io/vogler/free-games-claimer
``` ```
_This currently gives you a captcha challenge for epic-games. Until [issue #183](https://github.com/vogler/free-games-claimer/issues/183) is fixed, it is recommended to just run `node epic-games` without docker (see below)._ _This currently gives you a captcha challenge for epic-games. Until [issue #183](https://github.com/vogler/free-games-claimer/issues/183) is fixed, it is recommended to just run `node epic-games` without Docker on a desktop machine (not headless, see below)._
This will run `node epic-games; node prime-gaming; node gog` - if you only want to claim games for one of the stores, you can override the default command by appending e.g. `node epic-games` at the end of the `docker run` command, or if you want several `bash -c "node epic-games.js; node gog.js"`. This will run `node epic-games; node prime-gaming; node gog` - if you only want to claim games for one of the stores, you can override the default command by appending e.g. `node epic-games` at the end of the `docker run` command, or if you want several, `bash -c "node epic-games.js; node gog.js"`.
Data (including json files with claimed games, codes to redeem, screenshots) is stored in the Docker volume `fgc`. Data (including json files with claimed games, codes to redeem, screenshots) is stored in the Docker volume `fgc`.
<details> There's also a `docker-compose.yml` which you can use as an alternative with `docker compose up` (or if you need it for Portainer, your NAS, Unraid, ...).
<summary>I want to run without Docker or develop locally.</summary>
<!-- <details>
<summary>I want to run without Docker or develop locally.</summary> -->
### Without Docker
1. [Install Node.js](https://nodejs.org/en/download) 1. [Install Node.js](https://nodejs.org/en/download)
2. Clone/download this repository and `cd` into it in a terminal 2. Clone/download this repository and `cd` into it in a terminal
3. Run `npm install` 3. Run `npm install && npx patchright install chromium` to install dependencies
4. Run `pip install apprise` (or use [pipx](https://github.com/pypa/pipx) if you have [problems](https://stackoverflow.com/questions/75608323/how-do-i-solve-error-externally-managed-environment-every-time-i-use-pip-3)) to install [apprise](https://github.com/caronc/apprise) if you want notifications 4. Optional: notifications are handled by [apprise](https://github.com/caronc/apprise). See its docs for setup instructions (e.g. `pipx install apprise` ([uv](https://github.com/astral-sh/uv) and [pipx](https://github.com/pypa/pipx) are recommended over [pip](https://stackoverflow.com/questions/75608323/how-do-i-solve-error-externally-managed-environment-every-time-i-use-pip-3))
5. To get updates: `git pull; npm install` 5. To get updates: `git pull; npm install`
6. Run `node epic-games`, `node prime-gaming`, `node gog`... 6. Run `node epic-games`, `node prime-gaming`, `node gog`...
During `npm install` Playwright will download its Firefox to a cache in home ([doc](https://playwright.dev/docs/browsers#managing-browser-binaries)). Patchright/Playwright will download its Chromium to a cache in home ([doc](https://playwright.dev/docs/browsers#managing-browser-binaries)).
If you are missing some dependencies for the browser on your system, you can use `sudo npx playwright install firefox --with-deps`. If you are missing some dependencies for the browser on your system, you can use `npx patchright install chromium --with-deps`.
If you don't want to use Docker for quasi-headless mode, you could run inside a virtual machine, on a server, or you wake your PC at night to avoid being interrupted. If you don't want to use Docker for quasi-headless mode, you could run inside a virtual machine, on a server (as long as it has a (virtual) display), or you wake your PC at night to avoid being interrupted.
</details> <!-- </details> -->
## Usage ## Usage
All scripts start an automated Firefox instance, either with the browser GUI shown or hidden (*headless mode*). By default, you won't see any browser open on your host system.
- When running inside Docker, the browser will be shown only inside the container. You can open http://localhost:6080 to interact with the browser running inside the container via noVNC (or use other VNC clients on port 5900). All scripts start an automated Chromium instance, either with the browser GUI shown or hidden (_headless mode_). By default, you won't see any browser open on your host system.
Epic Games is an exception which will always show the browser since otherwise you would get a captcha challenge.
- When running inside Docker, the browser will be shown only inside the container. You can open <http://localhost:6080> to interact with the browser running inside the container via noVNC (or use other VNC clients on port 5900).
- When running the scripts outside of Docker, the browser will be hidden by default; you can use `SHOW=1 ...` to show the UI (see options below). - When running the scripts outside of Docker, the browser will be hidden by default; you can use `SHOW=1 ...` to show the UI (see options below).
When running the first time, you have to login for each store you want to claim games on. When running the first time, you have to login for each store you want to claim games on.
@ -61,59 +75,68 @@ There will be prompts in the terminal asking you to enter email, password, and a
After login, the script will continue claiming the current games. If it still waits after you are already logged in, you can restart it (and open an issue). If you run the scripts regularly, you should not have to login again. After login, the script will continue claiming the current games. If it still waits after you are already logged in, you can restart it (and open an issue). If you run the scripts regularly, you should not have to login again.
### Configuration / Options ### Configuration / Options
Options are set via [environment variables](https://kinsta.com/knowledgebase/what-is-an-environment-variable/) which allow for flexible configuration. Options are set via [environment variables](https://kinsta.com/knowledgebase/what-is-an-environment-variable/) which allow for flexible configuration.
TODO: ~~On the first run, the script will guide you through configuration and save all settings to `data/config.env`. You can edit this file directly or run `node fgc config` to run the configuration assistant again.~~ TODO: ~~On the first run, the script will guide you through configuration and save all settings to `data/config.env`. You can edit this file directly or run `node fgc config` to run the configuration assistant again.~~
Available options/variables and their default values: Available options/variables and their default values:
| Option | Default | Description | | Option | Default | Description |
|--------------- |--------- |------------------------------------------------------------------------ | |----------------|--------------|------------------------------------------------------------------------------------------------------------------------------|
| SHOW | 1 | Show browser if 1. Default for Docker, not shown when running outside. | | SHOW | 1 | Show browser if 1. Default for Docker, not shown when running outside. |
| WIDTH | 1280 | Width of the opened browser (and of screen for VNC in Docker). | | WIDTH | 1280 | Width of the opened browser (and of screen for VNC in Docker). |
| HEIGHT | 1280 | Height of the opened browser (and of screen for VNC in Docker). | | HEIGHT | 1280 | Height of the opened browser (and of screen for VNC in Docker). |
| VNC_PASSWORD | | VNC password for Docker. No password used by default! | | VNC_PASSWORD | | VNC password for Docker. No password used by default! |
| NOTIFY | | Notification services to use (Pushover, Slack, Telegram...), see below. [Apprise](https://github.com/caronc/apprise) | | NOTIFY | | Notification services to use (Pushover, Slack, Telegram...), see below. |
| NOTIFY_TITLE | | Optional title for notifications, e.g. for Pushover. | | NOTIFY_TITLE | | Optional title for notifications, e.g. for Pushover. |
| BROWSER_DIR | data/browser | Directory for browser profile, e.g. for multiple accounts. | | BROWSER_DIR | data/browser | Directory for browser profile, e.g. for multiple accounts. |
| TIMEOUT | 60 | Timeout for any page action. Should be fine even on slow machines. | | TIMEOUT | 60 | Timeout for any page action. Should be fine even on slow machines. |
| LOGIN_TIMEOUT | 180 | Timeout for login in seconds. Will wait twice (prompt + manual login). | | LOGIN_TIMEOUT | 180 | Timeout for login in seconds. Will wait twice (prompt + manual login). |
| EMAIL | | Default email for any login. | | EMAIL | | Default email for any login. |
| PASSWORD | | Default password for any login. | | PASSWORD | | Default password for any login. |
| EG_EMAIL | | Epic Games email for login. Overrides EMAIL. | | EG_EMAIL | | Epic Games email for login. Overrides EMAIL. |
| EG_PASSWORD | | Epic Games password for login. Overrides PASSWORD. | | EG_PASSWORD | | Epic Games password for login. Overrides PASSWORD. |
| EG_OTPKEY | | Epic Games MFA OTP key. | | EG_OTPKEY | | Epic Games MFA OTP key. |
| EG_PARENTALPIN | | Epic Games Parental Controls PIN. | | EG_PARENTALPIN | | Epic Games Parental Controls PIN. |
| PG_EMAIL | | Prime Gaming email for login. Overrides EMAIL. | | PG_EMAIL | | Prime Gaming email for login. Overrides EMAIL. |
| PG_PASSWORD | | Prime Gaming password for login. Overrides PASSWORD. | | PG_PASSWORD | | Prime Gaming password for login. Overrides PASSWORD. |
| PG_OTPKEY | | Prime Gaming MFA OTP key. | | PG_OTPKEY | | Prime Gaming MFA OTP key. |
| PG_REDEEM | 0 | Prime Gaming: try to redeem keys on external stores ([experimental](https://github.com/vogler/free-games-claimer/issues/5)). | | PG_REDEEM | 0 | Prime Gaming: try to redeem keys on external stores ([experimental](https://github.com/vogler/free-games-claimer/issues/5)). |
| PG_CLAIMDLC | 0 | Prime Gaming: try to claim DLCs ([experimental](https://github.com/vogler/free-games-claimer/issues/55)). | | PG_CLAIMDLC | 0 | Prime Gaming: try to claim DLCs ([experimental](https://github.com/vogler/free-games-claimer/issues/55)). |
| GOG_EMAIL | | GOG email for login. Overrides EMAIL. | | GOG_EMAIL | | GOG email for login. Overrides EMAIL. |
| GOG_PASSWORD | | GOG password for login. Overrides PASSWORD. | | GOG_PASSWORD | | GOG password for login. Overrides PASSWORD. |
| GOG_NEWSLETTER | 0 | Do not unsubscribe from newsletter after claiming a game if 1. | | GOG_NEWSLETTER | 0 | Do not unsubscribe from newsletter after claiming a game if 1. |
| LG_EMAIL | | Legacy Games: email to use for redeeming (if not set, defaults to PG_EMAIL) | | LG_EMAIL | | Legacy Games: email to use for redeeming (if not set, defaults to PG_EMAIL). |
See `src/config.js` for all options. See `src/config.js` for all options.
#### How to set options #### How to set options
You can add options directly in the command or put them in a file to load. You can add options directly in the command or put them in a file to load.
##### Docker ##### Docker
You can pass variables using `-e VAR=VAL`, for example `docker run -e EMAIL=foo@bar.baz -e NOTIFY='tgram://bottoken/ChatID' ...` or using `--env-file fgc.env` where `fgc.env` is a file on your host system (see [docs](https://docs.docker.com/engine/reference/commandline/run/#env)). You can also `docker cp` your configuration file to `/fgc/data/config.env` in the `fgc` volume to store it with the rest of the data instead of on the host ([example](https://github.com/moby/moby/issues/25245#issuecomment-365980572)).
You can pass variables using `-e VAR=VAL`.
For example, `docker run -e EMAIL=foo@bar.baz -e NOTIFY='tgram://bottoken/ChatID' ...`.
Alternatively, you can pass a file with `--env-file fgc.env` where `fgc.env` is a file on your host system (see [docs](https://docs.docker.com/engine/reference/commandline/run/#env)).
You can also `docker cp` your configuration file to `/fgc/data/config.env` in the `fgc` volume to store it with the rest of the data instead of on the host ([example](https://github.com/moby/moby/issues/25245#issuecomment-365980572)).
If you are using [docker compose](https://docs.docker.com/compose/environment-variables/) (or Portainer etc.), you can put options in the `environment:` section. If you are using [docker compose](https://docs.docker.com/compose/environment-variables/) (or Portainer etc.), you can put options in the `environment:` section.
##### Without Docker ##### Without Docker
On Linux/macOS you can prefix the variables you want to set, for example `EMAIL=foo@bar.baz SHOW=1 node epic-games` will show the browser and skip asking you for your login email. On Windows you have to use `set`, [example](https://github.com/vogler/free-games-claimer/issues/314). On Linux/macOS you can prefix the variables you want to set, for example `EMAIL=foo@bar.baz SHOW=1 node epic-games` will show the browser and skip asking you for your login email. On Windows you have to use `set`, [example](https://github.com/vogler/free-games-claimer/issues/314).
You can also put options in `data/config.env` which will be loaded by [dotenv](https://github.com/motdotla/dotenv). You can also put options in `data/config.env` which will be loaded by [dotenv](https://github.com/motdotla/dotenv).
### Notifications ### Notifications
The scripts will try to send notifications for successfully claimed games and any errors like needing to log in or encountered captchas (should not happen). The scripts will try to send notifications for successfully claimed games and any errors like needing to log in or encountered captchas (should not happen).
[apprise](https://github.com/caronc/apprise) is used for notifications and offers many services including Pushover, Slack, Telegram, SMS, Email, desktop and custom notifications. [apprise](https://github.com/caronc/apprise) is used for notifications and offers many services including Pushover, Slack, Telegram, SMS, Email, desktop and custom notifications.
You just need to set `NOTIFY` to the notification services you want to use, e.g. `NOTIFY='mailto://myemail:mypass@gmail.com' 'pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b'` - refer to their list of services and [examples](https://github.com/caronc/apprise#command-line-usage). You just need to set `NOTIFY` to the notification services you want to use, e.g. `NOTIFY='mailto://myemail@gmail.com' 'pbul://o.gn5kj6nfhv736I7jC3cj3QLRiyhgl98b'` - refer to their list of services and [examples](https://github.com/caronc/apprise#command-line-usage).
### Automatic login, two-factor authentication ### Automatic login, two-factor authentication
If you set the options for email, password and OTP key, there will be no prompts and logins should happen automatically. This is optional since all stores should stay logged in since cookies are refreshed. If you set the options for email, password and OTP key, there will be no prompts and logins should happen automatically. This is optional since all stores should stay logged in since cookies are refreshed.
To get the OTP key, it is easiest to follow the store's guide for adding an authenticator app. You should also scan the shown QR code with your favorite app to have an alternative method for 2FA. To get the OTP key, it is easiest to follow the store's guide for adding an authenticator app. You should also scan the shown QR code with your favorite app to have an alternative method for 2FA.
@ -125,9 +148,11 @@ To get the OTP key, it is easiest to follow the store's guide for adding an auth
Beware that storing passwords and OTP keys as clear text may be a security risk. Use a unique/generated password! TODO: maybe at least offer to base64 encode for storage. Beware that storing passwords and OTP keys as clear text may be a security risk. Use a unique/generated password! TODO: maybe at least offer to base64 encode for storage.
### Epic Games Store ### Epic Games Store
Run `node epic-games` (locally or in Docker). Run `node epic-games` (locally or in Docker).
### Amazon Prime Gaming ### Amazon Prime Gaming
Run `node prime-gaming` (locally or in Docker). Run `node prime-gaming` (locally or in Docker).
Claiming the Amazon Games works out-of-the-box, however, for games on external stores you need to either link your account or redeem a key. Claiming the Amazon Games works out-of-the-box, however, for games on external stores you need to either link your account or redeem a key.
@ -142,11 +167,13 @@ Claiming the Amazon Games works out-of-the-box, however, for games on external s
<!-- Run `node xbox` (locally or in docker). --> <!-- Run `node xbox` (locally or in docker). -->
### Run periodically ### Run periodically
#### How often? #### How often?
Epic Games usually has two free games *every week*, before Christmas every day.
Prime Gaming has new games *every month* or more often during Prime days. Epic Games usually has two free games _every week_, before Christmas every day.
Prime Gaming has new games _every month_ or more often during Prime days.
GOG usually has one new game every couples of weeks. GOG usually has one new game every couples of weeks.
Unreal Engine has new assets to claim *every first Tuesday of a month*. Unreal Engine has new assets to claim _every first Tuesday of a month_.
<!-- Xbox usually has two games *every month*. --> <!-- Xbox usually has two games *every month*. -->
It is safe to run the scripts every day. It is safe to run the scripts every day.
@ -157,6 +184,7 @@ If you want it to run regularly, you have to schedule the runs yourself:
- Linux/macOS: `crontab -e` ([example](https://github.com/vogler/free-games-claimer/discussions/56)) - Linux/macOS: `crontab -e` ([example](https://github.com/vogler/free-games-claimer/discussions/56))
- macOS: [launchd](https://stackoverflow.com/questions/132955/how-do-i-set-a-task-to-run-every-so-often) - macOS: [launchd](https://stackoverflow.com/questions/132955/how-do-i-set-a-task-to-run-every-so-often)
<!-- markdownlint-disable-next-line line-length -->
- Windows: [task scheduler](https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows/Scheduled_tasks_and_cron_jobs_on_Windows/index.html) ([example](https://github.com/vogler/free-games-claimer/wiki/%5BHowTo%5D-Schedule-runs-on-Windows)), [other options](https://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron), or just put the command in a `.bat` file in Autostart if you restart often... - Windows: [task scheduler](https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows/Scheduled_tasks_and_cron_jobs_on_Windows/index.html) ([example](https://github.com/vogler/free-games-claimer/wiki/%5BHowTo%5D-Schedule-runs-on-Windows)), [other options](https://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron), or just put the command in a `.bat` file in Autostart if you restart often...
- any OS: use a process manager like [pm2](https://pm2.keymetrics.io/docs/usage/restart-strategies/) - any OS: use a process manager like [pm2](https://pm2.keymetrics.io/docs/usage/restart-strategies/)
- Docker Compose `command: bash -c "node epic-games; node prime-gaming; node gog; echo sleeping; sleep 1d"` additionally add `restart: unless-stopped` to it. - Docker Compose `command: bash -c "node epic-games; node prime-gaming; node gog; echo sleeping; sleep 1d"` additionally add `restart: unless-stopped` to it.
@ -165,7 +193,7 @@ TODO: ~~add some server-mode where the script just keeps running and claims game
### Problems? ### Problems?
Check the open [issues](https://github.com/vogler/free-games-claimer/issues) and comment there or open a new issue. Check the open [issues](https://github.com/vogler/free-games-claimer/issues) and comment there or open a new issue (if it is something new).
If you're a developer, you can use `PWDEBUG=1 ...` to [inspect](https://playwright.dev/docs/inspector) which opens a debugger where you can step through the script. If you're a developer, you can use `PWDEBUG=1 ...` to [inspect](https://playwright.dev/docs/inspector) which opens a debugger where you can step through the script.
@ -176,10 +204,11 @@ If you're a developer, you can use `PWDEBUG=1 ...` to [inspect](https://playwrig
Tried [epicgames-freebies-claimer](https://github.com/Revadike/epicgames-freebies-claimer), but had problems since epicgames introduced hcaptcha (see [issue](https://github.com/Revadike/epicgames-freebies-claimer/issues/172)). Tried [epicgames-freebies-claimer](https://github.com/Revadike/epicgames-freebies-claimer), but had problems since epicgames introduced hcaptcha (see [issue](https://github.com/Revadike/epicgames-freebies-claimer/issues/172)).
Played around with puppeteer before, now trying newer https://playwright.dev which is pretty similar. Played around with Puppeteer before, now trying newer [Playwright](https://playwright.dev) which is pretty similar.
Playwright Inspector and `codegen` to generate scripts are nice, but failed to generate the right code for clicking a button in an iframe. Playwright Inspector and `codegen` to generate scripts are nice, but failed to generate the right code for clicking a button in an iframe.
Added [main.spec.ts](https://github.com/vogler/epicgames-claimer/commit/e5ce7916ab6329cfc7134677c4d89c2b3fa3ba97#diff-d18d03e9c407a20e05fbf03cbd6f9299857740544fb6b50d6a70b9c6fbc35831) which was the test script generated by `npx playwright codegen` with manual fix for clicking buttons in the created iframe. Can be executed by `npx playwright test`. The test runner has options `--debug` and `--timeout` and can execute typescript which is nice. However, this only worked up to the button 'I Agree', and then showed an hcaptcha. <!-- markdownlint-disable-next-line line-length -->
Added [main.spec.ts](https://github.com/vogler/epicgames-claimer/commit/e5ce7916ab6329cfc7134677c4d89c2b3fa3ba97#diff-d18d03e9c407a20e05fbf03cbd6f9299857740544fb6b50d6a70b9c6fbc35831) which was the test script generated by `npx playwright codegen` with manual fix for clicking buttons in the created iframe. Can be executed by `npx playwright test`. The test runner has options `--debug` and `--timeout` and can execute TypeScript which is nice. However, this only worked up to the button 'I Agree', and then showed an hcaptcha.
Added [main.captcha.js](https://github.com/vogler/epicgames-claimer/commit/e5ce7916ab6329cfc7134677c4d89c2b3fa3ba97#diff-d18d03e9c407a20e05fbf03cbd6f9299857740544fb6b50d6a70b9c6fbc35831) which uses beta of `playwright-extra@next` and `@extra/recaptcha@next` (from [comment on puppeteer-extra](https://github.com/berstend/puppeteer-extra/pull/303#issuecomment-775277480)). Added [main.captcha.js](https://github.com/vogler/epicgames-claimer/commit/e5ce7916ab6329cfc7134677c4d89c2b3fa3ba97#diff-d18d03e9c407a20e05fbf03cbd6f9299857740544fb6b50d6a70b9c6fbc35831) which uses beta of `playwright-extra@next` and `@extra/recaptcha@next` (from [comment on puppeteer-extra](https://github.com/berstend/puppeteer-extra/pull/303#issuecomment-775277480)).
However, `playwright-extra` seems to be old and missing `:has-text` selector (fixed [here](https://github.com/vogler/epicgames-claimer/commit/ba97a0e840b65f4476cca18e28d8461b0c703420)) and `page.frameLocator`, so the script did not run without adjustments. However, `playwright-extra` seems to be old and missing `:has-text` selector (fixed [here](https://github.com/vogler/epicgames-claimer/commit/ba97a0e840b65f4476cca18e28d8461b0c703420)) and `page.frameLocator`, so the script did not run without adjustments.
@ -199,7 +228,7 @@ Renamed repository from epicgames-claimer to free-games-claimer since a script f
epic games: `headless` mode gets hcaptcha challenge. More details/references in [issue](https://github.com/vogler/free-games-claimer/issues/2). epic games: `headless` mode gets hcaptcha challenge. More details/references in [issue](https://github.com/vogler/free-games-claimer/issues/2).
https://github.com/vogler/free-games-claimer/pull/11 introduced a Dockerfile for running non-headless inside the container via xvfb which makes it headless for the host running the container. [PR](https://github.com/vogler/free-games-claimer/pull/11) introduced a Dockerfile for running non-headless inside the container via xvfb which makes it headless for the host running the container.
v1.0 Standalone scripts node epic-games and node prime-gaming using Chromium. v1.0 Standalone scripts node epic-games and node prime-gaming using Chromium.

View file

@ -1,32 +1,37 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra // import { firefox } from 'playwright-firefox';
import { datetime, filenamify, prompt, handleSIGINT, stealth } from './src/util.js'; import { chromium } from 'patchright';
import { datetime, filenamify, prompt, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js'; import { cfg } from './src/config.js';
// using https://github.com/apify/fingerprint-suite worked, but has no launchPersistentContext... // can probably be removed and hard-code headers for mobile view
// from https://github.com/apify/fingerprint-suite/issues/162
import { FingerprintInjector } from 'fingerprint-injector'; import { FingerprintInjector } from 'fingerprint-injector';
import { FingerprintGenerator } from 'fingerprint-generator'; import { FingerprintGenerator } from 'fingerprint-generator';
const { fingerprint, headers } = new FingerprintGenerator().getFingerprint({ const { fingerprint, headers } = new FingerprintGenerator().getFingerprint({
devices: ["mobile"], devices: ['mobile'],
operatingSystems: ["android"], operatingSystems: ['android'],
}); });
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
// viewport: { width: cfg.width, height: cfg.height }, // viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800 recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/aliexpress-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools recordHar: cfg.record ? { path: `data/record/aliexpress-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// e.g. for coins, mobile view is needed, otherwise it just says to install the app
userAgent: fingerprint.navigator.userAgent, userAgent: fingerprint.navigator.userAgent,
viewport: { viewport: {
width: fingerprint.screen.width, width: fingerprint.screen.width,
height: fingerprint.screen.height, height: fingerprint.screen.height,
}, },
extraHTTPHeaders: { extraHTTPHeaders: {
'accept-language': headers['accept-language'], 'accept-language': headers['accept-language'],
}, },
// https://peter.sh/experiments/chromium-command-line-switches/
args: [
'--hide-crash-restore-bubble',
],
}); });
handleSIGINT(context); handleSIGINT(context);
// await stealth(context); // await stealth(context);
@ -36,43 +41,41 @@ context.setDefaultTimeout(cfg.debug ? 0 : cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
const auth = async (url) => { const auth = async url => {
console.log('auth', url); console.log('auth', url);
await page.goto(url, { waitUntil: 'domcontentloaded' }); await page.goto(url, { waitUntil: 'domcontentloaded' });
// redirects to https://login.aliexpress.com/?return_url=https%3A%2F%2Fwww.aliexpress.com%2Fp%2Fcoin-pc-index%2Findex.html // redirects to https://login.aliexpress.com/?return_url=https%3A%2F%2Fwww.aliexpress.com%2Fp%2Fcoin-pc-index%2Findex.html
await Promise.any([page.waitForURL(/.*login.aliexpress.com.*/).then(async () => { await Promise.any([page.waitForURL(/.*login\.aliexpress.com.*/).then(async () => {
// manual login // manual login
console.error('Not logged in! Will wait for 120s for you to login...'); console.error('Not logged in! Will wait for 120s for you to login in the browser or terminal...');
// await page.waitForTimeout(120*1000); context.setDefaultTimeout(120 * 1000);
// or try automated // or try automated
page.locator('span:has-text("Switch account")').click().catch(_ => {}); // sometimes no longer logged in, but previous user/email is pre-selected -> in this case we want to go back to the classic login page.locator('span:has-text("Switch account")').click().catch(_ => {}); // sometimes no longer logged in, but previous user/email is pre-selected -> in this case we want to go back to the classic login
const login = page.locator('.login-container'); const login = page.locator('#root'); // not universal: .content, .nfm-login
const email = cfg.ae_email || await prompt({ message: 'Enter email' }); const email = cfg.ae_email || await prompt({ message: 'Enter email' });
const emailInput = login.locator('input[label="Email or phone number"]'); const emailInput = login.locator('input[label="Email or phone number"]');
await emailInput.fill(email); await emailInput.fill(email);
await emailInput.blur(); // otherwise Continue button stays disabled await emailInput.blur(); // otherwise Continue button stays disabled
const continueButton = login.locator('button:has-text("Continue")'); const continueButton = login.locator('button:has-text("Continue")');
await continueButton.click({ force: true }); // normal click waits for button to no longer be covered by their suggestion menu, so we have to force click somewhere for the menu to close and then click await continueButton.click({ force: true }); // normal click waits for button to no longer be covered by their suggestion menu, so we have to force click somewhere for the menu to close and then click
await continueButton.click();
const password = email && (cfg.ae_password || await prompt({ type: 'password', message: 'Enter password' })); const password = email && (cfg.ae_password || await prompt({ type: 'password', message: 'Enter password' }));
await login.locator('input[label="Password"]').fill(password); await login.locator('input[label="Password"]').fill(password);
await login.locator('button:has-text("Sign in")').click(); await login.locator('button:has-text("Sign in")').click();
const error = login.locator('.error-text'); const error = login.locator('.nfm-login-input-error-text');
error.waitFor().then(async _ => console.error('Login error:', await error.innerText())); error.waitFor().then(async _ => console.error('Login error (please restart):', await error.innerText())).catch(_ => console.log('No login error.'));
await page.waitForURL(url); await page.waitForURL(u => u.toString().startsWith(url)); // e.g. https://m.aliexpress.com/p/coin-index/index.html?_immersiveMode=true&from=pc302
// TODO the following won't be executed anymore due to the navigation - patchright issue?
context.setDefaultTimeout(cfg.debug ? 0 : cfg.timeout);
console.log('Logged in!'); // this should still be printed, but isn't...
// await page.addLocatorHandler(page.getByRole('button', { name: 'Accept cookies' }), btn => btn.click()); // await page.addLocatorHandler(page.getByRole('button', { name: 'Accept cookies' }), btn => btn.click());
page.getByRole('button', { name: 'Accept cookies' }).click().then(_ => console.log('Accepted cookies')).catch(_ => { }); // page.getByRole('button', { name: 'Accept cookies' }).click().then(_ => console.log('Accepted cookies')).catch(_ => { });
}), page.locator('#nav-user-account').waitFor()]).catch(_ => {}); }), page.locator('.app-game').waitFor()]);
// await page.locator('#nav-user-account').hover();
// console.log('Logged in as:', await page.locator('.welcome-name').innerText());
}; };
// copied URLs from AliExpress app on tablet which has menu for the used webview // copied URLs from AliExpress app on tablet which has menu for the used webview
const urls = { const urls = {
// works with desktop view, but stuck at 100% loading in mobile view:
coins: 'https://www.aliexpress.com/p/coin-pc-index/index.html',
// only work with mobile view: // only work with mobile view:
coins: 'https://www.aliexpress.com/p/coin-pc-index/index.html',
grow: 'https://m.aliexpress.com/p/ae_fruit/index.html', // firefox: stuck at 60% loading, chrome: loads, but canvas grow: 'https://m.aliexpress.com/p/ae_fruit/index.html', // firefox: stuck at 60% loading, chrome: loads, but canvas
gogo: 'https://m.aliexpress.com/p/gogo-match-cc/index.html', // closes firefox?! gogo: 'https://m.aliexpress.com/p/gogo-match-cc/index.html', // closes firefox?!
// only show notification to install the app // only show notification to install the app
@ -81,38 +84,50 @@ const urls = {
}; };
const coins = async () => { const coins = async () => {
// await auth(urls.coins); console.log('Checking coins...');
await Promise.any([page.locator('.checkin-button').click(), page.locator('.addcoin').waitFor()]); const collectBtn = page.locator('.signVersion-panel div:has-text("Collect")').first();
console.log('Coins:', await page.locator('.mycoin-content-right-money').innerText()); const moreBtn = page.locator('.signVersion-panel div:has-text("Earn more coins")').first();
console.log('Streak:', await page.locator('.title-box').innerText()); await Promise.any([
console.log('Tomorrow:', await page.locator('.addcoin').innerText()); collectBtn.click().then(_ => console.log('Collected coins for today!')),
moreBtn.waitFor().then(_ => console.log('No more coins to collect today!')),
]); // sometimes did not make it click the collect button... moreBtn.isVisible() as alternative also didn't work
// await collectBtn.click().catch(_ => moreBtn.waitFor()); // TODO change this since it's going to delay by timeout if already collected
console.log(await page.locator('.marquee-content:has-text(" coins")').first().innerText());
const n = (await page.locator('.marquee-item:has-text(" coins")').first().innerText()).replace(' coins', '');
console.log('Coins:', n);
// console.log('Streak:', await page.locator('.title-box').innerText());
// console.log('Tomorrow:', await page.locator('.addcoin').innerText());
}; };
const grow = async () => { // const grow = async () => {
await page.pause(); // await page.pause();
}; // };
//
const gogo = async () => { // const gogo = async () => {
await page.pause(); // await page.pause();
}; // };
//
const euro = async () => { // const euro = async () => {
await page.pause(); // await page.pause();
}; // };
//
const merge = async () => { // const merge = async () => {
await page.pause(); // await page.pause();
}; // };
try { try {
// await coins(); // await coins();
await [ await [
// coins, coins,
// grow, // grow,
// gogo, // gogo,
// euro, // euro,
merge, // merge,
].reduce((a, f) => a.then(async _ => { await auth(urls[f.name]); await f(); console.log() }), Promise.resolve()); ].reduce((a, f) => a.then(async _ => {
await auth(urls[f.name]);
await f();
console.log();
}), Promise.resolve());
// await page.pause(); // await page.pause();
} catch (error) { } catch (error) {

View file

@ -4,6 +4,10 @@ services:
container_name: fgc # is printed in front of every output line container_name: fgc # is printed in front of every output line
image: ghcr.io/vogler/free-games-claimer # otherwise image name will be free-games-claimer-free-games-claimer image: ghcr.io/vogler/free-games-claimer # otherwise image name will be free-games-claimer-free-games-claimer
build: . build: .
healthcheck: # see Dockerfile, check that node is running and noVNC is reachable
test: pgrep node && curl --fail http://localhost:6080 || exit 1
interval: 5s
timeout: 5s
ports: ports:
# - "5900:5900" # VNC server # - "5900:5900" # VNC server
- "6080:6080" # noVNC (browser-based VNC client) - "6080:6080" # noVNC (browser-based VNC client)

View file

@ -3,53 +3,38 @@
set -eo pipefail # exit on error, error on any fail in pipe (not just last cmd); add -x to print each cmd; see gist bash_strict_mode.md set -eo pipefail # exit on error, error on any fail in pipe (not just last cmd); add -x to print each cmd; see gist bash_strict_mode.md
echo "Version: https://github.com/vogler/free-games-claimer/tree/${COMMIT}" echo "Version: https://github.com/vogler/free-games-claimer/tree/${COMMIT}"
[ ! -z $BRANCH ] && [ $BRANCH != "main" ] && echo "Branch: ${BRANCH}" [ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && echo "Branch: ${BRANCH}"
echo "Build: $NOW" echo "Build: $NOW"
BROWSER="${BROWSER_DIR:-data/browser}"
# Remove chromium profile lock. # Remove chromium profile lock.
# When running in docker and then killing it, on the next run chromium displayed a dialog to unlock the profile which made the script time out. # When running in docker and then killing it, on the next run chromium displayed a dialog to unlock the profile which made the script time out.
# Maybe due to changed hostname of container or due to how the docker container kills playwright - didn't check. # Maybe due to changed hostname of container or due to how the docker container kills playwright - didn't check.
# https://bugs.chromium.org/p/chromium/issues/detail?id=367048 # https://bugs.chromium.org/p/chromium/issues/detail?id=367048
rm -f /fgc/data/browser/SingletonLock rm -f "/fgc/$BROWSER/SingletonLock"
# Firefox preferences are stored in $BROWSER_DIR/pref.js and can be overridden by a file user.js
# Since this file has to be in the volume (data/browser), we can't do this in Dockerfile.
mkdir -p /fgc/data/browser
# fix for 'Incorrect response' after solving a captcha correctly - https://github.com/vogler/free-games-claimer/issues/261#issuecomment-1868385830
# echo 'user_pref("privacy.resistFingerprinting", true);' > /fgc/data/browser/user.js
cat << EOT > /fgc/data/browser/user.js
user_pref("privacy.resistFingerprinting", true);
// user_pref("privacy.resistFingerprinting.letterboxing", true);
// user_pref("browser.contentblocking.category", "strict");
// user_pref("webgl.disabled", true);
EOT
# TODO disable session restore message?
# Remove X server display lock, fix for `docker compose up` which reuses container which made it fail after initial run, https://github.com/vogler/free-games-claimer/issues/31 # Remove X server display lock, fix for `docker compose up` which reuses container which made it fail after initial run, https://github.com/vogler/free-games-claimer/issues/31
# echo $DISPLAY # Maybe no longer needed after adding #478's -nolisten unix below
# ls -l /tmp/.X11-unix/
rm -f /tmp/.X1-lock rm -f /tmp/.X1-lock
# 6000+SERVERNUM is the TCP port Xvfb is listening on:
# SERVERNUM=$(echo "$DISPLAY" | sed 's/:\([0-9][0-9]*\).*/\1/')
# Options passed directly to the Xvfb server:
# -ac disables host-based access control mechanisms
# screen NUM WxHxD creates the screen and sets its width, height, and depth
export DISPLAY=:1 # need to export this, otherwise playwright complains with 'Looks like you launched a headed browser without having a XServer running.' export DISPLAY=:1 # need to export this, otherwise playwright complains with 'Looks like you launched a headed browser without having a XServer running.'
Xvfb $DISPLAY -ac -screen 0 "${WIDTH}x${HEIGHT}x${DEPTH}" &
echo "Xvfb display server created screen with resolution ${WIDTH}x${HEIGHT}"
if [ -z "$VNC_PASSWORD" ]; then if [ -z "$VNC_PASSWORD" ]; then
pw="-nopw" pw="-SecurityTypes None"
pwt="no password!" pwt="no password!"
else else
pw="-passwd $VNC_PASSWORD" # pw="-passwd $VNC_PASSWORD" # not supported anymore
pwt="with password" pw="-rfbauth ~/.vnc/passwd"
mkdir ~/.vnc/
echo "$VNC_PASSWORD" | /opt/TurboVNC/bin/vncpasswd -f >~/.vnc/passwd
pwt="with password"
fi fi
x11vnc -display $DISPLAY -forever -shared -rfbport $VNC_PORT -bg $pw 2>/dev/null 1>&2 # TurboVNC server replaces Xvfb+x11vnc
echo "VNC is running on port $VNC_PORT ($pwt)" # shellcheck disable=SC2086
websockify -D --web "/usr/share/novnc/" $NOVNC_PORT "localhost:$VNC_PORT" 2>/dev/null 1>&2 & /opt/TurboVNC/bin/vncserver $DISPLAY -geometry "${WIDTH}x${HEIGHT}" -depth "${DEPTH}" -rfbport "${VNC_PORT}" $pw -vgl -log /fgc/data/TurboVNC.log -xstartup /usr/bin/ratpoison 2>/dev/null # -noxstartup -novnc /usr/share/novnc/
echo "TurboVNC is running on port $VNC_PORT ($pwt) with resolution ${WIDTH}x${HEIGHT}"
# TODO keep websockify just for custom NOVNC_PORT? https://www.perplexity.ai/search/how-to-specify-the-novnc-port-rfv96C9tTZufnyFPRye5xA#0
websockify -D --web "/usr/share/novnc/" "$NOVNC_PORT" "localhost:$VNC_PORT" 2>/dev/null 1>&2 &
echo "noVNC (VNC via browser) is running on http://localhost:$NOVNC_PORT" echo "noVNC (VNC via browser) is running on http://localhost:$NOVNC_PORT"
echo echo
exec tini -g -- "$@" # https://github.com/krallin/tini/issues/8 node/playwright respond to signals like ctrl-c, but unsure about zombie processes exec tini -g -- "$@" # https://github.com/krallin/tini/issues/8 node/playwright respond to signals like ctrl-c, but unsure about zombie processes

View file

@ -1,9 +1,12 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra // import { chromium } from 'playwright-chromium';
import { chromium } from 'patchright';
import { authenticator } from 'otplib'; import { authenticator } from 'otplib';
import chalk from 'chalk';
import path from 'path'; import path from 'path';
import { existsSync, writeFileSync, appendFileSync } from 'fs'; import { existsSync, writeFileSync } from 'fs';
import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './src/util.js'; import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js'; import { cfg } from './src/config.js';
import { getGames } from './src/epic-games-mobile.js';
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'epic-games', ...a); const screenshot = (...a) => resolve(cfg.dir.screenshots, 'epic-games', ...a);
@ -16,43 +19,36 @@ const db = await jsonDb('epic-games.json', {});
if (cfg.time) console.time('startup'); if (cfg.time) console.time('startup');
const browserPrefs = path.join(cfg.dir.browser, 'prefs.js');
if (existsSync(browserPrefs)) {
console.log('Adding webgl.disabled to', browserPrefs);
appendFileSync(browserPrefs, 'user_pref("webgl.disabled", true);'); // apparently Firefox removes duplicates (and sorts), so no problem appending every time
} else {
console.log(browserPrefs, 'does not exist yet, will patch it on next run. Restart the script if you get a captcha.');
}
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, // channel: 'chrome', // recommended, but `npx patchright install chrome` clashes with system Chrome - https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-nodejs#best-practice----use-chrome-without-fingerprint-injection
headless: false, // don't use cfg.headless headless here since SHOW=0 will lead to captcha
viewport: { width: cfg.width, height: cfg.height }, viewport: { width: cfg.width, height: cfg.height },
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0', // see replace of Headless in util.newStealthContext. TODO Windows UA enough to avoid 'device not supported'? update if browser is updated?
// userAgent firefox (macOS): Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0
// userAgent firefox (docker): Mozilla/5.0 (X11; Linux aarch64; rv:109.0) Gecko/20100101 Firefox/115.0
locale: 'en-US', // ignore OS locale to be sure to have english text for locators locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800 recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/eg-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools recordHar: cfg.record ? { path: `data/record/eg-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// user settings for firefox have to be put in $BROWSER_DIR/user.js // https://peter.sh/experiments/chromium-command-line-switches/
args: [ // https://wiki.mozilla.org/Firefox/CommandLineOptions args: [
// '-kiosk', '--hide-crash-restore-bubble',
'--ignore-gpu-blocklist', // required for OpenGL: Disabled -> Enabled & WebGL: Software only -> Hardware accelerated
'--enable-unsafe-webgpu', // required for WebGPU: Disabled -> Hardware accelerated
], ],
// The following makes the browser crash in docker with 'Chromium sandboxing failed!':
// chromiumSandbox: true, // https://github.com/Kaliiiiiiiiii-Vinyzu/patchright/issues/52
}); });
handleSIGINT(context); // console.log(context.browser().browserType()); // browser is null...
if (cfg.debug) console.log(chromium.executablePath());
// Without stealth plugin, the website shows an hcaptcha on login with username/password and in the last step of claiming a game. It may have other heuristics like unsuccessful logins as well. After <6h (TBD) it resets to no captcha again. Getting a new IP also resets. handleSIGINT(context);
await stealth(context);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout); if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
await page.setViewportSize({ width: cfg.width, height: cfg.height }); // TODO workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it // await page.setViewportSize({ width: cfg.width, height: cfg.height }); // TODO workaround for https://github.com/vogler/free-games-claimer/issues/277 until Playwright fixes it
// some debug info about the page (screen dimensions, user agent, platform) // some debug info about the page (screen dimensions, user agent, platform)
// eslint-disable-next-line no-undef
if (cfg.debug) console.debug(await page.evaluate(() => [(({ width, height, availWidth, availHeight }) => ({ width, height, availWidth, availHeight }))(window.screen), navigator.userAgent, navigator.platform, navigator.vendor])); // deconstruct screen needed since `window.screen` prints {}, `window.screen.toString()` '[object Screen]', and can't use some pick function without defining it on `page` if (cfg.debug) console.debug(await page.evaluate(() => [(({ width, height, availWidth, availHeight }) => ({ width, height, availWidth, availHeight }))(window.screen), navigator.userAgent, navigator.platform, navigator.vendor])); // deconstruct screen needed since `window.screen` prints {}, `window.screen.toString()` '[object Screen]', and can't use some pick function without defining it on `page`
if (cfg.debug_network) { if (cfg.debug_network) {
// const filter = _ => true; // const filter = _ => true;
@ -79,6 +75,7 @@ try {
while (await page.locator('egs-navigation').getAttribute('isloggedin') != 'true') { while (await page.locator('egs-navigation').getAttribute('isloggedin') != 'true') {
console.error('Not signed in anymore. Please login in the browser or here in the terminal.'); console.error('Not signed in anymore. Please login in the browser or here in the terminal.');
if (cfg.nowait) process.exit(1);
if (cfg.novnc_port) console.info(`Open http://localhost:${cfg.novnc_port} to login inside the docker container.`); if (cfg.novnc_port) console.info(`Open http://localhost:${cfg.novnc_port} to login inside the docker container.`);
if (!cfg.debug) context.setDefaultTimeout(cfg.login_timeout); // give user some extra time to log in if (!cfg.debug) context.setDefaultTimeout(cfg.login_timeout); // give user some extra time to log in
console.info(`Login timeout is ${cfg.login_timeout / 1000} seconds!`); console.info(`Login timeout is ${cfg.login_timeout / 1000} seconds!`);
@ -149,8 +146,17 @@ try {
// debug showed that in those cases the href was still correct, so we `goto` the urls instead of clicking. // debug showed that in those cases the href was still correct, so we `goto` the urls instead of clicking.
// Alternative: parse the json loaded to build the page https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions // Alternative: parse the json loaded to build the page https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions
// i.e. filter data.Catalog.searchStore.elements for .promotions.promotionalOffers being set and build URL with .catalogNs.mappings[0].pageSlug or .urlSlug if not set to some wrong id like it was the case for spirit-of-the-north-f58a66 - this is also what's done here: https://github.com/claabs/epicgames-freegames-node/blob/938a9653ffd08b8284ea32cf01ac8727d25c5d4c/src/puppet/free-games.ts#L138-L213 // i.e. filter data.Catalog.searchStore.elements for .promotions.promotionalOffers being set and build URL with .catalogNs.mappings[0].pageSlug or .urlSlug if not set to some wrong id like it was the case for spirit-of-the-north-f58a66 - this is also what's done here: https://github.com/claabs/epicgames-freegames-node/blob/938a9653ffd08b8284ea32cf01ac8727d25c5d4c/src/puppet/free-games.ts#L138-L213
const urlSlugs = await Promise.all((await game_loc.elementHandles()).map(a => a.getAttribute('href'))); const urlSlugs = await Promise.all((await game_loc.all()).map(a => a.getAttribute('href')));
const urls = urlSlugs.map(s => 'https://store.epicgames.com' + s); const urls = urlSlugs.map(s => 'https://store.epicgames.com' + s);
// Free mobile games - https://github.com/vogler/free-games-claimer/issues/474
// https://egs-platform-service.store.epicgames.com/api/v2/public/discover/home?count=10&country=DE&locale=en&platform=android&start=0&store=EGS
if (cfg.eg_mobile) {
console.log('Including mobile games...');
const mobileGames = await getGames();
urls.push(...mobileGames.map(x => x.url));
}
console.log('Free games:', urls); console.log('Free games:', urls);
for (const url of urls) { for (const url of urls) {
@ -192,7 +198,7 @@ try {
const game_id = page.url().split('/').pop(); const game_id = page.url().split('/').pop();
const existedInDb = db.data[user][game_id]; const existedInDb = db.data[user][game_id];
db.data[user][game_id] ||= { title, time: datetime(), url: page.url() }; // this will be set on the initial run only! db.data[user][game_id] ||= { title, time: datetime(), url: page.url() }; // this will be set on the initial run only!
console.log('Current free game:', title); console.log('Current free game:', chalk.blue(title));
if (bundle_includes) console.log(' This bundle includes:', bundle_includes); if (bundle_includes) console.log(' This bundle includes:', bundle_includes);
const notify_game = { title, url, status: 'failed' }; const notify_game = { title, url, status: 'failed' };
notify_games.push(notify_game); // status is updated below notify_games.push(notify_game); // status is updated below
@ -260,6 +266,7 @@ try {
if (cfg.time) console.timeEnd('claim game'); if (cfg.time) console.timeEnd('claim game');
continue; continue;
} }
if (cfg.interactive && !await confirm()) continue;
// Playwright clicked before button was ready to handle event, https://github.com/vogler/free-games-claimer/issues/84#issuecomment-1474346591 // Playwright clicked before button was ready to handle event, https://github.com/vogler/free-games-claimer/issues/84#issuecomment-1474346591
await iframe.locator('button:has-text("Place Order"):not(:has(.payment-loading--loading))').click({ delay: 11 }); await iframe.locator('button:has-text("Place Order"):not(:has(.payment-loading--loading))').click({ delay: 11 });

View file

@ -2,75 +2,78 @@
// https://eslint.org/docs/latest/use/configure/migration-guide // https://eslint.org/docs/latest/use/configure/migration-guide
import js from '@eslint/js'; import js from '@eslint/js';
import globals from 'globals'; import globals from 'globals';
import stylistic from '@stylistic/eslint-plugin-js'; import stylistic from '@stylistic/eslint-plugin';
export default [ export default [
// https://eslint.org/docs/latest/use/configure/configuration-files-new#globally-ignoring-files-with-ignores // https://eslint.org/docs/latest/use/configure/configuration-files-new#globally-ignoring-files-with-ignores
// object with just `ignores` applies to all configuration objects // object with just `ignores` applies to all configuration objects
// had `ln -s .gitignore .eslintignore` before, but .eslintignore no longer supported // had `ln -s .gitignore .eslintignore` before, but .eslintignore no longer supported
{ {
ignores: ['data/**'], ignores: ['data/**', 'megalinter-reports/**'],
}, },
js.configs.recommended, // TODO still needed? js.configs.recommended, // TODO still needed?
{ {
// files: ['*.js'], // files: ['*.js'],
languageOptions: { languageOptions: {
globals: globals.node, globals: {
...globals.node,
...globals.browser,
},
}, },
plugins: { plugins: {
'@stylistic/js': stylistic, '@stylistic': stylistic,
}, },
// https://eslint.org/docs/latest/rules/ // https://eslint.org/docs/latest/rules/
// https://eslint.style/packages/js // https://eslint.style/packages/js
rules: { rules: {
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'prefer-const': 'error', 'prefer-const': 'error',
'@stylistic/js/array-bracket-newline': ['error', 'consistent'], '@stylistic/array-bracket-newline': ['error', 'consistent'],
'@stylistic/js/array-bracket-spacing': 'error', '@stylistic/array-bracket-spacing': 'error',
'@stylistic/js/array-element-newline': ['error', 'consistent'], '@stylistic/array-element-newline': ['error', 'consistent'],
'@stylistic/js/arrow-parens': ['error', 'as-needed'], '@stylistic/arrow-parens': ['error', 'as-needed'],
'@stylistic/js/arrow-spacing': 'error', '@stylistic/arrow-spacing': 'error',
'@stylistic/js/block-spacing': 'error', '@stylistic/block-spacing': 'error',
'@stylistic/js/brace-style': 'error', '@stylistic/brace-style': 'error',
'@stylistic/js/comma-dangle': ['error', 'always-multiline'], '@stylistic/comma-dangle': ['error', 'always-multiline'],
'@stylistic/js/comma-spacing': 'error', '@stylistic/comma-spacing': 'error',
'@stylistic/js/comma-style': 'error', '@stylistic/comma-style': 'error',
'@stylistic/js/eol-last': 'error', '@stylistic/eol-last': 'error',
'@stylistic/js/func-call-spacing': 'error', '@stylistic/func-call-spacing': 'error',
'@stylistic/js/function-paren-newline': ['error', 'consistent'], '@stylistic/function-paren-newline': ['error', 'consistent'],
'@stylistic/js/implicit-arrow-linebreak': 'error', '@stylistic/implicit-arrow-linebreak': 'error',
'@stylistic/js/indent': ['error', 2], '@stylistic/indent': ['error', 2],
'@stylistic/js/key-spacing': 'error', '@stylistic/key-spacing': 'error',
'@stylistic/js/keyword-spacing': 'error', '@stylistic/keyword-spacing': 'error',
'@stylistic/js/linebreak-style': 'error', '@stylistic/linebreak-style': 'error',
'@stylistic/js/no-extra-parens': 'error', '@stylistic/no-extra-parens': 'error',
'@stylistic/js/no-extra-semi': 'error', '@stylistic/no-extra-semi': 'error',
'@stylistic/js/no-mixed-spaces-and-tabs': 'error', '@stylistic/no-mixed-spaces-and-tabs': 'error',
'@stylistic/js/no-multi-spaces': 'error', '@stylistic/no-multi-spaces': 'error',
'@stylistic/js/no-multiple-empty-lines': 'error', '@stylistic/no-multiple-empty-lines': 'error',
'@stylistic/js/no-tabs': 'error', '@stylistic/no-tabs': 'error',
'@stylistic/js/no-trailing-spaces': 'error', '@stylistic/no-trailing-spaces': 'error',
'@stylistic/js/no-whitespace-before-property': 'error', '@stylistic/no-whitespace-before-property': 'error',
'@stylistic/js/nonblock-statement-body-position': 'error', '@stylistic/nonblock-statement-body-position': 'error',
'@stylistic/js/object-curly-newline': 'error', '@stylistic/object-curly-newline': 'error',
'@stylistic/js/object-curly-spacing': ['error', 'always'], '@stylistic/object-curly-spacing': ['error', 'always'],
'@stylistic/js/object-property-newline': ['error', { allowAllPropertiesOnSameLine: true }], '@stylistic/object-property-newline': ['error', { allowAllPropertiesOnSameLine: true }],
'@stylistic/js/quote-props': ['error', 'as-needed'], '@stylistic/quote-props': ['error', 'as-needed'],
'@stylistic/js/quotes': ['error', 'single'], '@stylistic/quotes': ['error', 'single'],
'@stylistic/js/rest-spread-spacing': 'error', '@stylistic/rest-spread-spacing': 'error',
'@stylistic/js/semi': 'error', '@stylistic/semi': 'error',
'@stylistic/js/semi-spacing': 'error', '@stylistic/semi-spacing': 'error',
'@stylistic/js/semi-style': 'error', '@stylistic/semi-style': 'error',
'@stylistic/js/space-before-blocks': 'error', '@stylistic/space-before-blocks': 'error',
'@stylistic/js/space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }], '@stylistic/space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
'@stylistic/js/space-in-parens': 'error', '@stylistic/space-in-parens': 'error',
'@stylistic/js/space-infix-ops': 'error', '@stylistic/space-infix-ops': 'error',
'@stylistic/js/space-unary-ops': 'error', '@stylistic/space-unary-ops': 'error',
'@stylistic/js/spaced-comment': 'error', '@stylistic/spaced-comment': 'error',
'@stylistic/js/switch-colon-spacing': 'error', '@stylistic/switch-colon-spacing': 'error',
'@stylistic/js/template-curly-spacing': 'error', '@stylistic/template-curly-spacing': 'error',
'@stylistic/js/template-tag-spacing': 'error', '@stylistic/template-tag-spacing': 'error',
'@stylistic/js/wrap-regex': 'error', '@stylistic/wrap-regex': 'error',
}, },
}, },
]; ];

33
gog.js
View file

@ -1,5 +1,7 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra // import { firefox } from 'playwright-firefox';
import { resolve, jsonDb, datetime, filenamify, prompt, notify, html_game_list, handleSIGINT } from './src/util.js'; import { chromium } from 'patchright';
import chalk from 'chalk';
import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js'; import { cfg } from './src/config.js';
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'gog', ...a); const screenshot = (...a) => resolve(cfg.dir.screenshots, 'gog', ...a);
@ -16,13 +18,17 @@ if (cfg.width < 1280) { // otherwise 'Sign in' and #menuUsername are hidden (but
} }
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
viewport: { width: cfg.width, height: cfg.height }, viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800 recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/gog-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools recordHar: cfg.record ? { path: `data/record/gog-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// https://peter.sh/experiments/chromium-command-line-switches/
args: [
'--hide-crash-restore-bubble',
],
}); });
handleSIGINT(context); handleSIGINT(context);
@ -43,9 +49,12 @@ try {
// page.click('#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll').catch(_ => { }); // does not work reliably, solved by setting CookieConsent above // page.click('#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll').catch(_ => { }); // does not work reliably, solved by setting CookieConsent above
const signIn = page.locator('a:has-text("Sign in")').first(); const signIn = page.locator('a:has-text("Sign in")').first();
await Promise.any([signIn.waitFor(), page.waitForSelector('#menuUsername')]); // TODO for the below signIn.waitFor(), patchright failed most of the time with: locator.waitFor: JSHandles can be evaluated only in the context they were created!
while (await signIn.isVisible()) { // await Promise.any([signIn.waitFor(), page.waitForSelector('#menuUsername')]);
console.error('Not signed in anymore.'); const username = page.locator('#menuUsername').first();
while (await signIn.isVisible() && !await username.isVisible()) {
console.error('Not signed!');
if (cfg.nowait) process.exit(1);
await signIn.click(); await signIn.click();
// it then creates an iframe for the login // it then creates an iframe for the login
await page.waitForSelector('#GalaxyAccountsFrameContainer iframe'); // TODO needed? await page.waitForSelector('#GalaxyAccountsFrameContainer iframe'); // TODO needed?
@ -57,10 +66,14 @@ try {
const email = cfg.gog_email || await prompt({ message: 'Enter email' }); const email = cfg.gog_email || await prompt({ message: 'Enter email' });
const password = email && (cfg.gog_password || await prompt({ type: 'password', message: 'Enter password' })); const password = email && (cfg.gog_password || await prompt({ type: 'password', message: 'Enter password' }));
if (email && password) { if (email && password) {
iframe.locator('a[href="/logout"]').click().catch(_ => { }); // Click 'Change account' (email from previous login is set in some cookie) // iframe.locator('a[href="/logout"]').click().catch(_ => { }); // Click 'Change account' (email from previous login is set in some cookie)
await iframe.locator('#login_username').fill(email); // TODO above didn't work with patchright
if (!await iframe.locator('#login_username').isDisabled()) {
await iframe.locator('#login_username').fill(email);
}
await iframe.locator('#login_password').fill(password); await iframe.locator('#login_password').fill(password);
await iframe.locator('#login_login').click(); await iframe.locator('#login_login').click();
await page.waitForTimeout(2000); // TODO patchright waits forever for MFA locator otherwise
// handle MFA, but don't await it // handle MFA, but don't await it
iframe.locator('form[name=second_step_authentication]').waitFor().then(async () => { iframe.locator('form[name=second_step_authentication]').waitFor().then(async () => {
console.log('Two-Step Verification - Enter security code'); console.log('Two-Step Verification - Enter security code');
@ -95,6 +108,7 @@ try {
db.data[user] ||= {}; db.data[user] ||= {};
const banner = page.locator('#giveaway'); const banner = page.locator('#giveaway');
await page.waitForTimeout(2000); // TODO patchright sometimes missed banner otherwise
if (!await banner.count()) { if (!await banner.count()) {
console.log('Currently no free giveaway!'); console.log('Currently no free giveaway!');
} else { } else {
@ -102,9 +116,10 @@ try {
const match_all = text.match(/Claim (.*) and don't miss the|Success! (.*) was added to/); const match_all = text.match(/Claim (.*) and don't miss the|Success! (.*) was added to/);
const title = match_all[1] ? match_all[1] : match_all[2]; const title = match_all[1] ? match_all[1] : match_all[2];
const url = await banner.locator('a').first().getAttribute('href'); const url = await banner.locator('a').first().getAttribute('href');
console.log(`Current free game: ${title} - ${url}`); console.log(`Current free game: ${chalk.blue(title)} - ${url}`);
db.data[user][title] ||= { title, time: datetime(), url }; db.data[user][title] ||= { title, time: datetime(), url };
if (cfg.dryrun) process.exit(1); if (cfg.dryrun) process.exit(1);
if (cfg.interactive && !await confirm()) process.exit(0);
// await page.locator('#giveaway:not(.is-loading)').waitFor(); // otherwise screenshot is sometimes with loading indicator instead of game title; #TODO fix, skipped due to timeout, see #240 // await page.locator('#giveaway:not(.is-loading)').waitFor(); // otherwise screenshot is sometimes with loading indicator instead of game title; #TODO fix, skipped due to timeout, see #240
await banner.screenshot({ path: screenshot(`${filenamify(title)}.png`) }); // overwrites every time - only keep first? await banner.screenshot({ path: screenshot(`${filenamify(title)}.png`) }); // overwrites every time - only keep first?

View file

@ -3,7 +3,7 @@
"checkJs": true, "checkJs": true,
"target": "es2021", "target": "es2021",
"module": "NodeNext", "module": "NodeNext",
"moduleResolution": "NodeNext", // https://github.com/typicode/lowdb/issues/554 "moduleResolution": "NodeNext" // https://github.com/typicode/lowdb/issues/554
}, },
"exclude": ["node_modules", "**/node_modules"] "exclude": ["node_modules", "**/node_modules"]
} }

1798
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"docker:build": "docker build . -t ghcr.io/vogler/free-games-claimer", "docker:build": "docker build . -t ghcr.io/vogler/free-games-claimer",
"docker": "cross-env-shell docker run --rm -it -p 5900:5900 -p 6080:6080 -v \\\"$INIT_CWD/data\\\":/fgc/data --name fgc ghcr.io/vogler/free-games-claimer", "docker:build-log": "(docker buildx build --progress=plain . -t ghcr.io/vogler/free-games-claimer; docker image ls) 2>&1 | tee data/docker-build.log",
"docker": "docker run --rm -it -p 6080:6080 -v fgc:/fgc/data --pull=always ghcr.io/vogler/free-games-claimer",
"lint": "npx eslint ." "lint": "npx eslint ."
}, },
"type": "module", "type": "module",
@ -20,18 +21,16 @@
"node": ">=17" "node": ">=17"
}, },
"dependencies": { "dependencies": {
"chalk": "^5.3.0", "chalk": "^5.4.1",
"cross-env": "^7.0.3", "dotenv": "^16.5.0",
"dotenv": "^16.4.5",
"enquirer": "^2.4.1", "enquirer": "^2.4.1",
"fingerprint-injector": "^2.1.52", "fingerprint-injector": "^2.1.66",
"lowdb": "^7.0.1", "lowdb": "^7.0.1",
"otplib": "^12.0.1", "otplib": "^12.0.1",
"playwright-firefox": "^1.45.0", "patchright": "^1.52.4"
"puppeteer-extra-plugin-stealth": "^2.11.2"
}, },
"devDependencies": { "devDependencies": {
"@stylistic/eslint-plugin-js": "^4.0.0", "@stylistic/eslint-plugin": "^4.4.0",
"eslint": "^9.5.0" "eslint": "^9.27.0"
} }
} }

View file

@ -1,7 +1,8 @@
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra // import { chromium } from 'playwright-chromium';
import { chromium } from 'patchright';
import { authenticator } from 'otplib'; import { authenticator } from 'otplib';
import chalk from 'chalk'; import chalk from 'chalk';
import { resolve, jsonDb, datetime, stealth, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js'; import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js'; import { cfg } from './src/config.js';
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a); const screenshot = (...a) => resolve(cfg.dir.screenshots, 'prime-gaming', ...a);
@ -14,20 +15,20 @@ console.log(datetime(), 'started checking prime-gaming');
const db = await jsonDb('prime-gaming.json', {}); const db = await jsonDb('prime-gaming.json', {});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
viewport: { width: cfg.width, height: cfg.height }, viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800 recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/pg-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools recordHar: cfg.record ? { path: `data/record/pg-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
args: [
'--hide-crash-restore-bubble',
],
}); });
handleSIGINT(context); handleSIGINT(context);
// TODO test if needed
await stealth(context);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout); if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
@ -44,6 +45,7 @@ try {
page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(_ => { }); // to not waste screen space when non-headless, TODO does not work reliably, need to wait for something else first? page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(_ => { }); // to not waste screen space when non-headless, TODO does not work reliably, need to wait for something else first?
while (await page.locator('button:has-text("Sign in")').count() > 0) { while (await page.locator('button:has-text("Sign in")').count() > 0) {
console.error('Not signed in anymore.'); console.error('Not signed in anymore.');
if (cfg.nowait) process.exit(1);
await page.click('button:has-text("Sign in")'); await page.click('button:has-text("Sign in")');
if (!cfg.debug) context.setDefaultTimeout(cfg.login_timeout); // give user some extra time to log in if (!cfg.debug) context.setDefaultTimeout(cfg.login_timeout); // give user some extra time to log in
console.info(`Login timeout is ${cfg.login_timeout / 1000} seconds!`); console.info(`Login timeout is ${cfg.login_timeout / 1000} seconds!`);
@ -55,7 +57,7 @@ try {
await page.fill('[name=email]', email); await page.fill('[name=email]', email);
await page.click('input[type="submit"]'); await page.click('input[type="submit"]');
await page.fill('[name=password]', password); await page.fill('[name=password]', password);
await page.check('[name=rememberMe]'); // await page.check('[name=rememberMe]'); // no longer exists
await page.click('input[type="submit"]'); await page.click('input[type="submit"]');
page.waitForURL('**/ap/signin**').then(async () => { // check for wrong credentials page.waitForURL('**/ap/signin**').then(async () => { // check for wrong credentials
const error = await page.locator('.a-alert-content').first().innerText(); const error = await page.locator('.a-alert-content').first().innerText();
@ -126,47 +128,49 @@ try {
await scrollUntilStable(() => page.evaluate(() => document.querySelector('.tw-full-width').scrollHeight)); // height may change during loading while number of games is still the same? await scrollUntilStable(() => page.evaluate(() => document.querySelector('.tw-full-width').scrollHeight)); // height may change during loading while number of games is still the same?
console.log('Number of already claimed games (total):', await games.locator('p:has-text("Collected")').count()); console.log('Number of already claimed games (total):', await games.locator('p:has-text("Collected")').count());
// can't use .all() since the list of elements via locator will change after click while we iterate over it // can't use .all() since the list of elements via locator will change after click while we iterate over it
const internal = await games.locator('.item-card__action:has(button[data-a-target="FGWPOffer"])').elementHandles(); const internal = await games.locator('.item-card__action:has(button[data-a-target="FGWPOffer"])').all();
const external = await games.locator('.item-card__action:has(a[data-a-target="FGWPOffer"])').all(); const external = await games.locator('.item-card__action:has(a[data-a-target="FGWPOffer"])').all();
// bottom to top: oldest to newest games // bottom to top: oldest to newest games
internal.reverse(); internal.reverse();
external.reverse(); external.reverse();
const checkTimeLeft = async url => { const sameOrNewPage = async url => {
// console.log(' Checking time left for game:', url); const isNew = page.url() != url;
const check = async p => { let p = page;
console.log(' ', await p.locator('.availability-date').innerText()); if (isNew) {
const dueDateOrg = await p.locator('.availability-date .tw-bold').innerText(); p = await context.newPage();
const dueDate = datetime(new Date(Date.parse(dueDateOrg + ' 17:00')));
console.log(' Due date:', dueDate);
};
if (page.url() == url) {
await check(page);
} else {
const p = await context.newPage();
await p.goto(url, { waitUntil: 'domcontentloaded' }); await p.goto(url, { waitUntil: 'domcontentloaded' });
await check(p);
p.close();
} }
return { p, isNew };
}; };
console.log('Number of free unclaimed games (Prime Gaming):', internal.length); const skipBasedOnTime = async url => {
// console.log(' Checking time left for game:', url);
const { p, isNew } = await sameOrNewPage(url);
const dueDateOrg = await p.locator('.availability-date .tw-bold').innerText();
const dueDate = new Date(Date.parse(dueDateOrg + ' 17:00'));
const daysLeft = (dueDate.getTime() - Date.now()) / 1000 / 60 / 60 / 24;
console.log(' ', await p.locator('.availability-date').innerText(), '->', daysLeft.toFixed(2));
if (isNew) await p.close();
return daysLeft > cfg.pg_timeLeft;
};
console.log('\nNumber of free unclaimed games (Prime Gaming):', internal.length);
// claim games in internal store // claim games in internal store
for (const card of internal) { for (const card of internal) {
await card.scrollIntoViewIfNeeded(); await card.scrollIntoViewIfNeeded();
const title = await (await card.$('.item-card-details__body__primary')).innerText(); const title = await (await card.locator('.item-card-details__body__primary')).innerText();
const slug = await (await card.$('a')).getAttribute('href'); const slug = await (await card.locator('a')).getAttribute('href');
const url = 'https://gaming.amazon.com' + slug.split('?')[0]; const url = 'https://gaming.amazon.com' + slug.split('?')[0];
console.log('Current free game:', title); console.log('Current free game:', chalk.blue(title));
if (cfg.pg_timeLeft) await checkTimeLeft(url); if (cfg.pg_timeLeft && await skipBasedOnTime(url)) continue;
if (cfg.dryrun) continue; if (cfg.dryrun) continue;
if (cfg.interactive && !await confirm()) continue; if (cfg.interactive && !await confirm()) continue;
await (await card.$('.tw-button:has-text("Claim")')).click(); await card.locator('.tw-button:has-text("Claim")').click();
db.data[user][title] ||= { title, time: datetime(), url, store: 'internal' }; db.data[user][title] ||= { title, time: datetime(), url, store: 'internal' };
notify_games.push({ title, status: 'claimed', url }); notify_games.push({ title, status: 'claimed', url });
// const img = await (await card.$('img.tw-image')).getAttribute('src'); // const img = await card.locator('img.tw-image').getAttribute('src');
// console.log('Image:', img); // console.log('Image:', img);
await card.screenshot({ path: screenshot('internal', `${filenamify(title)}.png`) }); await card.screenshot({ path: screenshot('internal', `${filenamify(title)}.png`) });
} }
console.log('Number of free unclaimed games (external stores):', external.length); console.log('\nNumber of free unclaimed games (external stores):', external.length);
// claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com, microsoft // claim games in external/linked stores. Linked: origin.com, epicgames.com; Redeem-key: gog.com, legacygames.com, microsoft
const external_info = []; const external_info = [];
for (const card of external) { // need to get data incl. URLs in this loop and then navigate in another, otherwise .all() would update after coming back and .elementHandles() like above would lead to error due to page navigation: elementHandle.$: Protocol error (Page.adoptNode) for (const card of external) { // need to get data incl. URLs in this loop and then navigate in another, otherwise .all() would update after coming back and .elementHandles() like above would lead to error due to page navigation: elementHandle.$: Protocol error (Page.adoptNode)
@ -178,13 +182,13 @@ try {
} }
// external_info = [ { title: 'Fallout 76 (XBOX)', url: 'https://gaming.amazon.com/fallout-76-xbox-fgwp/dp/amzn1.pg.item.9fe17d7b-b6c2-4f58-b494-cc4e79528d0b?ingress=amzn&ref_=SM_Fallout76XBOX_S01_FGWP_CRWN' } ]; // external_info = [ { title: 'Fallout 76 (XBOX)', url: 'https://gaming.amazon.com/fallout-76-xbox-fgwp/dp/amzn1.pg.item.9fe17d7b-b6c2-4f58-b494-cc4e79528d0b?ingress=amzn&ref_=SM_Fallout76XBOX_S01_FGWP_CRWN' } ];
for (const { title, url } of external_info) { for (const { title, url } of external_info) {
console.log('Current free game:', title); // , url); console.log('Current free game:', chalk.blue(title)); // , url);
await page.goto(url, { waitUntil: 'domcontentloaded' }); await page.goto(url, { waitUntil: 'domcontentloaded' });
if (cfg.debug) await page.pause(); if (cfg.debug) await page.pause();
const item_text = await page.innerText('[data-a-target="DescriptionItemDetails"]'); const item_text = await page.innerText('[data-a-target="DescriptionItemDetails"]');
const store = item_text.toLowerCase().replace(/.* on /, '').slice(0, -1); const store = item_text.toLowerCase().replace(/.* on /, '').slice(0, -1);
console.log(' External store:', store); console.log(' External store:', store);
if (cfg.pg_timeLeft) await checkTimeLeft(url); if (cfg.pg_timeLeft && await skipBasedOnTime(url)) continue;
if (cfg.dryrun) continue; if (cfg.dryrun) continue;
if (cfg.interactive && !await confirm()) continue; if (cfg.interactive && !await confirm()) continue;
await Promise.any([page.click('[data-a-target="buy-box"] .tw-button:has-text("Get game")'), page.click('[data-a-target="buy-box"] .tw-button:has-text("Claim")'), page.click('.tw-button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")'), page.waitForSelector('.thank-you-title:has-text("Success")')]); // waits for navigation await Promise.any([page.click('[data-a-target="buy-box"] .tw-button:has-text("Get game")'), page.click('[data-a-target="buy-box"] .tw-button:has-text("Claim")'), page.click('.tw-button:has-text("Complete Claim")'), page.waitForSelector('div:has-text("Link game account")'), page.waitForSelector('.thank-you-title:has-text("Success")')]); // waits for navigation
@ -215,7 +219,7 @@ try {
const code = await Promise.any([page.inputValue('input[type="text"]'), page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', ''))]); // input: Legacy Games; text: gog.com const code = await Promise.any([page.inputValue('input[type="text"]'), page.textContent('[data-a-target="ClaimStateClaimCodeContent"]').then(s => s.replace('Your code: ', ''))]); // input: Legacy Games; text: gog.com
console.log(' Code to redeem game:', chalk.blue(code)); console.log(' Code to redeem game:', chalk.blue(code));
if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/ if (store == 'legacy games') { // may be different URL like https://legacygames.com/primeday/puzzleoftheyear/
redeem[store] = await (await page.$('li:has-text("Click here") a')).getAttribute('href'); // full text: Click here to enter your redemption code. redeem[store] = await page.locator('li:has-text("Click here") a').getAttribute('href'); // full text: Click here to enter your redemption code.
} }
let redeem_url = redeem[store]; let redeem_url = redeem[store];
if (store == 'gog.com') redeem_url += '/' + code; // to log and notify, but can't use for goto below (captcha) if (store == 'gog.com') redeem_url += '/' + code; // to log and notify, but can't use for goto below (captcha)
@ -256,10 +260,14 @@ try {
const r2 = page2.waitForResponse(r => r.request().method() == 'POST' && r.url().startsWith('https://redeem.gog.com/')); const r2 = page2.waitForResponse(r => r.request().method() == 'POST' && r.url().startsWith('https://redeem.gog.com/'));
await page2.click('[type="submit"]'); // click Redeem await page2.click('[type="submit"]'); // click Redeem
const r2t = await (await r2).text(); const r2t = await (await r2).text();
const reason2 = JSON.parse(r2t).reason;
if (r2t == '{}') { if (r2t == '{}') {
redeem_action = 'redeemed'; redeem_action = 'redeemed';
console.log(' Redeemed successfully.'); console.log(' Redeemed successfully.');
db.data[user][title].status = 'claimed and redeemed'; db.data[user][title].status = 'claimed and redeemed';
} else if (reason2?.includes('captcha')) {
redeem_action = 'redeem (got captcha)';
console.error(' Got captcha; could not redeem!');
} else { } else {
console.debug(` Response 2: ${r2t}`); console.debug(` Response 2: ${r2t}`);
console.log(' Unknown Response 2 - please report in https://github.com/vogler/free-games-claimer/issues/5'); console.log(' Unknown Response 2 - please report in https://github.com/vogler/free-games-claimer/issues/5');
@ -269,7 +277,7 @@ try {
console.error(` Redeem on ${store} is experimental!`); console.error(` Redeem on ${store} is experimental!`);
// await page2.pause(); // await page2.pause();
if (page2.url().startsWith('https://login.')) { if (page2.url().startsWith('https://login.')) {
console.error(' Not logged in! Use the browser to login manually. Waiting for 60s.'); console.error(' Not logged in! Please redeem the code above manually. You can now login in the browser for next time. Waiting for 60s.');
await page2.waitForTimeout(60 * 1000); await page2.waitForTimeout(60 * 1000);
redeem_action = 'redeem (login)'; redeem_action = 'redeem (login)';
} else { } else {
@ -294,7 +302,7 @@ try {
if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') { if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') {
redeem_action = 'already redeemed'; redeem_action = 'already redeemed';
console.error(' error: UserAlreadyOwnsContent'); console.error(' error: UserAlreadyOwnsContent');
} else if (true) { // TODO what's returned on success? } else { // TODO what's returned on success?
redeem_action = 'redeemed'; redeem_action = 'redeemed';
db.data[user][title].status = 'claimed and redeemed?'; db.data[user][title].status = 'claimed and redeemed?';
console.log(' Redeemed successfully? Please report if not in https://github.com/vogler/free-games-claimer/issues/5'); console.log(' Redeemed successfully? Please report if not in https://github.com/vogler/free-games-claimer/issues/5');
@ -306,6 +314,7 @@ try {
} }
} }
} else if (store == 'legacy games') { } else if (store == 'legacy games') {
// await page2.pause();
await page2.fill('[name=coupon_code]', code); await page2.fill('[name=coupon_code]', code);
await page2.fill('[name=email]', cfg.lg_email); await page2.fill('[name=email]', cfg.lg_email);
await page2.fill('[name=email_validate]', cfg.lg_email); await page2.fill('[name=email_validate]', cfg.lg_email);
@ -359,7 +368,7 @@ try {
await loot.waitFor(); await loot.waitFor();
process.stdout.write('Loading all DLCs on page...'); process.stdout.write('Loading all DLCs on page...');
await scrollUntilStable(() => loot.locator('[data-a-target="item-card"]').count()) await scrollUntilStable(() => loot.locator('[data-a-target="item-card"]').count());
console.log('\nNumber of already claimed DLC:', await loot.locator('p:has-text("Collected")').count()); console.log('\nNumber of already claimed DLC:', await loot.locator('p:has-text("Collected")').count());

View file

@ -9,8 +9,9 @@ export const cfg = {
debug_network: process.env.DEBUG_NETWORK == '1', // log network requests and responses debug_network: process.env.DEBUG_NETWORK == '1', // log network requests and responses
record: process.env.RECORD == '1', // `recordHar` (network) + `recordVideo` record: process.env.RECORD == '1', // `recordHar` (network) + `recordVideo`
time: process.env.TIME == '1', // log duration of each step time: process.env.TIME == '1', // log duration of each step
interactive: process.env.INTERACTIVE == '1', // confirm to claim, enter to skip
dryrun: process.env.DRYRUN == '1', // don't claim anything dryrun: process.env.DRYRUN == '1', // don't claim anything
interactive: process.env.INTERACTIVE == '1', // confirm to claim, default skip nowait: process.env.NOWAIT == '1', // fail fast instead of waiting for user input
show: process.env.SHOW == '1', // run non-headless show: process.env.SHOW == '1', // run non-headless
get headless() { get headless() {
return !this.debug && !this.show; return !this.debug && !this.show;
@ -33,6 +34,7 @@ export const cfg = {
eg_password: process.env.EG_PASSWORD || process.env.PASSWORD, eg_password: process.env.EG_PASSWORD || process.env.PASSWORD,
eg_otpkey: process.env.EG_OTPKEY, eg_otpkey: process.env.EG_OTPKEY,
eg_parentalpin: process.env.EG_PARENTALPIN, eg_parentalpin: process.env.EG_PARENTALPIN,
eg_mobile: process.env.EG_MOBILE != '0', // claim mobile games
// auth prime-gaming // auth prime-gaming
pg_email: process.env.PG_EMAIL || process.env.EMAIL, pg_email: process.env.PG_EMAIL || process.env.EMAIL,
pg_password: process.env.PG_PASSWORD || process.env.PASSWORD, pg_password: process.env.PG_PASSWORD || process.env.PASSWORD,
@ -49,5 +51,5 @@ export const cfg = {
pg_redeem: process.env.PG_REDEEM == '1', // prime-gaming: redeem keys on external stores pg_redeem: process.env.PG_REDEEM == '1', // prime-gaming: redeem keys on external stores
lg_email: process.env.LG_EMAIL || process.env.PG_EMAIL || process.env.EMAIL, // prime-gaming: external: legacy-games: email to use for redeeming lg_email: process.env.LG_EMAIL || process.env.PG_EMAIL || process.env.EMAIL, // prime-gaming: external: legacy-games: email to use for redeeming
pg_claimdlc: process.env.PG_CLAIMDLC == '1', // prime-gaming: claim in-game content pg_claimdlc: process.env.PG_CLAIMDLC == '1', // prime-gaming: claim in-game content
pg_timeLeft: process.env.PG_TIMELEFT == '1', // prime-gaming: list time left to claim pg_timeLeft: Number(process.env.PG_TIMELEFT), // prime-gaming: check time left to claim and skip game if there are more than PG_TIMELEFT days left to claim it
}; };

51
src/epic-games-mobile.js Normal file
View file

@ -0,0 +1,51 @@
// following https://github.com/vogler/free-games-claimer/issues/474
const get = async (platform = 'android') => { // or ios
const r = await fetch(`https://egs-platform-service.store.epicgames.com/api/v2/public/discover/home?count=10&country=DE&locale=en&platform=${platform}&start=0&store=EGS`);
return await r.json();
};
// $ jq '.data[].topicId' -r
// $ jq '.data[] | {topicId,type} | flatten | @tsv' -r
// mobile-android-carousel featured
// mobile-android-featured-breaker featured
// mobile-android-genre-must-play interactiveIconList
// mobile-android-1pp featured
// mobile-android-fn-exp imageOnly
// android-mega-sale interactiveIconList
// mobile-android-free-game freeGame
// mobile-android-genre-action featured
// mobile-android-genre-free interactiveIconList
// mobile-android-genre-paid interactiveIconList
// $ jq '.data[].offers[].content | {slug: .mapping.slug, price: (.purchase[] | {decimal: .price.decimalPrice, type: .purchaseType})}'
// {
// "slug": "dc-heroes-united-android-de4bc2",
// "price": {
// "decimal": 0,
// "type": "Claim"
// }
// }
// {
// "slug": "ashworld-android-abd8de",
// "price": {
// "decimal": 4.79,
// "type": "Purchase"
// }
// }
const url = s => `https://store.epicgames.com/en-US/p/${s}`;
export const getPlatformGames = async platform => {
const json = await get(platform);
const free_game = json.data.filter(x => x.type == 'freeGame')[0];
// console.log(free_game);
return free_game.offers.map(offer => {
const c = offer.content;
// console.log(c.purchase)
return { title: c.title, url: url(c.mapping.slug) };
});
};
export const getGames = async () => [...await getPlatformGames('android'), ...await getPlatformGames('ios')];
// console.log(await getGames());

View file

@ -42,6 +42,11 @@ const gh = await (await fetch('https://api.github.com/repos/vogler/free-games-cl
log('Local commit:', sha, new Date(date)); log('Local commit:', sha, new Date(date));
log('Online commit:', gh.sha, new Date(gh.commit.committer.date)); log('Online commit:', gh.sha, new Date(gh.commit.committer.date));
// git describe --all --long --dirty
// --> heads/main-0-gdee47d2-dirty
// git describe --tags --long --dirty
// --> v1.7-35-gdee47d2-dirty
if (sha == gh.sha) { if (sha == gh.sha) {
log('Running the latest version!'); log('Running the latest version!');
} else { } else {

61
steam-games.js Normal file
View file

@ -0,0 +1,61 @@
// import { firefox } from 'playwright-firefox';
import { chromium } from 'patchright';
import { datetime, filenamify, jsonDb, prompt } from './src/util.js';
import { cfg } from './src/config.js';
const db = await jsonDb('steam-games.json', {});
const user = cfg.steam_id || await prompt({ message: 'Enter Steam community id ("View my profile", then copy from URL)' });
const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless,
// viewport: { width: cfg.width, height: cfg.height },
locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/steam-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// https://peter.sh/experiments/chromium-command-line-switches/
args: [
'--hide-crash-restore-bubble',
],
});
context.setDefaultTimeout(cfg.debug ? 0 : cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
try {
await page.goto(`https://steamcommunity.com/id/${user}/games?tab=all`);
const games = page.locator('div[data-featuretarget="gameslist-root"] > div.Panel > div.Panel > div');
await games.last().waitFor();
await page.keyboard.press('End');
await page.waitForLoadState('networkidle');
console.log('All Games:', await games.count());
for (const game of await games.all()) {
const title = await game.locator('span a').innerText();
let time, last, achievements, size;
const ltime = game.locator('span:has-text("total played")');
if (await ltime.count()) time = (await ltime.first().innerText()).split('\n')[1];
const llast = game.locator('span:has-text("last played")');
if (await llast.count()) last = (await llast.first().innerText()).split('\n')[1];
const lachievements = game.locator('a:has-text("achievements") + span');
if (await lachievements.count()) achievements = (await lachievements.first().innerText()).split('\n');
const lsize = game.locator('span:has(+ button)');
if (await lsize.count()) size = await lsize.first().innerText();
const url = await game.locator('a').first().getAttribute('href');
const img = await game.locator('img').first().getAttribute('src');
const stat = { title, time, last, achievements, size, url, img };
console.log(stat);
db.data[title] = stat;
}
// await page.pause();
} catch (error) {
process.exitCode ||= 1;
console.error('--- Exception:');
console.error(error); // .toString()?
} finally {
await db.write(); // write out json db
}
if (page.video()) console.log('Recorded video:', await page.video().path());
await context.close();

View file

@ -0,0 +1,21 @@
// open issue: prevents handleSIGINT() to work if prompt is cancelled with Ctrl-C instead of Escape: https://github.com/enquirer/enquirer/issues/372
function onRawSIGINT(fn) {
const { stdin, stdout } = process;
stdin.setRawMode(true);
stdin.resume();
stdin.on('data', data => {
const key = data.toString('utf-8');
if (key === '\u0003') { // ctrl + c
fn();
} else {
stdout.write(key);
}
});
}
console.log(1);
onRawSIGINT(() => {
console.log('raw'); process.exit(1);
});
console.log(2);
// onRawSIGINT workaround for enquirer keeps the process from exiting here...

View file

@ -1,34 +1,35 @@
// https://github.com/enquirer/enquirer/issues/372 // https://github.com/enquirer/enquirer/issues/372
import { prompt } from '../src/util.js'; import { prompt, handleSIGINT } from '../src/util.js';
const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C // const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
console.log('\nInterrupted by SIGINT. Exit!'); // console.log('\nInterrupted by SIGINT. Exit!');
process.exitCode = 130; // process.exitCode = 130;
}); // });
handleSIGINT(); handleSIGINT();
function onRawSIGINT(fn) { // function onRawSIGINT(fn) {
const { stdin, stdout } = process; // const { stdin, stdout } = process;
stdin.setRawMode(true); // stdin.setRawMode(true);
stdin.resume(); // stdin.resume();
stdin.on('data', data => { // stdin.on('data', data => {
const key = data.toString('utf-8'); // const key = data.toString('utf-8');
if (key === '\u0003') { // ctrl + c // if (key === '\u0003') { // ctrl + c
fn(); // fn();
} else { // } else {
stdout.write(key); // stdout.write(key);
} // }
}); // });
} // }
onRawSIGINT(() => { // onRawSIGINT(() => {
console.log('raw'); process.exit(1); // console.log('raw'); process.exit(1);
}); // });
console.log('hello'); console.log('hello');
console.error('hello error'); console.error('hello error');
try { try {
let i = 'foo'; let i = 'foo';
i = await prompt(); // SIGINT no longer handled if this is executed i = await prompt(); // SIGINT no longer handled if this is executed
i = await prompt(); // SIGINT no longer handled if this is executed
// handleSIGINT(); // handleSIGINT();
console.log('value:', i); console.log('value:', i);
setTimeout(() => console.log('timeout 3s'), 3000); setTimeout(() => console.log('timeout 3s'), 3000);

View file

@ -13,3 +13,8 @@ await enquirer.prompt({
name: 'username', name: 'username',
message: 'What is your username?', message: 'What is your username?',
}); });
await enquirer.prompt({
type: 'input',
name: 'username',
message: 'What is your username 2?',
});

29
test/webgl.js Normal file
View file

@ -0,0 +1,29 @@
import { chromium } from 'patchright';
import { handleSIGINT } from '../src/util.js';
import { cfg } from '../src/config.js';
const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: false, // don't use cfg.headless headless here since SHOW=0 will lead to captcha
locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// https://peter.sh/experiments/chromium-command-line-switches/
args: [
'--hide-crash-restore-bubble',
'--ignore-gpu-blocklist', // required for OpenGL: Disabled -> Enabled & WebGL: Software only -> Hardware accelerated
'--enable-unsafe-webgpu', // required for WebGPU: Disabled -> Hardware accelerated
],
});
handleSIGINT(context);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist
await page.goto('https://get.webgl.org/');
console.log(await page.locator('h1').innerText());
await page.goto('https://webglreport.com/?v=2');
console.log(await page.locator('tr:has-text("Unmasked Renderer")').innerText());
console.log('Waiting. You can check chrome://gpu as well via noVNC. Press ctrl-c to quit...');
// without --ignore-gpu-blocklist: OpenGL Disabled, WebGL: Software only, hardware acceleration unavailable.
// Unmasked Renderer: ANGLE (Mesa, llvmpipe (LLVM 15.0.7 128 bits), OpenGL 4.5)
// with --ignore-gpu-blocklist: OpenGL Enabled, WebGL: Hardware accelerated
// Unmasked Renderer: ANGLE (Google, Vulkan 1.3.0 (SwiftShader Device (LLVM 10.0.0) (0x0000C0DE)), SwiftShader driver)

View file

@ -1,11 +1,12 @@
// TODO This is mostly a copy of epic-games.js // TODO This is mostly a copy of epic-games.js
// New assets to claim every first Tuesday of a month. // New assets to claim every first Tuesday of a month.
import { firefox } from 'playwright-firefox'; // stealth plugin needs no outdated playwright-extra // import { firefox } from 'playwright-firefox';
import { chromium } from 'patchright';
import { authenticator } from 'otplib'; import { authenticator } from 'otplib';
import path from 'path'; import path from 'path';
import { writeFileSync } from 'fs'; import { writeFileSync } from 'fs';
import { resolve, jsonDb, datetime, stealth, filenamify, prompt, notify, html_game_list, handleSIGINT } from './src/util.js'; import { resolve, jsonDb, datetime, filenamify, prompt, notify, html_game_list, handleSIGINT } from './src/util.js';
import { cfg } from './src/config.js'; import { cfg } from './src/config.js';
const screenshot = (...a) => resolve(cfg.dir.screenshots, 'unrealengine', ...a); const screenshot = (...a) => resolve(cfg.dir.screenshots, 'unrealengine', ...a);
@ -18,21 +19,21 @@ console.log(datetime(), 'started checking unrealengine');
const db = await jsonDb('unrealengine.json', {}); const db = await jsonDb('unrealengine.json', {});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await chromium.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
viewport: { width: cfg.width, height: cfg.height }, viewport: { width: cfg.width, height: cfg.height },
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36', // see replace of Headless in util.newStealthContext. TODO Windows UA enough to avoid 'device not supported'? update if browser is updated? locale: 'en-US', // ignore OS locale to be sure to have english text for locators -> done via /en in URL
// userAgent for firefox: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0
locale: 'en-US', // ignore OS locale to be sure to have english text for locators
recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800 recordVideo: cfg.record ? { dir: 'data/record/', size: { width: cfg.width, height: cfg.height } } : undefined, // will record a .webm video for each page navigated; without size, video would be scaled down to fit 800x800
recordHar: cfg.record ? { path: `data/record/ue-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools recordHar: cfg.record ? { path: `data/record/gog-${filenamify(datetime())}.har` } : undefined, // will record a HAR file with network requests and responses; can be imported in Chrome devtools
handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved handleSIGINT: false, // have to handle ourselves and call context.close(), otherwise recordings from above won't be saved
// https://peter.sh/experiments/chromium-command-line-switches/
args: [
'--hide-crash-restore-bubble',
],
}); });
handleSIGINT(context); handleSIGINT(context);
await stealth(context);
if (!cfg.debug) context.setDefaultTimeout(cfg.timeout); if (!cfg.debug) context.setDefaultTimeout(cfg.timeout);
const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist const page = context.pages().length ? context.pages()[0] : await context.newPage(); // should always exist