feat(proxy): Add support for rotating proxies
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m41s

This commit is contained in:
Fijxu 2025-01-28 18:35:58 -03:00
parent 4a8f0f8cd2
commit 734b81590b
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

View file

@ -7,22 +7,40 @@ export const getFetchClient = (konfigStore: Store): {
}): Promise<Response>;
(input: URL | Request | string, init?: RequestInit): Promise<Response>;
} => {
if (Deno.env.get("PROXY") || konfigStore.get("networking.proxy")) {
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)) {
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);
if (!proxyUrl) {
throw new Error("No proxy available");
}
const client = Deno.createHttpClient({
proxy: {
url: Deno.env.get("PROXY") || konfigStore.get("networking.proxy") as string,
url: proxyUrl,
},
});
currentProxyIndex = (currentProxyIndex + 1) % proxies.length;
const fetchRes = await fetch(input, {
client,
headers: init?.headers,
method: init?.method,
body: init?.body,
});
return new Response(fetchRes.body, {
status: fetchRes.status,
headers: fetchRes.headers,