summary refs log tree commit diff
path: root/src/api/middlewares
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/api/middlewares/Authentication.ts (renamed from api/src/middlewares/Authentication.ts)3
-rw-r--r--src/api/middlewares/BodyParser.ts (renamed from api/src/middlewares/BodyParser.ts)2
-rw-r--r--src/api/middlewares/CORS.ts (renamed from api/src/middlewares/CORS.ts)0
-rw-r--r--src/api/middlewares/ErrorHandler.ts (renamed from api/src/middlewares/ErrorHandler.ts)2
-rw-r--r--src/api/middlewares/RateLimit.ts (renamed from api/src/middlewares/RateLimit.ts)10
-rw-r--r--src/api/middlewares/TestClient.ts154
-rw-r--r--src/api/middlewares/Translation.ts (renamed from api/src/middlewares/Translation.ts)6
-rw-r--r--src/api/middlewares/index.ts (renamed from api/src/middlewares/index.ts)0
8 files changed, 166 insertions, 11 deletions
diff --git a/api/src/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts

index 5a08caf3..2d9ccf57 100644 --- a/api/src/middlewares/Authentication.ts +++ b/src/api/middlewares/Authentication.ts
@@ -1,5 +1,5 @@ import { NextFunction, Request, Response } from "express"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from "@fosscord/util"; import { checkToken, Config, Rights } from "@fosscord/util"; export const NO_AUTHORIZATION_ROUTES = [ @@ -7,6 +7,7 @@ export const NO_AUTHORIZATION_ROUTES = [ "/auth/login", "/auth/register", "/auth/location-metadata", + "/auth/mfa/totp", // Routes with a seperate auth system "/webhooks/", // Public information endpoints diff --git a/api/src/middlewares/BodyParser.ts b/src/api/middlewares/BodyParser.ts
index 4cb376bc..35db3c6f 100644 --- a/api/src/middlewares/BodyParser.ts +++ b/src/api/middlewares/BodyParser.ts
@@ -1,6 +1,6 @@ import bodyParser, { OptionsJson } from "body-parser"; import { NextFunction, Request, Response } from "express"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from "@fosscord/util"; export function BodyParser(opts?: OptionsJson) { const jsonParser = bodyParser.json(opts); diff --git a/api/src/middlewares/CORS.ts b/src/api/middlewares/CORS.ts
index 20260cf9..20260cf9 100644 --- a/api/src/middlewares/CORS.ts +++ b/src/api/middlewares/CORS.ts
diff --git a/api/src/middlewares/ErrorHandler.ts b/src/api/middlewares/ErrorHandler.ts
index 2012b91c..8a046e06 100644 --- a/api/src/middlewares/ErrorHandler.ts +++ b/src/api/middlewares/ErrorHandler.ts
@@ -1,5 +1,5 @@ import { NextFunction, Request, Response } from "express"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from "@fosscord/util"; import { ApiError, FieldError } from "@fosscord/util"; const EntityNotFoundErrorRegex = /"(\w+)"/; diff --git a/api/src/middlewares/RateLimit.ts b/src/api/middlewares/RateLimit.ts
index 13f1602c..47180b62 100644 --- a/api/src/middlewares/RateLimit.ts +++ b/src/api/middlewares/RateLimit.ts
@@ -28,7 +28,7 @@ type RateLimit = { expires_at: Date; }; -var Cache = new Map<string, RateLimit>(); +let Cache = new Map<string, RateLimit>(); const EventRateLimit = "RATELIMIT"; export default function rateLimit(opts: { @@ -52,10 +52,10 @@ export default function rateLimit(opts: { } const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, ""); - var executor_id = getIpAdress(req); + let executor_id = getIpAdress(req); if (!opts.onlyIp && req.user_id) executor_id = req.user_id; - var max_hits = opts.count; + let max_hits = opts.count; if (opts.bot && req.user_bot) max_hits = opts.bot; if (opts.GET && ["GET", "OPTIONS", "HEAD"].includes(req.method)) max_hits = opts.GET; else if (opts.MODIFY && ["POST", "DELETE", "PATCH", "PUT"].includes(req.method)) max_hits = opts.MODIFY; @@ -165,7 +165,7 @@ export async function initRateLimits(app: Router) { async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number; }) { const id = opts.executor_id + opts.bucket_id; - var limit = Cache.get(id); + let limit = Cache.get(id); if (!limit) { limit = { id: opts.bucket_id, @@ -183,7 +183,7 @@ async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits } /* - var ratelimit = await RateLimit.findOne({ id: opts.bucket_id, executor_id: opts.executor_id }); + let ratelimit = await RateLimit.findOne({ where: { id: opts.bucket_id, executor_id: opts.executor_id } }); if (!ratelimit) { ratelimit = new RateLimit({ id: opts.bucket_id, diff --git a/src/api/middlewares/TestClient.ts b/src/api/middlewares/TestClient.ts new file mode 100644
index 00000000..c8ea57f6 --- /dev/null +++ b/src/api/middlewares/TestClient.ts
@@ -0,0 +1,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; +} diff --git a/api/src/middlewares/Translation.ts b/src/api/middlewares/Translation.ts
index baabf221..64b03bf8 100644 --- a/api/src/middlewares/Translation.ts +++ b/src/api/middlewares/Translation.ts
@@ -6,8 +6,8 @@ import i18nextBackend from "i18next-node-fs-backend"; import { Router } from "express"; export async function initTranslation(router: Router) { - const languages = fs.readdirSync(path.join(__dirname, "..", "..", "locales")); - const namespaces = fs.readdirSync(path.join(__dirname, "..", "..", "locales", "en")); + const languages = fs.readdirSync(path.join(__dirname, "..", "..", "..", "assets", "locales")); + const namespaces = fs.readdirSync(path.join(__dirname, "..", "..", "..", "assets", "locales", "en")); const ns = namespaces.filter((x) => x.endsWith(".json")).map((x) => x.slice(0, x.length - 5)); await i18next @@ -19,7 +19,7 @@ export async function initTranslation(router: Router) { fallbackLng: "en", ns, backend: { - loadPath: __dirname + "/../../locales/{{lng}}/{{ns}}.json" + loadPath: __dirname + "/../../../assets/locales/{{lng}}/{{ns}}.json" }, load: "all" }); diff --git a/api/src/middlewares/index.ts b/src/api/middlewares/index.ts
index f0c50dbe..f0c50dbe 100644 --- a/api/src/middlewares/index.ts +++ b/src/api/middlewares/index.ts