From cc8459c161a1fff47ab0ecef330201c83dba1f5f Mon Sep 17 00:00:00 2001 From: McToel Date: Thu, 20 Feb 2025 23:45:44 +0100 Subject: [PATCH] using math.random instead of webcrypto and reintroduced randomizeUserAgent --- lib/request.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/request.js b/lib/request.js index db88b76b..f6643a7a 100644 --- a/lib/request.js +++ b/lib/request.js @@ -3,13 +3,23 @@ import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; import {HafasError} from './errors.js'; -const randomBytesHex = (nBytes = 8) => { - const array = new Uint8Array(nBytes); - crypto.getRandomValues(array); - return Array.from(array) - .map((byte) => byte.toString(16) - .padStart(2, '0')) - .join(''); +const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16) + .toString(16)) + .join(''); + +const id = randomBytesHexString(6) + .toString('hex'); +const randomizeUserAgent = (userAgent) => { + let ua = userAgent; + for ( + let i = Math.round(5 + Math.random() * 5); + i < ua.length; + i += Math.round(5 + Math.random() * 5) + ) { + ua = ua.slice(0, i) + id + ua.slice(i); + i += id.length; + } + return ua; }; const checkIfResponseIsOk = (_) => { @@ -65,7 +75,9 @@ const request = async (ctx, userAgent, reqData) => { 'Accept-Encoding': 'gzip, br, deflate', 'Accept': 'application/json', 'Accept-Language': opt.language || profile.defaultLanguage || 'en', - 'user-agent': userAgent, + 'user-agent': profile.randomizeUserAgent + ? randomizeUserAgent(userAgent) + : userAgent, ...reqData.headers, }, redirect: 'follow', @@ -76,7 +88,7 @@ const request = async (ctx, userAgent, reqData) => { if (reqOptions.query) { url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); } - const reqId = randomBytesHex(3); + const reqId = randomBytesHexString(6); const fetchReq = new Request(url, reqOptions); profile.logRequest(ctx, fetchReq, reqId);