mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 23:29:35 +02:00
Removed Proxy and local address code
This commit is contained in:
parent
9314e59053
commit
f7909aac29
2 changed files with 57 additions and 61 deletions
117
lib/request.js
117
lib/request.js
|
@ -1,62 +1,62 @@
|
|||
import ProxyAgent from 'https-proxy-agent';
|
||||
import {isIP} from 'net';
|
||||
import {Agent as HttpsAgent} from 'https';
|
||||
import roundRobin from '@derhuerst/round-robin-scheduler';
|
||||
// import ProxyAgent from 'https-proxy-agent';
|
||||
// import {isIP} from 'net';
|
||||
// import {Agent as HttpsAgent} from 'https';
|
||||
// import roundRobin from '@derhuerst/round-robin-scheduler';
|
||||
import {randomBytes} from 'crypto';
|
||||
import {stringify} from 'qs';
|
||||
import {Request, fetch} from 'cross-fetch';
|
||||
import {parse as parseContentType} from 'content-type';
|
||||
import {HafasError} from './errors.js';
|
||||
|
||||
const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null;
|
||||
const localAddresses = process.env.LOCAL_ADDRESS || null;
|
||||
// const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null;
|
||||
// const localAddresses = process.env.LOCAL_ADDRESS || null;
|
||||
|
||||
if (proxyAddress && localAddresses) {
|
||||
console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.');
|
||||
process.exit(1);
|
||||
}
|
||||
// if (proxyAddress && localAddresses) {
|
||||
// console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.');
|
||||
// process.exit(1);
|
||||
// }
|
||||
|
||||
const plainAgent = new HttpsAgent({
|
||||
keepAlive: true,
|
||||
});
|
||||
let getAgent = () => plainAgent;
|
||||
// const plainAgent = new HttpsAgent({
|
||||
// keepAlive: true,
|
||||
// });
|
||||
// let getAgent = () => plainAgent;
|
||||
|
||||
if (proxyAddress) {
|
||||
const agent = new ProxyAgent(proxyAddress, {
|
||||
keepAlive: true,
|
||||
keepAliveMsecs: 10 * 1000, // 10s
|
||||
});
|
||||
getAgent = () => agent;
|
||||
} else if (localAddresses) {
|
||||
const agents = process.env.LOCAL_ADDRESS.split(',')
|
||||
.map((addr) => {
|
||||
const family = isIP(addr);
|
||||
if (family === 0) {
|
||||
throw new Error('invalid local address:' + addr);
|
||||
}
|
||||
return new HttpsAgent({
|
||||
localAddress: addr, family,
|
||||
keepAlive: true,
|
||||
});
|
||||
});
|
||||
const pool = roundRobin(agents);
|
||||
getAgent = () => pool.get();
|
||||
}
|
||||
// if (proxyAddress) {
|
||||
// const agent = new ProxyAgent(proxyAddress, {
|
||||
// keepAlive: true,
|
||||
// keepAliveMsecs: 10 * 1000, // 10s
|
||||
// });
|
||||
// getAgent = () => agent;
|
||||
// } else if (localAddresses) {
|
||||
// const agents = process.env.LOCAL_ADDRESS.split(',')
|
||||
// .map((addr) => {
|
||||
// const family = isIP(addr);
|
||||
// if (family === 0) {
|
||||
// throw new Error('invalid local address:' + addr);
|
||||
// }
|
||||
// return new HttpsAgent({
|
||||
// localAddress: addr, family,
|
||||
// keepAlive: true,
|
||||
// });
|
||||
// });
|
||||
// const pool = roundRobin(agents);
|
||||
// getAgent = () => pool.get();
|
||||
// }
|
||||
|
||||
const id = randomBytes(3)
|
||||
.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 id = randomBytes(3)
|
||||
// .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 {
|
||||
|
@ -101,8 +101,8 @@ const request = async (ctx, userAgent, reqData) => {
|
|||
delete reqData.endpoint;
|
||||
const rawReqBody = profile.transformReqBody(ctx, reqData.body);
|
||||
|
||||
const req = profile.transformReq(ctx, {
|
||||
agent: getAgent(),
|
||||
const reqOptions = profile.transformReq(ctx, {
|
||||
keepalive: true,
|
||||
method: reqData.method,
|
||||
// todo: CORS? referrer policy?
|
||||
body: JSON.stringify(rawReqBody),
|
||||
|
@ -111,10 +111,7 @@ const request = async (ctx, userAgent, reqData) => {
|
|||
'Accept-Encoding': 'gzip, br, deflate',
|
||||
'Accept': 'application/json',
|
||||
'Accept-Language': opt.language || profile.defaultLanguage || 'en',
|
||||
'user-agent': profile.randomizeUserAgent
|
||||
? randomizeUserAgent(userAgent)
|
||||
: userAgent,
|
||||
'connection': 'keep-alive', // prevent excessive re-connecting
|
||||
'user-agent': userAgent,
|
||||
...reqData.headers,
|
||||
},
|
||||
redirect: 'follow',
|
||||
|
@ -122,15 +119,15 @@ const request = async (ctx, userAgent, reqData) => {
|
|||
});
|
||||
|
||||
let url = endpoint + (reqData.path || '');
|
||||
if (req.query) {
|
||||
url += '?' + stringify(req.query, {arrayFormat: 'brackets', encodeValuesOnly: true});
|
||||
if (reqOptions.query) {
|
||||
url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true});
|
||||
}
|
||||
const reqId = randomBytes(3)
|
||||
.toString('hex');
|
||||
const fetchReq = new Request(url, req);
|
||||
const fetchReq = new Request(url, reqOptions);
|
||||
profile.logRequest(ctx, fetchReq, reqId);
|
||||
|
||||
const res = await fetch(url, req);
|
||||
const res = await fetch(url, reqOptions);
|
||||
|
||||
const errProps = {
|
||||
// todo [breaking]: assign as non-enumerable property
|
||||
|
@ -150,7 +147,7 @@ const request = async (ctx, userAgent, reqData) => {
|
|||
let cType = res.headers.get('content-type');
|
||||
if (cType) {
|
||||
const {type} = parseContentType(cType);
|
||||
if (type !== req.headers['Accept']) {
|
||||
if (type !== reqOptions.headers['Accept']) {
|
||||
throw new HafasError('invalid/unsupported response content-type: ' + cType, null, errProps);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,6 @@
|
|||
"node": ">=16"
|
||||
},
|
||||
"dependencies": {
|
||||
"@derhuerst/round-robin-scheduler": "^1.0.4",
|
||||
"content-type": "^1.0.4",
|
||||
"cross-fetch": "^4.0.0",
|
||||
"db-hafas-stations": "^1.0.0",
|
||||
|
|
Loading…
Add table
Reference in a new issue