mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 23:29:35 +02:00
using math.random instead of webcrypto and reintroduced randomizeUserAgent
This commit is contained in:
parent
c7c5bf633c
commit
cc8459c161
1 changed files with 21 additions and 9 deletions
|
@ -3,13 +3,23 @@ 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 randomBytesHex = (nBytes = 8) => {
|
const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16)
|
||||||
const array = new Uint8Array(nBytes);
|
.toString(16))
|
||||||
crypto.getRandomValues(array);
|
|
||||||
return Array.from(array)
|
|
||||||
.map((byte) => byte.toString(16)
|
|
||||||
.padStart(2, '0'))
|
|
||||||
.join('');
|
.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 = (_) => {
|
const checkIfResponseIsOk = (_) => {
|
||||||
|
@ -65,7 +75,9 @@ 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': userAgent,
|
'user-agent': profile.randomizeUserAgent
|
||||||
|
? randomizeUserAgent(userAgent)
|
||||||
|
: userAgent,
|
||||||
...reqData.headers,
|
...reqData.headers,
|
||||||
},
|
},
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
|
@ -76,7 +88,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 = randomBytesHex(3);
|
const reqId = randomBytesHexString(6);
|
||||||
const fetchReq = new Request(url, reqOptions);
|
const fetchReq = new Request(url, reqOptions);
|
||||||
profile.logRequest(ctx, fetchReq, reqId);
|
profile.logRequest(ctx, fetchReq, reqId);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue