summary refs log tree commit diff
path: root/src/Server.ts
blob: 0ff4df8c479fd48e877ccae1e9772c0138731eb2 (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
155
import "missing-native-js-functions";
import fs from "fs/promises";
import { Connection } from "mongoose";
import { Server, ServerOptions } from "lambert-server";
import { Authentication, CORS, GlobalRateLimit } from "./middlewares/";
import { Config, db } from "@fosscord/server-util";
import i18next from "i18next";
import i18nextMiddleware, { I18next } from "i18next-http-middleware";
import i18nextBackend from "i18next-node-fs-backend";
import { ErrorHandler } from "./middlewares/ErrorHandler";
import { BodyParser } from "./middlewares/BodyParser";
import express, { Router } from "express";
import fetch, { Response } from "node-fetch";
import mongoose from "mongoose";
import path from "path";

// this will return the new updated document for findOneAndUpdate
mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate

export interface FosscordServerOptions extends ServerOptions {}

declare global {
	namespace Express {
		interface Request {
			// @ts-ignore
			server: FosscordServer;
		}
	}
}

const assetCache = new Map<
	string,
	{
		response: Response;
		buffer: Buffer;
	}
>();

export class FosscordServer extends Server {
	public options: FosscordServerOptions;

	constructor(opts?: Partial<FosscordServerOptions>) {
		// @ts-ignore
		super({ ...opts, errorHandler: false, jsonBody: false });
	}

	async setupSchema() {
		return Promise.all([
			db.collection("users").createIndex({ id: 1 }, { unique: true }),
			db.collection("messages").createIndex({ id: 1 }, { unique: true }),
			db.collection("channels").createIndex({ id: 1 }, { unique: true }),
			db.collection("guilds").createIndex({ id: 1 }, { unique: true }),
			db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }),
			db.collection("roles").createIndex({ id: 1 }, { unique: true }),
			db.collection("emojis").createIndex({ id: 1 }, { unique: true }),
			db.collection("invites").createIndex({ code: 1 }, { unique: true }),
			db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }) // after 0 seconds of expires_at the invite will get delete
		]);
	}

	async start() {
		// @ts-ignore
		await (db as Promise<Connection>);
		await this.setupSchema();
		console.log("[DB] connected");
		await Config.init();

		this.app.use(GlobalRateLimit);
		this.app.use(Authentication);
		this.app.use(CORS);
		this.app.use(BodyParser({ inflate: true }));
		const languages = await fs.readdir(path.join(__dirname, "..", "locales"));
		const namespaces = await fs.readdir(path.join(__dirname, "..", "locales", "en"));
		const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5));

		await i18next
			.use(i18nextBackend)
			.use(i18nextMiddleware.LanguageDetector)
			.init({
				preload: languages,
				// debug: true,
				fallbackLng: "en",
				ns,
				backend: {
					loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json"
				},
				load: "all"
			});
		this.app.use(i18nextMiddleware.handle(i18next, {}));

		const app = this.app;
		const prefix = Router();
		// @ts-ignore
		this.app = prefix;

		this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/"));
		app.use("/api/v8", prefix);
		this.app = app;
		this.app.use(ErrorHandler);
		const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html"), { encoding: "utf8" });

		this.app.use("/assets", express.static(path.join(__dirname, "..", "assets")));

		this.app.get("/assets/:file", async (req, res) => {
			delete req.headers.host;
			var response: Response;
			var buffer: Buffer;
			const cache = assetCache.get(req.params.file);
			if (!cache) {
				response = await fetch(`https://discord.com/assets/${req.params.file}`, {
					// @ts-ignore
					headers: {
						...req.headers
					}
				});
				buffer = await response.buffer();
			} else {
				response = cache.response;
				buffer = cache.buffer;
			}

			response.headers.forEach((value, name) => {
				if (
					[
						"content-length",
						"content-security-policy",
						"strict-transport-security",
						"set-cookie",
						"transfer-encoding",
						"expect-ct",
						"access-control-allow-origin",
						"content-encoding"
					].includes(name.toLowerCase())
				) {
					return;
				}
				res.set(name, value);
			});
			assetCache.set(req.params.file, { buffer, response });

			return res.send(buffer);
		});
		this.app.get("*", (req, res) => {
			res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
			res.set("content-type", "text/html");
			res.send(
				indexHTML.replace(
					/CDN_HOST: ".+"/,
					`CDN_HOST: "${(Config.get().cdn.endpoint || "http://localhost:3003").replace(/https?:/, "")}"`
				)
			);
		});
		return super.start();
	}
}