2017-11-11 22:49:04 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-09-03 15:45:31 +02:00
|
|
|
const DEV = process.env.NODE_ENV === 'dev'
|
2021-02-17 18:33:18 +01:00
|
|
|
const DEBUG = /(^|,)hafas-client(,|$)/.test(process.env.DEBUG || '')
|
2018-09-03 15:31:20 +02:00
|
|
|
|
2021-02-07 22:17:02 +01:00
|
|
|
const ProxyAgent = require('https-proxy-agent')
|
|
|
|
const {isIP} = require('net')
|
|
|
|
const {Agent: HttpsAgent} = require('https')
|
|
|
|
const roundRobin = require('@derhuerst/round-robin-scheduler')
|
2019-08-04 14:32:44 +02:00
|
|
|
const {randomBytes} = require('crypto')
|
2018-05-21 12:15:40 +02:00
|
|
|
const createHash = require('create-hash')
|
2021-01-14 20:31:14 +01:00
|
|
|
const pick = require('lodash/pick')
|
2018-09-03 15:45:31 +02:00
|
|
|
const captureStackTrace = DEV ? require('capture-stack-trace') : () => {}
|
2019-02-05 19:07:19 +01:00
|
|
|
const {stringify} = require('qs')
|
2017-11-11 22:49:04 +01:00
|
|
|
const Promise = require('pinkie-promise')
|
|
|
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
2021-04-18 18:42:03 +02:00
|
|
|
const {parse: parseContentType} = require('content-type')
|
2020-04-13 17:06:50 +02:00
|
|
|
const {addErrorInfo} = require('./errors')
|
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
|
2021-02-07 22:17:02 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-12-09 18:53:18 +01:00
|
|
|
|
|
|
|
const plainAgent = new HttpsAgent({
|
|
|
|
keepAlive: true,
|
|
|
|
})
|
|
|
|
let getAgent = () => plainAgent
|
|
|
|
|
2021-02-07 22:17:02 +01:00
|
|
|
if (proxyAddress) {
|
2021-12-09 18:53:18 +01:00
|
|
|
// 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
|
2021-02-07 22:17:02 +01:00
|
|
|
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)
|
2021-12-09 18:53:18 +01:00
|
|
|
return new HttpsAgent({
|
|
|
|
localAddress: addr, family,
|
|
|
|
keepAlive: true,
|
|
|
|
})
|
2021-02-07 22:17:02 +01:00
|
|
|
})
|
|
|
|
const pool = roundRobin(agents)
|
|
|
|
getAgent = () => pool.get()
|
|
|
|
}
|
2020-09-21 13:18:31 +02:00
|
|
|
|
2019-08-04 14:32:44 +02:00
|
|
|
const id = randomBytes(6).toString('hex')
|
2018-08-08 18:40:35 +02:00
|
|
|
const randomizeUserAgent = (userAgent) => {
|
|
|
|
const i = Math.round(Math.random() * userAgent.length)
|
|
|
|
return userAgent.slice(0, i) + id + userAgent.slice(i)
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:15:40 +02:00
|
|
|
const md5 = input => createHash('md5').update(input).digest()
|
2018-01-15 00:45:05 +01:00
|
|
|
|
2021-12-08 14:12:22 +01:00
|
|
|
// todo [breaking]: remove userAgent parameter
|
2019-10-20 01:47:09 +02:00
|
|
|
const request = (ctx, userAgent, reqData) => {
|
|
|
|
const {profile, opt} = ctx
|
|
|
|
|
|
|
|
const body = 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',
|
2019-10-20 01:47:09 +02:00
|
|
|
svcReqL: [reqData]
|
2018-07-09 12:40:38 +02:00
|
|
|
})
|
2021-01-14 20:31:14 +01:00
|
|
|
Object.assign(body, pick(profile, [
|
|
|
|
'client', // client identification
|
|
|
|
'ext', // ?
|
|
|
|
'ver', // HAFAS protocol version
|
|
|
|
'auth', // static authentication
|
|
|
|
]))
|
2018-09-03 15:31:20 +02:00
|
|
|
if (DEBUG) console.error(JSON.stringify(body))
|
|
|
|
|
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?
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
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-09 18:53:18 +01:00
|
|
|
'user-agent': randomizeUserAgent(userAgent),
|
|
|
|
'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',
|
2018-01-15 00:45:05 +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) {
|
|
|
|
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
|
|
|
|
const salt = Buffer.from(profile.salt, 'hex')
|
|
|
|
|
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,
|
2018-01-15 00:45:40 +01:00
|
|
|
]))
|
|
|
|
req.query.checksum = checksum.toString('hex')
|
|
|
|
}
|
|
|
|
if (profile.addMicMac) {
|
|
|
|
const mic = md5(Buffer.from(req.body, 'utf8'))
|
|
|
|
req.query.mic = mic.toString('hex')
|
|
|
|
|
2018-01-19 15:47:41 +01:00
|
|
|
const micAsHex = Buffer.from(mic.toString('hex'), 'utf8')
|
2021-01-14 20:30:48 +01:00
|
|
|
const mac = md5(Buffer.concat([micAsHex, salt]))
|
2018-01-15 00:45:40 +01:00
|
|
|
req.query.mac = mac.toString('hex')
|
|
|
|
}
|
2018-01-15 00:45:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 23:57:29 +01:00
|
|
|
const url = profile.endpoint + '?' + stringify(req.query)
|
2017-11-11 22:49:04 +01:00
|
|
|
|
2018-01-23 01:24:37 +01:00
|
|
|
// Async stack traces are not supported everywhere yet, so we create our own.
|
|
|
|
const err = new Error()
|
2018-12-16 23:47:36 +01:00
|
|
|
err.isHafasError = true // todo: rename to `isHafasClientError`
|
2019-10-20 02:24:07 +02:00
|
|
|
err.request = req.body // todo: commit as bugfix
|
2018-03-02 23:57:29 +01:00
|
|
|
err.url = url
|
2018-01-23 01:24:37 +01:00
|
|
|
captureStackTrace(err)
|
|
|
|
|
2017-11-11 22:49:04 +01:00
|
|
|
return fetch(url, req)
|
|
|
|
.then((res) => {
|
2018-01-23 01:24:37 +01:00
|
|
|
err.statusCode = res.status
|
2017-11-11 22:49:04 +01:00
|
|
|
if (!res.ok) {
|
2018-01-23 01:24:37 +01:00
|
|
|
err.message = res.statusText
|
|
|
|
throw err
|
2017-11-11 22:49:04 +01:00
|
|
|
}
|
2021-04-18 18:42:03 +02:00
|
|
|
|
|
|
|
let cType = res.headers.get('content-type')
|
|
|
|
if (cType) {
|
|
|
|
const {type} = parseContentType(cType)
|
|
|
|
if (type !== 'application/json') {
|
|
|
|
const err = new Error('invalid response content-type: ' + cType)
|
|
|
|
err.response = res
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 22:49:04 +01:00
|
|
|
return res.json()
|
|
|
|
})
|
|
|
|
.then((b) => {
|
2018-09-03 15:31:20 +02:00
|
|
|
if (DEBUG) console.error(JSON.stringify(b))
|
|
|
|
|
2018-12-06 11:31:38 +01:00
|
|
|
if (b.err && b.err !== 'OK') {
|
2020-02-04 18:42:11 +01:00
|
|
|
addErrorInfo(err, b.err, b.errTxt, b.id)
|
2018-01-23 01:24:37 +01:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
if (!b.svcResL || !b.svcResL[0]) {
|
|
|
|
err.message = 'invalid response'
|
|
|
|
throw err
|
|
|
|
}
|
2017-11-11 22:49:04 +01:00
|
|
|
if (b.svcResL[0].err !== 'OK') {
|
2020-02-04 18:42:11 +01:00
|
|
|
addErrorInfo(err, b.svcResL[0].err, b.svcResL[0].errTxt, b.id)
|
2018-01-23 01:24:37 +01:00
|
|
|
throw err
|
2017-11-11 22:49:04 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:47:09 +02:00
|
|
|
const res = b.svcResL[0].res
|
|
|
|
return {
|
|
|
|
res,
|
|
|
|
common: profile.parseCommon({...ctx, res})
|
|
|
|
}
|
2017-11-11 22:49:04 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = request
|