From 378fa1150e10ec355bfd4a7dc4b0eb19ce6929a2 Mon Sep 17 00:00:00 2001 From: Zagrios <40181755+Zagrios@users.noreply.github.com> Date: Fri, 2 Jan 2026 16:12:47 +0100 Subject: [PATCH] [fix] Follow script redirect to get the JSON (#981) --- package-lock.json | 64 ++++++++++++++++++++++------ package.json | 5 ++- src/main/services/request.service.ts | 45 +++++++++++++++++-- 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5c5623eb..e6a62134 100644 --- a/package-lock.json +++ b/package-lock.json @@ -60,6 +60,7 @@ "tailwindcss-scoped-groups": "^2.0.0", "tippy.js": "^6.3.7", "to-ico": "^1.1.5", + "tough-cookie": "^6.0.0", "use-double-click": "^1.0.5", "use-fit-text": "^2.4.0", "yauzl": "^3.2.0" @@ -21894,10 +21895,16 @@ } }, "node_modules/psl": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", - "integrity": "sha512-E/ZsdU4HLs/68gYzgGTkMicWTLPdAftJLfJFlLUAAKZGkStNU72sZjT66SnMDVOfOWY/YAoiD7Jxa9iHvngcag==", - "license": "MIT" + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.15.0.tgz", + "integrity": "sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "funding": { + "url": "https://github.com/sponsors/lupomontero" + } }, "node_modules/pump": { "version": "3.0.0", @@ -21911,9 +21918,9 @@ } }, "node_modules/punycode": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", "license": "MIT", "engines": { "node": ">=6" @@ -22759,6 +22766,19 @@ "node": ">=0.6" } }, + "node_modules/request/node_modules/tough-cookie": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz", + "integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==", + "license": "BSD-3-Clause", + "dependencies": { + "psl": "^1.1.28", + "punycode": "^2.1.1" + }, + "engines": { + "node": ">=0.8" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -24839,6 +24859,24 @@ "@popperjs/core": "^2.9.0" } }, + "node_modules/tldts": { + "version": "7.0.19", + "resolved": "https://registry.npmjs.org/tldts/-/tldts-7.0.19.tgz", + "integrity": "sha512-8PWx8tvC4jDB39BQw1m4x8y5MH1BcQ5xHeL2n7UVFulMPH/3Q0uiamahFJ3lXA0zO2SUyRXuVVbWSDmstlt9YA==", + "license": "MIT", + "dependencies": { + "tldts-core": "^7.0.19" + }, + "bin": { + "tldts": "bin/cli.js" + } + }, + "node_modules/tldts-core": { + "version": "7.0.19", + "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-7.0.19.tgz", + "integrity": "sha512-lJX2dEWx0SGH4O6p+7FPwYmJ/bu1JbcGJ8RLaG9b7liIgZ85itUVEPbMtWRVrde/0fnDPEPHW10ZsKW3kVsE9A==", + "license": "MIT" + }, "node_modules/tmp": { "version": "0.2.5", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.5.tgz", @@ -24925,15 +24963,15 @@ } }, "node_modules/tough-cookie": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.5.0.tgz", - "integrity": "sha512-nlLsUzgm1kfLXSXfRZMc1KLAugd4hqJHDTvc2hDIwS3mZAfMEuMbc03SujMF+GEcpaX/qboeycw6iO8JwVv2+g==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-6.0.0.tgz", + "integrity": "sha512-kXuRi1mtaKMrsLUxz3sQYvVl37B0Ns6MzfrtV5DvJceE9bPyspOqk9xxv7XbZWcfLWbFmm997vl83qUWVJA64w==", + "license": "BSD-3-Clause", "dependencies": { - "psl": "^1.1.28", - "punycode": "^2.1.1" + "tldts": "^7.0.5" }, "engines": { - "node": ">=0.8" + "node": ">=16" } }, "node_modules/tree-kill": { diff --git a/package.json b/package.json index ae13385c..e2f4aaa7 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,6 @@ "homepage": "https://github.com/Zagrios/bs-manager#readme", "devDependencies": { "@electron/fuses": "^1.7.0", - "ts-node": "^10.9.2", - "tsx": "^4.19.2", "@electron/notarize": "^2.3.0", "@electron/rebuild": "^3.6.0", "@pmmmwh/react-refresh-webpack-plugin": "0.5.5", @@ -147,6 +145,8 @@ "terser-webpack-plugin": "^5.3.10", "ts-jest": "^29.1.2", "ts-loader": "^9.5.1", + "ts-node": "^10.9.2", + "tsx": "^4.19.2", "typescript": "^5.3.3", "url-loader": "^4.1.1", "webpack": "^5.90.3", @@ -206,6 +206,7 @@ "tailwindcss-scoped-groups": "^2.0.0", "tippy.js": "^6.3.7", "to-ico": "^1.1.5", + "tough-cookie": "^6.0.0", "use-double-click": "^1.0.5", "use-fit-text": "^2.4.0", "yauzl": "^3.2.0" diff --git a/src/main/services/request.service.ts b/src/main/services/request.service.ts index 863c1bd9..a98a2301 100644 --- a/src/main/services/request.service.ts +++ b/src/main/services/request.service.ts @@ -11,6 +11,7 @@ import { pipeline } from 'stream/promises'; import sanitize from 'sanitize-filename'; import internal from 'stream'; import { app } from 'electron'; +import { CookieJar } from 'tough-cookie'; export class RequestService { private static instance: RequestService; @@ -57,15 +58,51 @@ export class RequestService { } private async requestData(url: string, family: number): Promise<{ data: T; headers: IncomingHttpHeaders }> { - const response = await got(url, { + + const cookieJar = new CookieJar(); + + const first = await got(url, { // @ts-ignore (ESM is not well supported in this project, We need to move out electron-react-boilerplate, and use Vite) dnsLookupIpVersion: family, - responseType: "json", + cookieJar, headers: this.baseHeaders }); + + // Follow script redirect to get the JSON + if (first.headers['content-type']?.includes('text/html')) { + + const cookieMatch = first.body.match(/document\.cookie="([^"]+)"/); + if (!cookieMatch) { + throw new Error("Cookie not found in JS"); + } + + const cookieString = cookieMatch[1]; + + const redirectMatch = first.body.match(/location\.href="([^"]+)"/); + if (!redirectMatch) { + throw new Error("Redirect URL not found"); + } + + const jsonUrl = redirectMatch[1]; + await cookieJar.setCookie(cookieString, jsonUrl); + + const second = await got(jsonUrl, { + // @ts-ignore (ESM is not well supported in this project, We need to move out electron-react-boilerplate, and use Vite) + dnsLookupIpVersion: family, + responseType: "json", + cookieJar, + headers: this.baseHeaders + }); + + return { + data: second.body as T, + headers: second.headers + }; + } + return { - data: response.body as T, - headers: response.headers + data: JSON.parse(first.body) as T, + headers: first.headers }; }