From f098da870c3414c31be05cfbddc6db973ef87467 Mon Sep 17 00:00:00 2001 From: jj Date: Fri, 1 Nov 2024 14:55:00 +0000 Subject: [PATCH] api/cookie: pick cookie at random instead of round-robin --- api/src/processing/cookie/manager.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/api/src/processing/cookie/manager.js b/api/src/processing/cookie/manager.js index 02eecfda..eb25adbb 100644 --- a/api/src/processing/cookie/manager.js +++ b/api/src/processing/cookie/manager.js @@ -4,9 +4,7 @@ import { readFile, writeFile } from 'fs/promises'; import { Green, Yellow } from '../../misc/console-text.js'; import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser'; -const WRITE_INTERVAL = 60000, - COUNTER = Symbol('counter'); - +const WRITE_INTERVAL = 60000; let cookies = {}, dirty = false, intervalId; function writeChanges(cookiePath) { @@ -33,18 +31,14 @@ export const setup = async (cookiePath) => { export function getCookie(service) { if (!cookies[service] || !cookies[service].length) return; - let n; - if (cookies[service][COUNTER] === undefined) { - n = cookies[service][COUNTER] = 0 - } else { - ++cookies[service][COUNTER] - n = (cookies[service][COUNTER] %= cookies[service].length) + const idx = Math.floor(Math.random() * cookies[service].length); + + const cookie = cookies[service][idx]; + if (typeof cookie === 'string') { + cookies[service][idx] = Cookie.fromString(cookie); } - const cookie = cookies[service][n]; - if (typeof cookie === 'string') cookies[service][n] = Cookie.fromString(cookie); - - return cookies[service][n] + return cookies[service][idx]; } export function updateCookieValues(cookie, values) {