[fix] Follow script redirect to get the JSON (#981)

This commit is contained in:
Zagrios
2026-01-02 16:12:47 +01:00
committed by GitHub
parent 92a1232f85
commit 378fa1150e
3 changed files with 95 additions and 19 deletions
+51 -13
View File
@@ -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": {
+3 -2
View File
@@ -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"
+41 -4
View File
@@ -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<T>(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
};
}