feat(proxy): Add support for rotating proxies
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m41s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m41s
This commit is contained in:
parent
4a8f0f8cd2
commit
734b81590b
1 changed files with 20 additions and 2 deletions
|
@ -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,
|
||||
|
|
Reference in a new issue