mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
add profile.randomizeUserAgent flag 📝
This commit is contained in:
parent
e0cdd55908
commit
492fdeb2ef
4 changed files with 29 additions and 2 deletions
|
@ -60,6 +60,30 @@ const retryingDbProfile = withRetrying(dbProfile, {
|
||||||
const client = createClient(retryingDbProfile, 'my-awesome-program')
|
const client = createClient(retryingDbProfile, 'my-awesome-program')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## User agent randomization
|
||||||
|
|
||||||
|
By default, `hafas-client` randomizes the client name that you pass into `createClient`, and sends it as [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) in a randomized form:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const createClient('hafas-client')
|
||||||
|
// …
|
||||||
|
const client = createClient(someProfile, 'my-awesome-program')
|
||||||
|
|
||||||
|
await client.journeys(/* … */)
|
||||||
|
// User-Agent: my-awee70429some-pre70429ogram
|
||||||
|
await client.journeys(/* … */)
|
||||||
|
// User-Agent: my-awesom9bb8e2e-prog9bb8e2ram
|
||||||
|
```
|
||||||
|
|
||||||
|
You can turn this off by setting `profile.randomizeUserAgent` to `false`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const client = createClient({
|
||||||
|
...someProfile,
|
||||||
|
randomizeUserAgent: false,
|
||||||
|
}, 'my-awesome-program')
|
||||||
|
```
|
||||||
|
|
||||||
## Logging requests
|
## Logging requests
|
||||||
|
|
||||||
You can use `profile.logRequest` and `profile.logResponse` to process the raw [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), respectively.
|
You can use `profile.logRequest` and `profile.logResponse` to process the raw [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), respectively.
|
||||||
|
|
|
@ -65,6 +65,7 @@ const defaultProfile = {
|
||||||
salt: null,
|
salt: null,
|
||||||
addChecksum: false,
|
addChecksum: false,
|
||||||
addMicMac: false,
|
addMicMac: false,
|
||||||
|
randomizeUserAgent: true,
|
||||||
logRequest,
|
logRequest,
|
||||||
logResponse,
|
logResponse,
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,6 @@ const randomizeUserAgent = (userAgent) => {
|
||||||
|
|
||||||
const md5 = input => createHash('md5').update(input).digest()
|
const md5 = input => createHash('md5').update(input).digest()
|
||||||
|
|
||||||
// todo [breaking]: remove userAgent parameter
|
|
||||||
const request = (ctx, userAgent, reqData) => {
|
const request = (ctx, userAgent, reqData) => {
|
||||||
const {profile, opt} = ctx
|
const {profile, opt} = ctx
|
||||||
|
|
||||||
|
@ -92,7 +91,9 @@ const request = (ctx, userAgent, reqData) => {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept-Encoding': 'gzip, br, deflate',
|
'Accept-Encoding': 'gzip, br, deflate',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'user-agent': randomizeUserAgent(userAgent),
|
'user-agent': profile.randomizeUserAgent
|
||||||
|
? randomizeUserAgent(userAgent)
|
||||||
|
: userAgent,
|
||||||
'connection': 'keep-alive', // prevent excessive re-connecting
|
'connection': 'keep-alive', // prevent excessive re-connecting
|
||||||
},
|
},
|
||||||
redirect: 'follow',
|
redirect: 'follow',
|
||||||
|
|
|
@ -43,6 +43,7 @@ if (process.env.VCR_MODE && !process.env.VCR_OFF) {
|
||||||
const replayer = require('replayer')
|
const replayer = require('replayer')
|
||||||
replayer.configure({
|
replayer.configure({
|
||||||
headerWhitelist: [
|
headerWhitelist: [
|
||||||
|
// excludes User-Agent & Connection
|
||||||
'Content-Type', 'Accept-Encoding', 'Accept',
|
'Content-Type', 'Accept-Encoding', 'Accept',
|
||||||
],
|
],
|
||||||
includeHeaderValues: true,
|
includeHeaderValues: true,
|
||||||
|
|
Loading…
Add table
Reference in a new issue