summary refs log tree commit diff
path: root/src/api/middlewares/TestClient.ts
blob: c8ea57f627468c7f5f5dd7ab6d64cd16d064c95e (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import express, { Request, Response, Application } from "express";
import fs from "fs";
import path from "path";
import fetch, { Response as FetchResponse, Headers } from "node-fetch";
import ProxyAgent from 'proxy-agent';
import { Config } from "@fosscord/util";
import { AssetCacheItem } from "../util/entities/AssetCacheItem"
import { green } from "picocolors";

const AssetsPath = path.join(__dirname, "..", "..", "..", "assets")

export default function TestClient(app: Application) {
	const agent = new ProxyAgent();
	
	//build client page
	let html = fs.readFileSync(path.join(AssetsPath, "index.html"), { encoding: "utf8" });
	html = applyEnv(html);
	html = applyInlinePlugins(html);
	html = applyPlugins(html);
	html = applyPreloadPlugins(html);

	//load asset cache
	let newAssetCache: Map<string, AssetCacheItem> = new Map<string, AssetCacheItem>();
	let assetCacheDir = path.join(AssetsPath, "cache");
	if(process.env.ASSET_CACHE_DIR)
		assetCacheDir = process.env.ASSET_CACHE_DIR

	console.log(`[TestClient] ${green(`Using asset cache path: ${assetCacheDir}`)}`)
	if(!fs.existsSync(assetCacheDir)) {
		fs.mkdirSync(assetCacheDir);
	}
	if(fs.existsSync(path.join(assetCacheDir, "index.json"))) {
		let rawdata = fs.readFileSync(path.join(assetCacheDir, "index.json"));
		newAssetCache = new Map<string, AssetCacheItem>(Object.entries(JSON.parse(rawdata.toString())));
	}

	app.use("/assets", express.static(path.join(AssetsPath)));	
	app.get("/assets/:file", async (req: Request, res: Response) => {
		delete req.headers.host;
		let response: FetchResponse;
		let buffer: Buffer;
		let assetCacheItem: AssetCacheItem = new AssetCacheItem(req.params.file);
		if(newAssetCache.has(req.params.file)){
			assetCacheItem = newAssetCache.get(req.params.file)!;
			assetCacheItem.Headers.forEach((value: any, name: any) => {
				res.set(name, value);
			});
		}
		else {
			console.log(`[TestClient] Downloading file not yet cached! Asset file: ${req.params.file}`);
			response = await fetch(`https://discord.com/assets/${req.params.file}`, {
				agent,
				// @ts-ignore
				headers: {
					...req.headers
				}
			});
			
			//set cache info
			assetCacheItem.Headers = Object.fromEntries(stripHeaders(response.headers));
			assetCacheItem.FilePath = path.join(assetCacheDir, req.params.file);
			assetCacheItem.Key = req.params.file;
			//add to cache and save
			newAssetCache.set(req.params.file, assetCacheItem);
			fs.writeFileSync(path.join(assetCacheDir, "index.json"), JSON.stringify(Object.fromEntries(newAssetCache), null, 4));
			//download file
			fs.writeFileSync(assetCacheItem.FilePath, await response.buffer());
		}
		
		assetCacheItem.Headers.forEach((value: string, name: string) => {
			res.set(name, value);
		});
		return res.send(fs.readFileSync(assetCacheItem.FilePath));
	});
	app.get("/developers*", (_req: Request, res: Response) => {
		const { useTestClient } = Config.get().client;
		res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
		res.set("content-type", "text/html");

		if(!useTestClient) return res.send("Test client is disabled on this instance. Use a stand-alone client to connect this instance.")
		
		res.send(fs.readFileSync(path.join(__dirname, "..", "..", "..", "assets", "developers.html"), { encoding: "utf8" }));
	});
	app.get("*", (req: Request, res: Response) => {
		const { useTestClient } = Config.get().client;
		res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
		res.set("content-type", "text/html");

		if(req.url.startsWith("/api") || req.url.startsWith("/__development")) return;

		if(!useTestClient) return res.send("Test client is disabled on this instance. Use a stand-alone client to connect this instance.")
		if (req.url.startsWith("/invite")) return res.send(html.replace("9b2b7f0632acd0c5e781", "9f24f709a3de09b67c49"));
		
		res.send(html);
	});

	
}

function applyEnv(html: string): string {
	const CDN_ENDPOINT = (Config.get().cdn.endpointClient || Config.get()?.cdn.endpointPublic || process.env.CDN || "").replace(
		/(https?)?(:\/\/?)/g,
		""
	);
	const GATEWAY_ENDPOINT = Config.get().gateway.endpointClient || Config.get()?.gateway.endpointPublic || process.env.GATEWAY || "";

	if (CDN_ENDPOINT) {
		html = html.replace(/CDN_HOST: .+/, `CDN_HOST: \`${CDN_ENDPOINT}\`,`);
	}
	if (GATEWAY_ENDPOINT) {
		html = html.replace(/GATEWAY_ENDPOINT: .+/, `GATEWAY_ENDPOINT: \`${GATEWAY_ENDPOINT}\`,`);
	}
	return html;
}

function applyPlugins(html: string): string {
	// plugins
	let files = fs.readdirSync(path.join(AssetsPath, "plugins"));
	let plugins = "";
	files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script src='/assets/plugins/${x}'></script>\n`; });
	return html.replaceAll("<!-- plugin marker -->", plugins);
}

function applyInlinePlugins(html: string): string{
	// inline plugins
	let files = fs.readdirSync(path.join(AssetsPath, "inline-plugins"));
	let plugins = "";
	files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script src='/assets/inline-plugins/${x}'></script>\n\n`; });
	return html.replaceAll("<!-- inline plugin marker -->", plugins);
}

function applyPreloadPlugins(html: string): string{
	//preload plugins
	let files = fs.readdirSync(path.join(AssetsPath, "preload-plugins"));
	let plugins = "";
	files.forEach(x =>{if(x.endsWith(".js")) plugins += `<script>${fs.readFileSync(path.join(AssetsPath, "preload-plugins", x))}</script>\n`; });
	return html.replaceAll("<!-- preload plugin marker -->", plugins);
}

function stripHeaders(headers: Headers): Headers {
	[
		"content-length",
		"content-security-policy",
		"strict-transport-security",
		"set-cookie",
		"transfer-encoding",
		"expect-ct",
		"access-control-allow-origin",
		"content-encoding"
	].forEach(headerName => {
		headers.delete(headerName);
	});
	return headers;
}