Files
portal/render-service/proxy-pool.mjs
T

35 lines
1.3 KiB
JavaScript
Raw Normal View History

// Пул прокси round-robin. Источник — env PROXIES (через запятую). Провайдер-агностично: резидентные/
// мобильные RU. Формат каждого: «http://user:pass@host:port» ИЛИ «host:port» (тогда схема http).
// Ключи/пароли — ТОЛЬКО в .env виртуалки, в гит не идут. Пул пуст → next() отдаёт null (без прокси).
function parseOne(raw) {
const s = String(raw).trim();
if (s === '') return null;
const withScheme = /^[a-z]+:\/\//i.test(s) ? s : 'http://' + s;
let u;
try { u = new URL(withScheme); } catch { return null; }
const proxy = { server: `${u.protocol}//${u.host}` };
if (u.username) proxy.username = decodeURIComponent(u.username);
if (u.password) proxy.password = decodeURIComponent(u.password);
return proxy;
}
export function createProxyPool(envValue) {
const list = String(envValue || '')
.split(',')
.map(parseOne)
.filter(Boolean);
let cursor = 0;
return {
size: list.length,
// Следующий прокси по кругу (playwright-объект) или null, если пул пуст.
next() {
if (list.length === 0) return null;
const p = list[cursor % list.length];
cursor++;
return p;
},
};
}