From e3a022972c9d7914bc2b878fd7ec2c020b8b3562 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 13 Jan 2022 13:55:17 +0100 Subject: [PATCH] request: tweak user-agent randomisation see also https://github.com/deg0nz/MMM-PublicTransportBerlin/pull/67 --- lib/request.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/request.js b/lib/request.js index 76384193..bd976052 100644 --- a/lib/request.js +++ b/lib/request.js @@ -51,10 +51,18 @@ if (proxyAddress) { getAgent = () => pool.get() } -const id = randomBytes(6).toString('hex') +const id = randomBytes(3).toString('hex') const randomizeUserAgent = (userAgent) => { - const i = Math.round(Math.random() * userAgent.length) - return userAgent.slice(0, i) + id + userAgent.slice(i) + 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 } const md5 = input => createHash('md5').update(input).digest()