summary refs log tree commit diff
path: root/src/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'src/middlewares')
-rw-r--r--src/middlewares/Authentication.ts1
-rw-r--r--src/middlewares/RateLimit.ts6
-rw-r--r--src/middlewares/TestClient.ts66
3 files changed, 70 insertions, 3 deletions
diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts

index 76b335ad..01b7ef57 100644 --- a/src/middlewares/Authentication.ts +++ b/src/middlewares/Authentication.ts
@@ -6,6 +6,7 @@ export const NO_AUTHORIZATION_ROUTES = [ /^\/api(\/v\d+)?\/auth\/login/, /^\/api(\/v\d+)?\/auth\/register/, /^\/api(\/v\d+)?\/webhooks\//, + /^\/api(\/v\d+)?\/ping/, /^\/api(\/v\d+)?\/gateway/, /^\/api(\/v\d+)?\/experiments/, /^\/api(\/v\d+)?\/guilds\/\d+\/widget\.(json|png)/ diff --git a/src/middlewares/RateLimit.ts b/src/middlewares/RateLimit.ts
index 89e002df..088c3161 100644 --- a/src/middlewares/RateLimit.ts +++ b/src/middlewares/RateLimit.ts
@@ -1,5 +1,5 @@ import { db, MongooseCache, Bucket } from "@fosscord/server-util"; -import { NextFunction, Request, Response } from "express"; +import { IRouterHandler, NextFunction, Request, Response } from "express"; import { getIpAdress } from "../util/ipAddress"; import { API_PREFIX_TRAILING_SLASH } from "./Authentication"; @@ -43,10 +43,10 @@ export default function RateLimit(opts: { error?: boolean; success?: boolean; onylIp?: boolean; -}) { +}): any { Cache.init(); // will only initalize it once - return async (req: Request, res: Response, next: NextFunction) => { + return async (req: Request, res: Response, next: NextFunction): Promise<any> => { const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, ""); var user_id = getIpAdress(req); if (!opts.onylIp && req.user_id) user_id = req.user_id; diff --git a/src/middlewares/TestClient.ts b/src/middlewares/TestClient.ts new file mode 100644
index 00000000..4e3c9de3 --- /dev/null +++ b/src/middlewares/TestClient.ts
@@ -0,0 +1,66 @@ +import bodyParser, { OptionsJson } from "body-parser"; +import express, { NextFunction, Request, Response, Application } from "express"; +import { HTTPError } from "lambert-server"; +import fs from "fs"; +import path from "path"; +import fetch, { Response as FetchResponse } from "node-fetch"; +import { Config } from "@fosscord/server-util"; + +export default function TestClient(app: Application) { + const assetCache = new Map<string, { response: FetchResponse; buffer: Buffer }>(); + const indexHTML = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" }); + + app.use("/assets", express.static(path.join(__dirname, "..", "assets"))); + + app.get("/assets/:file", async (req: Request, res: Response) => { + delete req.headers.host; + var response: FetchResponse; + 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); + }); + app.get("*", (req: Request, res: Response) => { + res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); + res.set("content-type", "text/html"); + var html = indexHTML; + const CDN_ENDPOINT = (Config.get()?.cdn.endpoint || process.env.CDN || "").replace(/(https?)?(:\/\/?)/g, ""); + const GATEWAY_ENDPOINT = Config.get()?.gateway.endpoint || 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}",`); + + res.send(html); + }); +}