summary refs log tree commit diff
path: root/src/api/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/middlewares')
-rw-r--r--src/api/middlewares/Authentication.ts5
-rw-r--r--src/api/middlewares/BodyParser.ts2
-rw-r--r--src/api/middlewares/ErrorHandler.ts3
-rw-r--r--src/api/middlewares/RateLimit.ts9
-rw-r--r--src/api/middlewares/TestClient.ts71
-rw-r--r--src/api/middlewares/Translation.ts4
6 files changed, 46 insertions, 48 deletions
diff --git a/src/api/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts
index 2d9ccf57..6d063953 100644
--- a/src/api/middlewares/Authentication.ts
+++ b/src/api/middlewares/Authentication.ts
@@ -1,6 +1,5 @@
+import { checkToken, Config, HTTPError, Rights } from "@fosscord/util";
 import { NextFunction, Request, Response } from "express";
-import { HTTPError } from "@fosscord/util";
-import { checkToken, Config, Rights } from "@fosscord/util";
 
 export const NO_AUTHORIZATION_ROUTES = [
 	// Authentication routes
@@ -10,7 +9,7 @@ export const NO_AUTHORIZATION_ROUTES = [
 	"/auth/mfa/totp",
 	// Routes with a seperate auth system
 	"/webhooks/",
-	// Public information endpoints 
+	// Public information endpoints
 	"/ping",
 	"/gateway",
 	"/experiments",
diff --git a/src/api/middlewares/BodyParser.ts b/src/api/middlewares/BodyParser.ts
index 35db3c6f..36d89da7 100644
--- a/src/api/middlewares/BodyParser.ts
+++ b/src/api/middlewares/BodyParser.ts
@@ -1,6 +1,6 @@
+import { HTTPError } from "@fosscord/util";
 import bodyParser, { OptionsJson } from "body-parser";
 import { NextFunction, Request, Response } from "express";
-import { HTTPError } from "@fosscord/util";
 
 export function BodyParser(opts?: OptionsJson) {
 	const jsonParser = bodyParser.json(opts);
diff --git a/src/api/middlewares/ErrorHandler.ts b/src/api/middlewares/ErrorHandler.ts
index 8a046e06..813adc18 100644
--- a/src/api/middlewares/ErrorHandler.ts
+++ b/src/api/middlewares/ErrorHandler.ts
@@ -1,6 +1,5 @@
+import { ApiError, FieldError, HTTPError } from "@fosscord/util";
 import { NextFunction, Request, Response } from "express";
-import { HTTPError } from "@fosscord/util";
-import { ApiError, FieldError } from "@fosscord/util";
 const EntityNotFoundErrorRegex = /"(\w+)"/;
 
 export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
diff --git a/src/api/middlewares/RateLimit.ts b/src/api/middlewares/RateLimit.ts
index 47180b62..dc93dcef 100644
--- a/src/api/middlewares/RateLimit.ts
+++ b/src/api/middlewares/RateLimit.ts
@@ -1,6 +1,6 @@
-import { Config, getRights, listenEvent, Rights } from "@fosscord/util";
-import { NextFunction, Request, Response, Router } from "express";
 import { getIpAdress } from "@fosscord/api";
+import { Config, getRights, listenEvent } from "@fosscord/util";
+import { NextFunction, Request, Response, Router } from "express";
 import { API_PREFIX_TRAILING_SLASH } from "./Authentication";
 
 // Docs: https://discord.com/developers/docs/topics/rate-limits
@@ -48,7 +48,7 @@ export default function rateLimit(opts: {
 		// exempt user? if so, immediately short circuit
 		if (req.user_id) {
 			const rights = await getRights(req.user_id);
-			if (rights.has("BYPASS_RATE_LIMITS")) return;
+			if (rights.has("BYPASS_RATE_LIMITS")) return next();
 		}
 
 		const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, "");
@@ -121,6 +121,7 @@ export default function rateLimit(opts: {
 export async function initRateLimits(app: Router) {
 	const { routes, global, ip, error, disabled } = Config.get().limits.rate;
 	if (disabled) return;
+	console.log("Enabling rate limits...");
 	await listenEvent(EventRateLimit, (event) => {
 		Cache.set(event.channel_id as string, event.data);
 		event.acknowledge?.();
@@ -163,7 +164,7 @@ export async function initRateLimits(app: Router) {
 	app.use("/auth/register", rateLimit({ onlyIp: true, success: true, ...routes.auth.register }));
 }
 
-async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number; }) {
+async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number }) {
 	const id = opts.executor_id + opts.bucket_id;
 	let limit = Cache.get(id);
 	if (!limit) {
diff --git a/src/api/middlewares/TestClient.ts b/src/api/middlewares/TestClient.ts
index c8ea57f6..2784c8ab 100644
--- a/src/api/middlewares/TestClient.ts
+++ b/src/api/middlewares/TestClient.ts
@@ -1,17 +1,17 @@
-import express, { Request, Response, Application } from "express";
+import { Config } from "@fosscord/util";
+import express, { Application, Request, Response } from "express";
 import fs from "fs";
+import fetch, { Headers, Response as FetchResponse } from "node-fetch";
 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";
+import ProxyAgent from "proxy-agent";
+import { AssetCacheItem } from "../util/entities/AssetCacheItem";
 
-const AssetsPath = path.join(__dirname, "..", "..", "..", "assets")
+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);
@@ -22,31 +22,29 @@ export default function TestClient(app: Application) {
 	//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
+	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)) {
+	console.log(`[TestClient] ${green(`Using asset cache path: ${assetCacheDir}`)}`);
+	if (!fs.existsSync(assetCacheDir)) {
 		fs.mkdirSync(assetCacheDir);
 	}
-	if(fs.existsSync(path.join(assetCacheDir, "index.json"))) {
+	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.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)){
+		if (newAssetCache.has(req.params.file)) {
 			assetCacheItem = newAssetCache.get(req.params.file)!;
 			assetCacheItem.Headers.forEach((value: any, name: any) => {
 				res.set(name, value);
 			});
-		}
-		else {
+		} 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,
@@ -55,7 +53,7 @@ export default function TestClient(app: Application) {
 					...req.headers
 				}
 			});
-			
+
 			//set cache info
 			assetCacheItem.Headers = Object.fromEntries(stripHeaders(response.headers));
 			assetCacheItem.FilePath = path.join(assetCacheDir, req.params.file);
@@ -66,7 +64,7 @@ export default function TestClient(app: Application) {
 			//download file
 			fs.writeFileSync(assetCacheItem.FilePath, await response.buffer());
 		}
-		
+
 		assetCacheItem.Headers.forEach((value: string, name: string) => {
 			res.set(name, value);
 		});
@@ -77,8 +75,8 @@ export default function TestClient(app: Application) {
 		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.")
-		
+		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) => {
@@ -86,23 +84,18 @@ export default function TestClient(app: Application) {
 		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 (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 (!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 || "";
+	const CDN_ENDPOINT = (Config.get()?.cdn.endpointPublic || process.env.CDN || "").replace(/(https?)?(:\/\/?)/g, "");
+	const GATEWAY_ENDPOINT = Config.get()?.gateway.endpointPublic || process.env.GATEWAY || "";
 
 	if (CDN_ENDPOINT) {
 		html = html.replace(/CDN_HOST: .+/, `CDN_HOST: \`${CDN_ENDPOINT}\`,`);
@@ -117,23 +110,29 @@ 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`; });
+	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{
+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`; });
+	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{
+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`; });
+	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);
 }
 
@@ -147,7 +146,7 @@ function stripHeaders(headers: Headers): Headers {
 		"expect-ct",
 		"access-control-allow-origin",
 		"content-encoding"
-	].forEach(headerName => {
+	].forEach((headerName) => {
 		headers.delete(headerName);
 	});
 	return headers;
diff --git a/src/api/middlewares/Translation.ts b/src/api/middlewares/Translation.ts
index 64b03bf8..8e5e67e6 100644
--- a/src/api/middlewares/Translation.ts
+++ b/src/api/middlewares/Translation.ts
@@ -1,9 +1,9 @@
+import { Router } from "express";
 import fs from "fs";
-import path from "path";
 import i18next from "i18next";
 import i18nextMiddleware from "i18next-http-middleware";
 import i18nextBackend from "i18next-node-fs-backend";
-import { Router } from "express";
+import path from "path";
 
 export async function initTranslation(router: Router) {
 	const languages = fs.readdirSync(path.join(__dirname, "..", "..", "..", "assets", "locales"));