refactor: move proxy rotator logic outside of getFetchClient

This commit is contained in:
Fijxu 2025-02-03 16:29:07 -03:00
parent 55df6e6692
commit 6b83617fcb
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

View file

@ -1,5 +1,18 @@
import { Store } from "@willsoto/node-konfig-core";
let proxies: string[] = [];
let currentProxyIndex = 0;
let proxyData: string;
try {
proxyData = Deno.readTextFileSync("proxies.txt");
proxies = proxyData.split("\n").map((line) => line.trim()).filter((line) =>
line.length > 0
);
} catch (error) {
console.error("[ERROR] Error reading proxy file:", error);
console.log("[INFO] Proxies from a list of proxies will not be used");
}
export const getFetchClient = (konfigStore: Store): {
(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
(
@ -10,22 +23,17 @@ export const getFetchClient = (konfigStore: Store): {
): Promise<Response>;
(input: URL | Request | string, init?: RequestInit): Promise<Response>;
} => {
let proxies: string[] = [];
let currentProxyIndex = 0;
let proxyData
try {
proxyData = Deno.readTextFileSync("proxies.txt");
proxies = proxyData.split("\n").map(line => line.trim()).filter(line => line.length > 0);
} catch (error) {
console.error("Error reading proxy file:", error);
}
if (Deno.env.get("PROXY") || konfigStore.get("networking.proxy") || (proxies.length > 0)) {
if (
Deno.env.get("PROXY") || konfigStore.get("networking.proxy") ||
(proxies.length > 0)
) {
return async (
input: RequestInfo | URL,
init?: RequestInit,
) => {
const proxyUrl = Deno.env.get("PROXY") || konfigStore.get("networking.proxy") as string || (proxies.length > 0 ? proxies[currentProxyIndex] : null);
const proxyUrl = Deno.env.get("PROXY") ||
konfigStore.get("networking.proxy") as string ||
(proxies.length > 0 ? proxies[currentProxyIndex] : null);
if (!proxyUrl) {
throw new Error("No proxy available");
}