db-vendo-client/lib/request.js

167 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-11-11 22:49:04 +01:00
'use strict'
const ProxyAgent = require('https-proxy-agent')
const {isIP} = require('net')
const {Agent: HttpsAgent} = require('https')
const roundRobin = require('@derhuerst/round-robin-scheduler')
const {randomBytes} = require('crypto')
const createHash = require('create-hash')
2019-02-05 19:07:19 +01:00
const {stringify} = require('qs')
const {Request, fetch} = require('cross-fetch')
const {parse: parseContentType} = require('content-type')
2022-05-03 23:21:44 +02:00
const {HafasError} = require('./errors')
const checkIfResponseIsOk = require('./check-if-res-is-ok')
2017-11-11 22:49:04 +01:00
2020-09-21 13:18:31 +02:00
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)
}
const plainAgent = new HttpsAgent({
keepAlive: true,
})
let getAgent = () => plainAgent
if (proxyAddress) {
// todo: this doesn't honor `keepAlive: true`
// related:
// - https://github.com/TooTallNate/node-https-proxy-agent/pull/112
// - https://github.com/TooTallNate/node-agent-base/issues/5
const agent = new ProxyAgent(proxyAddress)
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()
}
2020-09-21 13:18:31 +02:00
const id = randomBytes(3).toString('hex')
2018-08-08 18:40:35 +02:00
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
2018-08-08 18:40:35 +02:00
}
const md5 = input => createHash('md5').update(input).digest()
const request = async (ctx, userAgent, reqData) => {
const {profile, opt} = ctx
const rawReqBody = profile.transformReqBody(ctx, {
2020-03-18 21:35:43 +01:00
// todo: is it `eng` actually?
// RSAG has `deu` instead of `de`
lang: opt.language || profile.defaultLanguage || 'en',
svcReqL: [reqData],
client: profile.client, // client identification
ext: profile.ext, // ?
ver: profile.ver, // HAFAS protocol version
auth: profile.auth, // static authentication
2018-07-09 12:40:38 +02:00
})
2018-09-03 15:31:20 +02:00
const req = profile.transformReq(ctx, {
agent: getAgent(),
2017-11-11 22:49:04 +01:00
method: 'post',
// todo: CORS? referrer policy?
body: JSON.stringify(rawReqBody),
2017-11-11 22:49:04 +01:00
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, br, deflate',
2018-06-07 12:04:21 +02:00
'Accept': 'application/json',
'user-agent': profile.randomizeUserAgent
? randomizeUserAgent(userAgent)
: userAgent,
'connection': 'keep-alive', // prevent excessive re-connecting
2017-11-11 22:49:04 +01:00
},
redirect: 'follow',
query: {}
2017-11-11 22:49:04 +01:00
})
if (profile.addChecksum || profile.addMicMac) {
2021-01-14 20:30:48 +01:00
if (!Buffer.isBuffer(profile.salt) && 'string' !== typeof profile.salt) {
throw new TypeError('profile.salt must be a Buffer or a string.')
}
2021-01-14 20:30:48 +01:00
// Buffer.from(buf, 'hex') just returns buf
const salt = Buffer.from(profile.salt, 'hex')
if (profile.addChecksum) {
const checksum = md5(Buffer.concat([
Buffer.from(req.body, 'utf8'),
2021-01-14 20:30:48 +01:00
salt,
]))
req.query.checksum = checksum.toString('hex')
}
if (profile.addMicMac) {
const mic = md5(Buffer.from(req.body, 'utf8'))
req.query.mic = mic.toString('hex')
const micAsHex = Buffer.from(mic.toString('hex'), 'utf8')
2021-01-14 20:30:48 +01:00
const mac = md5(Buffer.concat([micAsHex, salt]))
req.query.mac = mac.toString('hex')
}
}
const reqId = randomBytes(3).toString('hex')
2018-03-02 23:57:29 +01:00
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
const res = await fetch(url, req)
const errProps = {
2022-05-03 23:21:44 +02:00
request: fetchReq,
response: res,
url,
}
if (!res.ok) {
2022-05-03 23:21:44 +02:00
// todo [breaking]: make this a FetchError or a HafasClientError?
const err = new Error(res.statusText)
Object.assign(err, errProps)
throw err
}
let cType = res.headers.get('content-type')
if (cType) {
const {type} = parseContentType(cType)
if (type !== 'application/json') {
2022-05-03 23:21:44 +02:00
throw new HafasError('invalid/unsupported response content-type: ' + cType, null, errProps)
2017-11-11 22:49:04 +01:00
}
}
const body = await res.text()
profile.logResponse(ctx, res, body, reqId)
const b = JSON.parse(body)
2022-05-03 23:21:44 +02:00
checkIfResponseIsOk({
body: b,
errProps,
})
2018-09-03 15:31:20 +02:00
const svcRes = b.svcResL[0].res
return {
res: svcRes,
common: profile.parseCommon({...ctx, res: svcRes}),
}
2017-11-11 22:49:04 +01:00
}
module.exports = request