summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/Server.ts49
-rw-r--r--src/middlewares/Authentication.ts1
2 files changed, 45 insertions, 5 deletions
diff --git a/src/Server.ts b/src/Server.ts

index 0c92e1b7..3bc346e3 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -10,6 +10,8 @@ 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 { Router } from "express"; +import fetch from "node-fetch"; export interface DiscordServerOptions extends ServerOptions {} @@ -69,15 +71,52 @@ export class DiscordServer extends Server { }); this.app.use(i18nextMiddleware.handle(i18next, {})); + const app = this.app; + const prefix = Router(); + // @ts-ignore + this.app = prefix; + this.routes = await this.registerRoutes(__dirname + "/routes/"); + app.use("/api/v8", prefix); + this.app = app; this.app.use(ErrorHandler); const indexHTML = await fs.readFile(__dirname + "/../client_test/index.html"); - // this.app.get("*", (req, res) => { - // res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); - // res.set("content-type", "text/html"); - // res.send(indexHTML); - // }); + this.app.get("/assets/:file", async (req, res) => { + delete req.headers.host; + const response = await fetch(`https://discord.com/assets/${req.params.file}`, { + // @ts-ignore + headers: { + ...req.headers, + }, + }); + const buffer = await response.text(); + + 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); + }); + + 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); + }); return super.start(); } } diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts
index 65d5a2cf..4bfa219a 100644 --- a/src/middlewares/Authentication.ts +++ b/src/middlewares/Authentication.ts
@@ -14,6 +14,7 @@ declare global { } export async function Authentication(req: Request, res: Response, next: NextFunction) { + if (!req.url.startsWith("/api")) return next(); if (NO_AUTHORIZATION_ROUTES.some((x) => req.url.startsWith(x))) return next(); if (!req.headers.authorization) return next(new HTTPError("Missing Authorization Header", 401)); // TODO: check if user is banned/token expired