From 485698a43cc6c308a64fa2675d174f19d78052bf Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 22 Feb 2023 21:41:13 +0100 Subject: [PATCH] support for round robin proxies --- src/backend/helpers/fetch.ts | 11 ++++++++++- src/index.tsx | 2 +- src/setup/config.ts | 15 +++++++++++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/backend/helpers/fetch.ts b/src/backend/helpers/fetch.ts index 9428ab03..393ff7f8 100644 --- a/src/backend/helpers/fetch.ts +++ b/src/backend/helpers/fetch.ts @@ -1,6 +1,15 @@ import { conf } from "@/setup/config"; import { ofetch } from "ofetch"; +let proxyUrlIndex = Math.floor(Math.random() * conf().PROXY_URLS.length); + +// round robins all proxy urls +function getProxyUrl(): string { + const url = conf().PROXY_URLS[proxyUrlIndex]; + proxyUrlIndex = (proxyUrlIndex + 1) % conf().PROXY_URLS.length; + return url; +} + type P = Parameters>; type R = ReturnType>; @@ -41,7 +50,7 @@ export function proxiedFetch(url: string, ops: P[1] = {}): R { parsedUrl.searchParams.set(k, v); }); - return baseFetch(conf().BASE_PROXY_URL, { + return baseFetch(getProxyUrl(), { ...ops, baseURL: undefined, params: { diff --git a/src/index.tsx b/src/index.tsx index ea54335a..5d2b07a7 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -16,7 +16,7 @@ import { initializeStores } from "./utils/storage"; const key = (window as any)?.__CONFIG__?.VITE_KEY ?? import.meta.env.VITE_KEY ?? null; if (key) { - (window as any).initMW(conf().BASE_PROXY_URL, key); + (window as any).initMW(conf().PROXY_URLS, key); } initializeChromecast(); diff --git a/src/setup/config.ts b/src/setup/config.ts index 72a762f5..679e5141 100644 --- a/src/setup/config.ts +++ b/src/setup/config.ts @@ -10,8 +10,14 @@ interface Config { NORMAL_ROUTER: boolean; } -export interface RuntimeConfig extends Config { - BASE_PROXY_URL: string; +export interface RuntimeConfig { + APP_VERSION: string; + GITHUB_LINK: string; + DISCORD_LINK: string; + OMDB_API_KEY: string; + TMDB_API_KEY: string; + NORMAL_ROUTER: boolean; + PROXY_URLS: string[]; } const env: Record = { @@ -51,8 +57,9 @@ export function conf(): RuntimeConfig { DISCORD_LINK, OMDB_API_KEY: getKey("OMDB_API_KEY"), TMDB_API_KEY: getKey("TMDB_API_KEY"), - BASE_PROXY_URL: getKey("CORS_PROXY_URL"), - CORS_PROXY_URL: `${getKey("CORS_PROXY_URL")}/?destination=`, + PROXY_URLS: getKey("CORS_PROXY_URL") + .split(",") + .map((v) => v.trim()), NORMAL_ROUTER: (getKey("NORMAL_ROUTER") ?? "false") === "true", }; }