support for round robin proxies

This commit is contained in:
mrjvs 2023-02-22 21:41:13 +01:00
parent 444156236c
commit 485698a43c
3 changed files with 22 additions and 6 deletions

View File

@ -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<T> = Parameters<typeof ofetch<T>>;
type R<T> = ReturnType<typeof ofetch<T>>;
@ -41,7 +50,7 @@ export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
parsedUrl.searchParams.set(k, v);
});
return baseFetch<T>(conf().BASE_PROXY_URL, {
return baseFetch<T>(getProxyUrl(), {
...ops,
baseURL: undefined,
params: {

View File

@ -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();

View File

@ -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<keyof Config, undefined | string> = {
@ -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",
};
}