From bd3ffa3d3024b7b79e4fee2ca98338f50e60b74a Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:34:35 -0400 Subject: [PATCH 1/5] feat: Dockerize server --- .dockerignore | 5 +++++ Dockerfile | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..665b496 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.git +.env +node_modules +dist +logs diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..53811ab --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# syntax=docker/dockerfile:1 + +ARG app_dir="/home/node/app" + + +# * Base Node.js image +FROM node:20-alpine AS base +ARG app_dir +WORKDIR ${app_dir} + + +# * Installing production dependencies +FROM base AS dependencies + +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=package-lock.json,target=package-lock.json \ + --mount=type=cache,target=/root/.npm \ + npm ci --omit=dev + + +# * Installing development dependencies and building the application +FROM base AS build + +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=package-lock.json,target=package-lock.json \ + --mount=type=cache,target=/root/.npm \ + npm ci + +COPY . . +RUN npm run build + + +# * Running the final application +FROM base AS final +ARG app_dir + +RUN mkdir -p ${app_dir}/logs && chown node:node ${app_dir}/logs + +ENV NODE_ENV=production +USER node + +COPY package.json . + +COPY --from=dependencies ${app_dir}/node_modules ${app_dir}/node_modules +COPY --from=build ${app_dir}/dist ${app_dir}/dist + +CMD ["node", "."] From 31c3323d52f402fb1af3e8e1333f2169856d6c60 Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:35:08 -0400 Subject: [PATCH 2/5] feat: create Docker build and publish action --- .github/workflows/docker.yml | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..9e1ef9c --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,47 @@ +name: Build and Publish Docker Image + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + build-publish: + env: + SHOULD_PUSH_IMAGE: ${{ (github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')) || github.event_name == 'workflow_dispatch' }} + runs-on: ubuntu-latest + + steps: + - name: Set up QEMU for Docker + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log into the Docker container registry + if: ${{ env.SHOULD_PUSH_IMAGE == 'true' }} + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ github.repository }} + tags: | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }} + type=raw,value=edge,enable=${{ github.ref == 'refs/heads/dev' }} + type=sha + + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@v6 + with: + platforms: linux/amd64,linux/arm64 + push: ${{ env.SHOULD_PUSH_IMAGE }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max From 0b376788bcb52295590bed86a00f4c41bfe5e08d Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:39:28 -0400 Subject: [PATCH 3/5] fix: make fs-extra a production dependency --- package-lock.json | 30 +++++++++++------------------- package.json | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index f72209d..00d6594 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "dotenv": "^10.0.0", "express": "^4.17.1", "express-subdomain": "^1.0.5", + "fs-extra": "^11.2.0", "moment": "^2.29.4", "mongoose": "^7.4.3", "morgan": "^1.10.0", @@ -36,7 +37,6 @@ "@typescript-eslint/parser": "^6.4.0", "axios": "^1.6.2", "eslint": "^8.47.0", - "fs-extra": "^10.1.0", "tsc-alias": "^1.8.7", "typescript": "^5.1.6", "xmlbuilder2": "^3.0.2" @@ -3458,17 +3458,16 @@ } }, "node_modules/fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", - "dev": true, + "version": "11.2.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.2.0.tgz", + "integrity": "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==", "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" }, "engines": { - "node": ">=12" + "node": ">=14.14" } }, "node_modules/fs-minipass": { @@ -3599,8 +3598,7 @@ "node_modules/graceful-fs": { "version": "4.2.8", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.8.tgz", - "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==", - "dev": true + "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==" }, "node_modules/graphemer": { "version": "1.4.0", @@ -3855,7 +3853,6 @@ "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, "dependencies": { "universalify": "^2.0.0" }, @@ -5211,7 +5208,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true, "engines": { "node": ">= 10.0.0" } @@ -8071,10 +8067,9 @@ "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==" }, "fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", - "dev": true, + "version": "11.2.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.2.0.tgz", + "integrity": "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==", "requires": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", @@ -8172,8 +8167,7 @@ "graceful-fs": { "version": "4.2.8", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.8.tgz", - "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==", - "dev": true + "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==" }, "graphemer": { "version": "1.4.0", @@ -8357,7 +8351,6 @@ "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, "requires": { "graceful-fs": "^4.1.6", "universalify": "^2.0.0" @@ -9317,8 +9310,7 @@ "universalify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==" }, "unpipe": { "version": "1.0.0", diff --git a/package.json b/package.json index 0e95ff9..24a694b 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dotenv": "^10.0.0", "express": "^4.17.1", "express-subdomain": "^1.0.5", + "fs-extra": "^11.2.0", "moment": "^2.29.4", "mongoose": "^7.4.3", "morgan": "^1.10.0", @@ -40,7 +41,6 @@ "@typescript-eslint/parser": "^6.4.0", "axios": "^1.6.2", "eslint": "^8.47.0", - "fs-extra": "^10.1.0", "tsc-alias": "^1.8.7", "typescript": "^5.1.6", "xmlbuilder2": "^3.0.2" From e598224c6c4a07cf79b6af9468fad063ebc02d34 Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:39:40 -0400 Subject: [PATCH 4/5] fix: exit server on SIGTERM --- src/server.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/server.ts b/src/server.ts index 5db5542..f4c1d47 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,4 +1,7 @@ process.title = 'Pretendo - BOSS'; +process.on('SIGTERM', () => { + process.exit(0); +}); import express from 'express'; import morgan from 'morgan'; From 7f626abd7b377f185a420095e66409653ce15c02 Mon Sep 17 00:00:00 2001 From: Matthew Lopez <73856503+MatthewL246@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:45:53 -0400 Subject: [PATCH 5/5] chore: allow server to start without BOSS keys These keys are not required for serving BOSS content, only for uploading and encrypting new files. --- src/config-manager.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config-manager.ts b/src/config-manager.ts index 0ee95e0..6b6737c 100644 --- a/src/config-manager.ts +++ b/src/config-manager.ts @@ -88,15 +88,15 @@ if (!config.http.port) { } if (md5(config.crypto.wup.aes_key) !== BOSS_WIIU_AES_KEY_MD5_HASH) { - errors.push('Invalid BOSS WiiU AES key. Set or correct the PN_BOSS_CONFIG_BOSS_WIIU_AES_KEY environment variable'); + warnings.push('Invalid BOSS WiiU AES key. Uploading and encrypting new BOSS content for the Wii U won\'t work! Set or correct the PN_BOSS_CONFIG_BOSS_WIIU_AES_KEY environment variable'); } if (md5(config.crypto.wup.hmac_key) !== BOSS_WIIU_HMAC_KEY_MD5_HASH) { - errors.push('Invalid BOSS WiiU HMAC key. Set or correct the PN_BOSS_CONFIG_BOSS_WIIU_HMAC_KEY environment variable'); + warnings.push('Invalid BOSS WiiU HMAC key. Uploading and encrypting new BOSS content for the Wii U won\'t work! Set or correct the PN_BOSS_CONFIG_BOSS_WIIU_HMAC_KEY environment variable'); } if (md5(config.crypto.ctr.aes_key) !== BOSS_3DS_AES_KEY_MD5_HASH) { - errors.push('Invalid BOSS 3DS AES key. Set or correct the PN_BOSS_CONFIG_BOSS_3DS_AES_KEY environment variable'); + warnings.push('Invalid BOSS 3DS AES key. Uploading and encrypting new BOSS content for the 3DS won\'t work! Set or correct the PN_BOSS_CONFIG_BOSS_3DS_AES_KEY environment variable'); } if (!config.grpc.boss.address) {