diff options
Diffstat (limited to 'apps')
| -rw-r--r-- | apps/workers/utils.ts | 14 | ||||
| -rw-r--r-- | apps/workers/workers/crawlerWorker.ts | 7 |
2 files changed, 14 insertions, 7 deletions
diff --git a/apps/workers/utils.ts b/apps/workers/utils.ts index 55204570..a82dd12d 100644 --- a/apps/workers/utils.ts +++ b/apps/workers/utils.ts @@ -21,6 +21,10 @@ export function withTimeout<T, Ret>( }; } +export function getRandomProxy(proxyList: string[]): string { + return proxyList[Math.floor(Math.random() * proxyList.length)].trim(); +} + function getProxyAgent(url: string) { const { proxy } = serverConfig; @@ -48,12 +52,14 @@ function getProxyAgent(url: string) { } if (protocol === "https:" && proxy.httpsProxy) { - return new HttpsProxyAgent(proxy.httpsProxy); + const selectedProxy = getRandomProxy(proxy.httpsProxy); + return new HttpsProxyAgent(selectedProxy); } else if (protocol === "http:" && proxy.httpProxy) { - return new HttpProxyAgent(proxy.httpProxy); + const selectedProxy = getRandomProxy(proxy.httpProxy); + return new HttpProxyAgent(selectedProxy); } else if (proxy.httpProxy) { - // Fallback to HTTP proxy for HTTPS if HTTPS proxy not configured - return new HttpProxyAgent(proxy.httpProxy); + const selectedProxy = getRandomProxy(proxy.httpProxy); + return new HttpProxyAgent(selectedProxy); } return undefined; diff --git a/apps/workers/workers/crawlerWorker.ts b/apps/workers/workers/crawlerWorker.ts index 4831b4c4..e9024723 100644 --- a/apps/workers/workers/crawlerWorker.ts +++ b/apps/workers/workers/crawlerWorker.ts @@ -28,7 +28,7 @@ import { workerStatsCounter } from "metrics"; import { Browser, BrowserContextOptions } from "playwright"; import { chromium } from "playwright-extra"; import StealthPlugin from "puppeteer-extra-plugin-stealth"; -import { fetchWithProxy } from "utils"; +import { fetchWithProxy, getRandomProxy } from "utils"; import { getBookmarkDetails, updateAsset } from "workerUtils"; import { z } from "zod"; @@ -160,12 +160,13 @@ function getPlaywrightProxyConfig(): BrowserContextOptions["proxy"] { } // Use HTTPS proxy if available, otherwise fall back to HTTP proxy - const proxyUrl = proxy.httpsProxy || proxy.httpProxy; - if (!proxyUrl) { + const proxyList = proxy.httpsProxy || proxy.httpProxy; + if (!proxyList) { // Unreachable, but TypeScript doesn't know that return undefined; } + const proxyUrl = getRandomProxy(proxyList); const parsed = new URL(proxyUrl); return { |
