mirror of
https://github.com/wukko/cobalt.git
synced 2025-06-13 05:37:44 +02:00
api/cookie: pick cookie at random instead of round-robin
This commit is contained in:
@ -4,9 +4,7 @@ import { readFile, writeFile } from 'fs/promises';
|
|||||||
import { Green, Yellow } from '../../misc/console-text.js';
|
import { Green, Yellow } from '../../misc/console-text.js';
|
||||||
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
|
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
|
||||||
|
|
||||||
const WRITE_INTERVAL = 60000,
|
const WRITE_INTERVAL = 60000;
|
||||||
COUNTER = Symbol('counter');
|
|
||||||
|
|
||||||
let cookies = {}, dirty = false, intervalId;
|
let cookies = {}, dirty = false, intervalId;
|
||||||
|
|
||||||
function writeChanges(cookiePath) {
|
function writeChanges(cookiePath) {
|
||||||
@ -33,18 +31,14 @@ export const setup = async (cookiePath) => {
|
|||||||
export function getCookie(service) {
|
export function getCookie(service) {
|
||||||
if (!cookies[service] || !cookies[service].length) return;
|
if (!cookies[service] || !cookies[service].length) return;
|
||||||
|
|
||||||
let n;
|
const idx = Math.floor(Math.random() * cookies[service].length);
|
||||||
if (cookies[service][COUNTER] === undefined) {
|
|
||||||
n = cookies[service][COUNTER] = 0
|
const cookie = cookies[service][idx];
|
||||||
} else {
|
if (typeof cookie === 'string') {
|
||||||
++cookies[service][COUNTER]
|
cookies[service][idx] = Cookie.fromString(cookie);
|
||||||
n = (cookies[service][COUNTER] %= cookies[service].length)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cookie = cookies[service][n];
|
return cookies[service][idx];
|
||||||
if (typeof cookie === 'string') cookies[service][n] = Cookie.fromString(cookie);
|
|
||||||
|
|
||||||
return cookies[service][n]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateCookieValues(cookie, values) {
|
export function updateCookieValues(cookie, values) {
|
||||||
|
Reference in New Issue
Block a user