2024-02-06 22:58:49 +01:00
|
|
|
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 createHash from 'create-hash';
|
|
|
|
import {Buffer} from 'node:buffer';
|
|
|
|
import {stringify} from 'qs';
|
|
|
|
import {Request, fetch} from 'cross-fetch';
|
|
|
|
import {parse as parseContentType} from 'content-type';
|
|
|
|
import {HafasError, byErrorCode} from './errors.js';
|
|
|
|
|
|
|
|
const proxyAddress = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null;
|
|
|
|
const localAddresses = process.env.LOCAL_ADDRESS || null;
|
2021-02-07 22:17:02 +01:00
|
|
|
|
|
|
|
if (proxyAddress && localAddresses) {
|
2024-02-06 22:58:49 +01:00
|
|
|
console.error('Both env vars HTTPS_PROXY/HTTP_PROXY and LOCAL_ADDRESS are not supported.');
|
|
|
|
process.exit(1);
|
2021-02-07 22:17:02 +01:00
|
|
|
}
|
2021-12-09 18:53:18 +01:00
|
|
|
|
|
|
|
const plainAgent = new HttpsAgent({
|
|
|
|
keepAlive: true,
|
2024-02-06 22:58:49 +01:00
|
|
|
});
|
|
|
|
let getAgent = () => plainAgent;
|
2021-12-09 18:53:18 +01:00
|
|
|
|
2021-02-07 22:17:02 +01:00
|
|
|
if (proxyAddress) {
|
2023-07-07 19:33:06 +02:00
|
|
|
const agent = new ProxyAgent(proxyAddress, {
|
|
|
|
keepAlive: true,
|
2023-07-25 16:08:51 +02:00
|
|
|
keepAliveMsecs: 10 * 1000, // 10s
|
2024-02-06 22:58:49 +01:00
|
|
|
});
|
|
|
|
getAgent = () => agent;
|
2021-02-07 22:17:02 +01:00
|
|
|
} else if (localAddresses) {
|
|
|
|
const agents = process.env.LOCAL_ADDRESS.split(',')
|
2024-02-06 22:58:49 +01:00
|
|
|
.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();
|
2021-02-07 22:17:02 +01:00
|
|
|
}
|
2020-09-21 13:18:31 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const id = randomBytes(3)
|
|
|
|
.toString('hex');
|
2018-08-08 18:40:35 +02:00
|
|
|
const randomizeUserAgent = (userAgent) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
let ua = userAgent;
|
2022-01-13 13:55:17 +01:00
|
|
|
for (
|
|
|
|
let i = Math.round(5 + Math.random() * 5);
|
|
|
|
i < ua.length;
|
|
|
|
i += Math.round(5 + Math.random() * 5)
|
|
|
|
) {
|
2024-02-06 22:58:49 +01:00
|
|
|
ua = ua.slice(0, i) + id + ua.slice(i);
|
|
|
|
i += id.length;
|
2022-01-13 13:55:17 +01:00
|
|
|
}
|
2024-02-06 22:58:49 +01:00
|
|
|
return ua;
|
|
|
|
};
|
2018-08-08 18:40:35 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const md5 = input => createHash('md5')
|
|
|
|
.update(input)
|
|
|
|
.digest();
|
2018-01-15 00:45:05 +01:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
const checkIfResponseIsOk = (_) => {
|
|
|
|
const {
|
|
|
|
body,
|
|
|
|
errProps: baseErrProps,
|
2024-02-06 22:58:49 +01:00
|
|
|
} = _;
|
2022-05-07 16:17:37 +02:00
|
|
|
|
|
|
|
const errProps = {
|
|
|
|
...baseErrProps,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
|
|
|
if (body.id) {
|
|
|
|
errProps.hafasResponseId = body.id;
|
2022-05-07 16:17:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Because we want more accurate stack traces, we don't construct the error here,
|
|
|
|
// but only return the constructor & error message.
|
|
|
|
const getError = (_) => {
|
|
|
|
// mutating here is ugly but pragmatic
|
2024-02-06 22:58:49 +01:00
|
|
|
if (_.errTxt) {
|
|
|
|
errProps.hafasMessage = _.errTxt;
|
|
|
|
}
|
|
|
|
if (_.errTxtOut) {
|
|
|
|
errProps.hafasDescription = _.errTxtOut;
|
|
|
|
}
|
2022-05-07 16:17:37 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
if (_.err in byErrorCode) {
|
|
|
|
return byErrorCode[_.err];
|
|
|
|
}
|
2022-05-07 16:17:37 +02:00
|
|
|
return {
|
|
|
|
Error: HafasError,
|
|
|
|
message: body.errTxt || 'unknown error',
|
|
|
|
props: {},
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
|
|
|
};
|
2022-05-07 16:17:37 +02:00
|
|
|
|
|
|
|
if (body.err && body.err !== 'OK') {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {Error: HafasError, message, props} = getError(body);
|
|
|
|
throw new HafasError(message, body.err, {...errProps, ...props});
|
2022-05-07 16:17:37 +02:00
|
|
|
}
|
|
|
|
if (!body.svcResL || !body.svcResL[0]) {
|
2024-02-06 22:58:49 +01:00
|
|
|
throw new HafasError('invalid/unsupported response structure', null, errProps);
|
2022-05-07 16:17:37 +02:00
|
|
|
}
|
|
|
|
if (body.svcResL[0].err !== 'OK') {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {Error: HafasError, message, props} = getError(body.svcResL[0]);
|
|
|
|
throw new HafasError(message, body.svcResL[0].err, {...errProps, ...props});
|
2022-05-07 16:17:37 +02:00
|
|
|
}
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2022-05-07 16:17:37 +02:00
|
|
|
|
2022-05-03 17:51:52 +02:00
|
|
|
const request = async (ctx, userAgent, reqData) => {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {profile, opt} = ctx;
|
2019-10-20 01:47:09 +02:00
|
|
|
|
2022-05-03 17:51:52 +02:00
|
|
|
const rawReqBody = profile.transformReqBody(ctx, {
|
2020-03-18 21:35:43 +01:00
|
|
|
// todo: is it `eng` actually?
|
|
|
|
// RSAG has `deu` instead of `de`
|
2021-04-18 18:41:27 +02:00
|
|
|
lang: opt.language || profile.defaultLanguage || 'en',
|
2022-01-04 15:32:16 +01:00
|
|
|
svcReqL: [reqData],
|
|
|
|
|
|
|
|
client: profile.client, // client identification
|
|
|
|
ext: profile.ext, // ?
|
|
|
|
ver: profile.ver, // HAFAS protocol version
|
|
|
|
auth: profile.auth, // static authentication
|
2024-02-06 22:58:49 +01:00
|
|
|
});
|
2018-09-03 15:31:20 +02:00
|
|
|
|
2019-10-20 01:47:09 +02:00
|
|
|
const req = profile.transformReq(ctx, {
|
2021-02-07 22:17:02 +01:00
|
|
|
agent: getAgent(),
|
2017-11-11 22:49:04 +01:00
|
|
|
method: 'post',
|
|
|
|
// todo: CORS? referrer policy?
|
2022-05-03 17:51:52 +02:00
|
|
|
body: JSON.stringify(rawReqBody),
|
2017-11-11 22:49:04 +01:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2019-06-24 18:26:11 +02:00
|
|
|
'Accept-Encoding': 'gzip, br, deflate',
|
2018-06-07 12:04:21 +02:00
|
|
|
'Accept': 'application/json',
|
2021-12-29 17:01:14 +01:00
|
|
|
'user-agent': profile.randomizeUserAgent
|
|
|
|
? randomizeUserAgent(userAgent)
|
|
|
|
: userAgent,
|
2021-12-09 18:53:18 +01:00
|
|
|
'connection': 'keep-alive', // prevent excessive re-connecting
|
2017-11-11 22:49:04 +01:00
|
|
|
},
|
2019-06-24 18:26:11 +02:00
|
|
|
redirect: 'follow',
|
2024-02-06 22:58:49 +01:00
|
|
|
query: {},
|
|
|
|
});
|
2017-11-11 22:49:04 +01:00
|
|
|
|
2018-01-15 00:45:40 +01:00
|
|
|
if (profile.addChecksum || profile.addMicMac) {
|
2021-01-14 20:30:48 +01:00
|
|
|
if (!Buffer.isBuffer(profile.salt) && 'string' !== typeof profile.salt) {
|
2024-02-06 22:58:49 +01:00
|
|
|
throw new TypeError('profile.salt must be a Buffer or a string.');
|
2018-01-15 00:45:05 +01:00
|
|
|
}
|
2021-01-14 20:30:48 +01:00
|
|
|
// Buffer.from(buf, 'hex') just returns buf
|
2024-02-06 22:58:49 +01:00
|
|
|
const salt = Buffer.from(profile.salt, 'hex');
|
2021-01-14 20:30:48 +01:00
|
|
|
|
2018-01-15 00:45:40 +01:00
|
|
|
if (profile.addChecksum) {
|
|
|
|
const checksum = md5(Buffer.concat([
|
|
|
|
Buffer.from(req.body, 'utf8'),
|
2021-01-14 20:30:48 +01:00
|
|
|
salt,
|
2024-02-06 22:58:49 +01:00
|
|
|
]));
|
|
|
|
req.query.checksum = checksum.toString('hex');
|
2018-01-15 00:45:40 +01:00
|
|
|
}
|
|
|
|
if (profile.addMicMac) {
|
2024-02-06 22:58:49 +01:00
|
|
|
const mic = md5(Buffer.from(req.body, 'utf8'));
|
|
|
|
req.query.mic = mic.toString('hex');
|
2018-01-15 00:45:40 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const micAsHex = Buffer.from(mic.toString('hex'), 'utf8');
|
|
|
|
const mac = md5(Buffer.concat([micAsHex, salt]));
|
|
|
|
req.query.mac = mac.toString('hex');
|
2018-01-15 00:45:40 +01:00
|
|
|
}
|
2018-01-15 00:45:05 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const reqId = randomBytes(3)
|
|
|
|
.toString('hex');
|
|
|
|
const url = profile.endpoint + '?' + stringify(req.query);
|
|
|
|
const fetchReq = new Request(url, req);
|
|
|
|
profile.logRequest(ctx, fetchReq, reqId);
|
2017-11-11 22:49:04 +01:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const res = await fetch(url, req);
|
2022-05-03 17:51:52 +02:00
|
|
|
|
|
|
|
const errProps = {
|
2024-01-18 15:27:35 +01:00
|
|
|
// todo [breaking]: assign as non-enumerable property
|
2022-05-03 23:21:44 +02:00
|
|
|
request: fetchReq,
|
2024-01-18 15:27:35 +01:00
|
|
|
// todo [breaking]: assign as non-enumerable property
|
2022-05-03 23:21:44 +02:00
|
|
|
response: res,
|
|
|
|
url,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
2022-05-03 17:51:52 +02:00
|
|
|
|
|
|
|
if (!res.ok) {
|
2022-05-03 23:21:44 +02:00
|
|
|
// todo [breaking]: make this a FetchError or a HafasClientError?
|
2024-02-06 22:58:49 +01:00
|
|
|
const err = new Error(res.statusText);
|
|
|
|
Object.assign(err, errProps);
|
|
|
|
throw err;
|
2022-05-03 17:51:52 +02:00
|
|
|
}
|
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
let cType = res.headers.get('content-type');
|
2022-05-03 17:51:52 +02:00
|
|
|
if (cType) {
|
2024-02-06 22:58:49 +01:00
|
|
|
const {type} = parseContentType(cType);
|
2022-05-03 17:51:52 +02:00
|
|
|
if (type !== 'application/json') {
|
2024-02-06 22:58:49 +01:00
|
|
|
throw new HafasError('invalid/unsupported response content-type: ' + cType, null, errProps);
|
2017-11-11 22:49:04 +01:00
|
|
|
}
|
2022-05-03 17:51:52 +02:00
|
|
|
}
|
2021-04-18 18:42:03 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const body = await res.text();
|
|
|
|
profile.logResponse(ctx, res, body, reqId);
|
2022-05-03 17:51:52 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const b = JSON.parse(body);
|
2022-05-03 23:21:44 +02:00
|
|
|
checkIfResponseIsOk({
|
|
|
|
body: b,
|
|
|
|
errProps,
|
2024-02-06 22:58:49 +01:00
|
|
|
});
|
2018-09-03 15:31:20 +02:00
|
|
|
|
2024-02-06 22:58:49 +01:00
|
|
|
const svcRes = b.svcResL[0].res;
|
2022-05-03 17:51:52 +02:00
|
|
|
return {
|
|
|
|
res: svcRes,
|
|
|
|
common: profile.parseCommon({...ctx, res: svcRes}),
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|
|
|
|
};
|
2017-11-11 22:49:04 +01:00
|
|
|
|
2022-05-07 16:17:37 +02:00
|
|
|
export {
|
|
|
|
checkIfResponseIsOk,
|
|
|
|
request,
|
2024-02-06 22:58:49 +01:00
|
|
|
};
|