better User-Agent randomization 🐛

This commit is contained in:
Jannis R 2018-08-08 18:46:20 +02:00
parent 0b81a59c71
commit 50bd4409f5
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 9 additions and 1 deletions

2
.gitignore vendored
View file

@ -4,3 +4,5 @@ Thumbs.db
.nvm-version
node_modules
npm-debug.log
/id.json

View file

@ -9,10 +9,16 @@ const {stringify} = require('query-string')
const Promise = require('pinkie-promise')
const {fetch} = require('fetch-ponyfill')({Promise})
const userAgent = 'https://github.com/public-transport/hafas-client'
const clientId = Math.random().toString(16).substr(2, 10)
const md5 = input => createHash('md5').update(input).digest()
const randomizeUserAgent = () => {
const i = Math.round(Math.random() * userAgent.length)
return userAgent.slice(0, i) + clientId + userAgent.slice(i)
}
const request = (profile, data) => {
const body = profile.transformReqBody({lang: 'en', svcReqL: [data]})
const req = profile.transformReq({
@ -23,7 +29,7 @@ const request = (profile, data) => {
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip, deflate',
'Accept': 'application/json',
'user-agent': clientId + ' https://github.com/public-transport/hafas-client'
'user-agent': randomizeUserAgent()
},
query: {}
})