aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Utility/proxy.ts
blob: 57883836d034b00fb424ba12bc3b095edf3ad09c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { env } from "$env/dynamic/public";
import { isLocalApp } from "$lib/Utility/appOrigin";

const DEFAULT_PROXY_ORIGIN = "https://proxy.due.moe";

const normaliseOrigin = (origin: string) =>
	origin.endsWith("/") ? origin.slice(0, -1) : origin;

export const proxyOrigin = () =>
	normaliseOrigin(env.PUBLIC_PROXY_ORIGIN || DEFAULT_PROXY_ORIGIN);

export const proxyRoute = (path = "/") =>
	`${proxyOrigin()}${path.startsWith("/") ? path : `/${path}`}`;

export const proxy = (url: string, disable = false) => {
	const randomKey = Math.floor(Math.random() * 90) + 10;
	const forcedProxy = Boolean(env.PUBLIC_PROXY_ORIGIN);

	return isLocalApp() && !disable && !forcedProxy
		? url
		: `${proxyOrigin()}/?d2=${btoa(
				url
					.split("")
					.map((char) => char.charCodeAt(0) + randomKey)
					.join(":"),
			)}${randomKey}&dh`;
};

export default proxy;