Compare commits

..

1 commit

Author SHA1 Message Date
McToel
5244cf5401
Merge 3213dc4343 into 6d1d0c626f 2025-02-20 13:31:28 +00:00

View file

@ -3,23 +3,13 @@ import {Request, fetch} from 'cross-fetch';
import {parse as parseContentType} from 'content-type'; import {parse as parseContentType} from 'content-type';
import {HafasError} from './errors.js'; import {HafasError} from './errors.js';
const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16) const randomBytesHex = (nBytes = 8) => {
.toString(16)) const array = new Uint8Array(nBytes);
.join(''); crypto.getRandomValues(array);
return Array.from(array)
const id = randomBytesHexString(6) .map((byte) => byte.toString(16)
.toString('hex'); .padStart(2, '0'))
const randomizeUserAgent = (userAgent) => { .join('');
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 = (_) => { const checkIfResponseIsOk = (_) => {
@ -75,9 +65,7 @@ const request = async (ctx, userAgent, reqData) => {
'Accept-Encoding': 'gzip, br, deflate', 'Accept-Encoding': 'gzip, br, deflate',
'Accept': 'application/json', 'Accept': 'application/json',
'Accept-Language': opt.language || profile.defaultLanguage || 'en', 'Accept-Language': opt.language || profile.defaultLanguage || 'en',
'user-agent': profile.randomizeUserAgent 'user-agent': userAgent,
? randomizeUserAgent(userAgent)
: userAgent,
...reqData.headers, ...reqData.headers,
}, },
redirect: 'follow', redirect: 'follow',
@ -88,7 +76,7 @@ const request = async (ctx, userAgent, reqData) => {
if (reqOptions.query) { if (reqOptions.query) {
url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true});
} }
const reqId = randomBytesHexString(6); const reqId = randomBytesHex(3);
const fetchReq = new Request(url, reqOptions); const fetchReq = new Request(url, reqOptions);
profile.logRequest(ctx, fetchReq, reqId); profile.logRequest(ctx, fetchReq, reqId);