2021-02-06 10:09:32 +01:00
|
|
|
import "missing-native-js-functions";
|
2020-11-28 19:31:04 +01:00
|
|
|
import fs from "fs/promises";
|
2021-02-14 19:01:41 +01:00
|
|
|
import { Connection } from "mongoose";
|
2021-01-04 16:14:19 +01:00
|
|
|
import { Server, ServerOptions } from "lambert-server";
|
2021-06-27 23:14:13 +02:00
|
|
|
import { Authentication, CORS } from "./middlewares/";
|
2021-05-24 20:47:06 +02:00
|
|
|
import { Config, db } from "@fosscord/server-util";
|
2021-02-01 21:49:01 +01:00
|
|
|
import i18next from "i18next";
|
2021-02-02 00:51:00 +01:00
|
|
|
import i18nextMiddleware, { I18next } from "i18next-http-middleware";
|
|
|
|
import i18nextBackend from "i18next-node-fs-backend";
|
|
|
|
import { ErrorHandler } from "./middlewares/ErrorHandler";
|
|
|
|
import { BodyParser } from "./middlewares/BodyParser";
|
2021-05-31 21:03:09 +02:00
|
|
|
import express, { Router, Request, Response } from "express";
|
|
|
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
2021-04-26 00:10:20 +02:00
|
|
|
import mongoose from "mongoose";
|
2021-05-24 20:47:06 +02:00
|
|
|
import path from "path";
|
2021-07-01 09:33:54 +02:00
|
|
|
import RateLimit from "./middlewares/RateLimit";
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-04-26 00:10:20 +02:00
|
|
|
// this will return the new updated document for findOneAndUpdate
|
2021-05-01 13:54:12 +02:00
|
|
|
mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate
|
2021-04-26 00:10:20 +02:00
|
|
|
|
2021-05-01 13:54:12 +02:00
|
|
|
export interface FosscordServerOptions extends ServerOptions {}
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
declare global {
|
|
|
|
namespace Express {
|
|
|
|
interface Request {
|
2021-02-02 00:51:00 +01:00
|
|
|
// @ts-ignore
|
2021-04-22 23:29:06 +02:00
|
|
|
server: FosscordServer;
|
2021-01-04 16:14:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-05-29 20:04:09 +02:00
|
|
|
const assetCache = new Map<
|
|
|
|
string,
|
|
|
|
{
|
2021-05-31 21:03:09 +02:00
|
|
|
response: FetchResponse;
|
2021-05-29 20:04:09 +02:00
|
|
|
buffer: Buffer;
|
|
|
|
}
|
|
|
|
>();
|
|
|
|
|
2021-04-22 23:29:06 +02:00
|
|
|
export class FosscordServer extends Server {
|
2021-05-31 21:03:09 +02:00
|
|
|
public declare options: FosscordServerOptions;
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-04-22 23:29:06 +02:00
|
|
|
constructor(opts?: Partial<FosscordServerOptions>) {
|
2021-02-02 00:51:00 +01:00
|
|
|
// @ts-ignore
|
|
|
|
super({ ...opts, errorHandler: false, jsonBody: false });
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 19:01:41 +01:00
|
|
|
async setupSchema() {
|
2021-05-22 22:55:45 +02:00
|
|
|
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 }),
|
2021-07-01 09:33:54 +02:00
|
|
|
db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }), // after 0 seconds of expires_at the invite will get delete
|
|
|
|
db.collection("ratelimits").createIndex({ created_at: 1 }, { expireAfterSeconds: 1000 })
|
2021-05-22 22:55:45 +02:00
|
|
|
]);
|
2021-02-14 19:01:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 16:14:19 +01:00
|
|
|
async start() {
|
2021-02-14 19:01:41 +01:00
|
|
|
// @ts-ignore
|
2021-02-16 21:20:07 +01:00
|
|
|
await (db as Promise<Connection>);
|
2021-02-14 19:01:41 +01:00
|
|
|
await this.setupSchema();
|
2021-01-30 19:58:15 +01:00
|
|
|
console.log("[DB] connected");
|
2021-05-24 20:47:06 +02:00
|
|
|
await Config.init();
|
2021-01-30 19:58:15 +01:00
|
|
|
|
2021-05-01 13:54:12 +02:00
|
|
|
this.app.use(CORS);
|
2021-06-25 15:54:13 +02:00
|
|
|
this.app.use(Authentication);
|
2021-07-01 09:33:54 +02:00
|
|
|
this.app.use(RateLimit({ count: 10, error: 10, window: 5 }));
|
2021-05-30 01:53:01 +02:00
|
|
|
this.app.use(BodyParser({ inflate: true, limit: 1024 * 1024 * 2 }));
|
2021-05-24 20:47:06 +02:00
|
|
|
const languages = await fs.readdir(path.join(__dirname, "..", "locales"));
|
|
|
|
const namespaces = await fs.readdir(path.join(__dirname, "..", "locales", "en"));
|
2021-02-01 21:49:01 +01:00
|
|
|
const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5));
|
|
|
|
|
2021-02-02 00:51:00 +01:00
|
|
|
await i18next
|
|
|
|
.use(i18nextBackend)
|
|
|
|
.use(i18nextMiddleware.LanguageDetector)
|
|
|
|
.init({
|
|
|
|
preload: languages,
|
|
|
|
// debug: true,
|
|
|
|
fallbackLng: "en",
|
|
|
|
ns,
|
|
|
|
backend: {
|
2021-05-22 22:55:45 +02:00
|
|
|
loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json"
|
2021-02-02 00:51:00 +01:00
|
|
|
},
|
2021-05-22 22:55:45 +02:00
|
|
|
load: "all"
|
2021-02-02 00:51:00 +01:00
|
|
|
});
|
2021-02-01 21:49:01 +01:00
|
|
|
this.app.use(i18nextMiddleware.handle(i18next, {}));
|
2021-01-30 19:58:15 +01:00
|
|
|
|
2021-04-05 21:43:11 +02:00
|
|
|
const app = this.app;
|
|
|
|
const prefix = Router();
|
|
|
|
// @ts-ignore
|
|
|
|
this.app = prefix;
|
2021-07-01 09:33:54 +02:00
|
|
|
prefix.use("/guilds/:id", RateLimit({ count: 10, window: 5 }));
|
|
|
|
prefix.use("/webhooks/:id", RateLimit({ count: 10, window: 5 }));
|
|
|
|
prefix.use("/channels/:id", RateLimit({ count: 10, window: 5 }));
|
2021-04-05 21:43:11 +02:00
|
|
|
|
2021-05-24 20:57:22 +02:00
|
|
|
this.routes = await this.registerRoutes(path.join(__dirname, "routes", "/"));
|
2021-06-19 16:49:49 +02:00
|
|
|
app.use("/api", prefix); // allow unversioned requests
|
2021-04-05 21:43:11 +02:00
|
|
|
app.use("/api/v8", prefix);
|
2021-06-27 23:30:36 +02:00
|
|
|
app.use("/api/v9", prefix);
|
2021-04-05 21:43:11 +02:00
|
|
|
this.app = app;
|
2021-02-02 00:51:00 +01:00
|
|
|
this.app.use(ErrorHandler);
|
2021-05-30 01:44:15 +02:00
|
|
|
const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html"), { encoding: "utf8" });
|
2021-05-24 20:47:06 +02:00
|
|
|
|
|
|
|
this.app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
|
2020-11-28 19:31:04 +01:00
|
|
|
|
2021-05-31 21:03:09 +02:00
|
|
|
this.app.get("/assets/:file", async (req: Request, res: Response) => {
|
2021-04-05 21:43:11 +02:00
|
|
|
delete req.headers.host;
|
2021-05-31 21:03:09 +02:00
|
|
|
var response: FetchResponse;
|
2021-05-29 20:04:09 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-04-05 21:43:11 +02:00
|
|
|
|
|
|
|
response.headers.forEach((value, name) => {
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
"content-length",
|
|
|
|
"content-security-policy",
|
|
|
|
"strict-transport-security",
|
|
|
|
"set-cookie",
|
|
|
|
"transfer-encoding",
|
|
|
|
"expect-ct",
|
|
|
|
"access-control-allow-origin",
|
2021-05-22 22:55:45 +02:00
|
|
|
"content-encoding"
|
2021-04-05 21:43:11 +02:00
|
|
|
].includes(name.toLowerCase())
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.set(name, value);
|
|
|
|
});
|
2021-05-29 20:04:09 +02:00
|
|
|
assetCache.set(req.params.file, { buffer, response });
|
2021-04-05 21:43:11 +02:00
|
|
|
|
|
|
|
return res.send(buffer);
|
|
|
|
});
|
2021-05-31 21:03:09 +02:00
|
|
|
this.app.get("*", (req: Request, res: Response) => {
|
2021-04-05 21:43:11 +02:00
|
|
|
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
|
|
|
|
res.set("content-type", "text/html");
|
2021-05-30 01:44:15 +02:00
|
|
|
res.send(
|
|
|
|
indexHTML.replace(
|
|
|
|
/CDN_HOST: ".+"/,
|
|
|
|
`CDN_HOST: "${(Config.get().cdn.endpoint || "http://localhost:3003").replace(/https?:/, "")}"`
|
|
|
|
)
|
|
|
|
);
|
2021-04-05 21:43:11 +02:00
|
|
|
});
|
2021-01-04 16:14:19 +01:00
|
|
|
return super.start();
|
2020-11-28 19:31:04 +01:00
|
|
|
}
|
|
|
|
}
|