From f7909aac2909e904c7e8eed71b17a93af21a7ab9 Mon Sep 17 00:00:00 2001 From: McToel Date: Thu, 13 Feb 2025 15:25:33 +0100 Subject: [PATCH] Removed Proxy and local address code --- lib/request.js | 117 ++++++++++++++++++++++++------------------------- package.json | 1 - 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/lib/request.js b/lib/request.js index 349f86ba..1f0a5db7 100644 --- a/lib/request.js +++ b/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); } } diff --git a/package.json b/package.json index e92832f5..63a61a43 100644 --- a/package.json +++ b/package.json @@ -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",