mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
move checksum calculation to lib/request
This commit is contained in:
parent
cea043e44f
commit
904f890442
3 changed files with 23 additions and 16 deletions
|
@ -26,6 +26,9 @@ const filters = require('../format/filters')
|
||||||
const id = x => x
|
const id = x => x
|
||||||
|
|
||||||
const defaultProfile = {
|
const defaultProfile = {
|
||||||
|
salt: null,
|
||||||
|
addChecksum: false,
|
||||||
|
|
||||||
transformReqBody: id,
|
transformReqBody: id,
|
||||||
transformReq: id,
|
transformReq: id,
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('crypto')
|
||||||
const Promise = require('pinkie-promise')
|
const Promise = require('pinkie-promise')
|
||||||
const {fetch} = require('fetch-ponyfill')({Promise})
|
const {fetch} = require('fetch-ponyfill')({Promise})
|
||||||
const {stringify} = require('query-string')
|
const {stringify} = require('query-string')
|
||||||
|
@ -9,6 +10,8 @@ const hafasError = (err) => {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const md5 = input => crypto.createHash('md5').update(input).digest()
|
||||||
|
|
||||||
const request = (profile, data) => {
|
const request = (profile, data) => {
|
||||||
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
|
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
|
||||||
const req = profile.transformReq({
|
const req = profile.transformReq({
|
||||||
|
@ -20,10 +23,21 @@ const request = (profile, data) => {
|
||||||
'Accept-Encoding': 'gzip, deflate',
|
'Accept-Encoding': 'gzip, deflate',
|
||||||
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
'user-agent': 'https://github.com/derhuerst/hafas-client'
|
||||||
},
|
},
|
||||||
query: null
|
query: {}
|
||||||
})
|
})
|
||||||
const url = profile.endpoint + (req.query ? '?' + stringify(req.query) : '')
|
|
||||||
|
|
||||||
|
if (profile.addChecksum) {
|
||||||
|
if (!Buffer.isBuffer(profile.salt)) {
|
||||||
|
throw new Error('profile.salt must be a Buffer.')
|
||||||
|
}
|
||||||
|
const checksum = md5(Buffer.concat([
|
||||||
|
Buffer.from(req.body, 'utf8'),
|
||||||
|
profile.salt
|
||||||
|
]))
|
||||||
|
req.query.checksum = checksum.toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = profile.endpoint +'?' + stringify(req.query)
|
||||||
return fetch(url, req)
|
return fetch(url, req)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const crypto = require('crypto')
|
|
||||||
|
|
||||||
const _createParseLine = require('../../parse/line')
|
const _createParseLine = require('../../parse/line')
|
||||||
const _createParseJourney = require('../../parse/journey')
|
const _createParseJourney = require('../../parse/journey')
|
||||||
const _formatStation = require('../../format/station')
|
const _formatStation = require('../../format/station')
|
||||||
|
@ -23,17 +21,6 @@ const transformReqBody = (body) => {
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
const salt = 'bdI8UVj40K5fvxwf'
|
|
||||||
const transformReq = (req) => {
|
|
||||||
const hash = crypto.createHash('md5')
|
|
||||||
hash.update(req.body + salt)
|
|
||||||
|
|
||||||
if (!req.query) req.query = {}
|
|
||||||
req.query.checksum = hash.digest('hex')
|
|
||||||
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
|
|
||||||
const transformJourneysQuery = (query, opt) => {
|
const transformJourneysQuery = (query, opt) => {
|
||||||
const filters = query.jnyFltrL
|
const filters = query.jnyFltrL
|
||||||
if (opt.bike) filters.push(bike)
|
if (opt.bike) filters.push(bike)
|
||||||
|
@ -142,8 +129,11 @@ const dbProfile = {
|
||||||
locale: 'de-DE',
|
locale: 'de-DE',
|
||||||
timezone: 'Europe/Berlin',
|
timezone: 'Europe/Berlin',
|
||||||
endpoint: 'https://reiseauskunft.bahn.de/bin/mgate.exe',
|
endpoint: 'https://reiseauskunft.bahn.de/bin/mgate.exe',
|
||||||
|
|
||||||
|
salt: Buffer.from('bdI8UVj40K5fvxwf', 'utf8'),
|
||||||
|
addChecksum: true,
|
||||||
|
|
||||||
transformReqBody,
|
transformReqBody,
|
||||||
transformReq,
|
|
||||||
transformJourneysQuery,
|
transformJourneysQuery,
|
||||||
|
|
||||||
products: modes.allProducts,
|
products: modes.allProducts,
|
||||||
|
|
Loading…
Add table
Reference in a new issue