From 14a31ad143c37502b45e4868f95ae25877d4c717 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 28 Jun 2021 18:43:50 +0200 Subject: [PATCH] :construction: rate limit --- package.json | 7 +++---- src/middlewares/RateLimit.ts | 31 +++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index e66a588c..0b1adb3b 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "atomically": "^1.7.0", "bcrypt": "^5.0.1", "body-parser": "^1.19.0", - "canvas": "^2.8.0", "cheerio": "^1.0.0-rc.9", "dot-prop": "^6.0.1", "dotenv": "^8.2.0", @@ -50,13 +49,15 @@ "i18next-http-middleware": "^3.1.3", "i18next-node-fs-backend": "^2.1.3", "image-size": "^1.0.0", + "ipdata": "^1.1.3", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.2.5", "missing-native-js-functions": "^1.2.6", "mongoose": "^5.12.3", "mongoose-autopopulate": "^0.12.3", "mongoose-long": "^0.3.2", - "multer": "^1.4.2" + "multer": "^1.4.2", + "node-fetch": "^2.6.1" }, "devDependencies": { "@types/bcrypt": "^3.0.0", @@ -68,9 +69,7 @@ "@types/node-fetch": "^2.5.7", "@zerollup/ts-transform-paths": "^1.7.18", "0x": "^4.10.2", - "ipdata": "^1.1.3", "jest": "^26.6.3", - "node-fetch": "^2.6.1", "ts-node": "^9.1.1", "ts-node-dev": "^1.1.6", "typescript": "^4.1.2" diff --git a/src/middlewares/RateLimit.ts b/src/middlewares/RateLimit.ts index 24f4013f..e610d55b 100644 --- a/src/middlewares/RateLimit.ts +++ b/src/middlewares/RateLimit.ts @@ -1,8 +1,31 @@ import { db, MongooseCache } from "@fosscord/server-util"; -import { NextFunction } from "express"; +import { NextFunction, Request, Response } from "express"; -const Cache = new MongooseCache(db.collection("ratelimit"), [], { onlyEvents: false }); +const Cache = new MongooseCache(db.collection("ratelimits"), [{ $match: { blocked: true } }], { onlyEvents: false, array: true }); -export default function RateLimit({}) { - return async (req: Request, res: Response, next: NextFunction) => {}; +// Docs: https://discord.com/developers/docs/topics/rate-limits + +/* +? bucket limit? Max actions/sec per bucket? + +TODO: ip rate limit +TODO: user rate limit +TODO: different rate limit for bots/user/oauth/webhook +TODO: delay database requests to include multiple queries +TODO: different for methods (GET/POST) +TODO: bucket major parameters (channel_id, guild_id, webhook_id) +TODO: use config values + +> IP addresses that make too many invalid HTTP requests are automatically and temporarily restricted from accessing the Discord API. Currently, this limit is 10,000 per 10 minutes. An invalid request is one that results in 401, 403, or 429 statuses. + +> All bots can make up to 50 requests per second to our API. This is independent of any individual rate limit on a route. If your bot gets big enough, based on its functionality, it may be impossible to stay below 50 requests per second during normal operations. + +*/ + +export default function RateLimit(opts: { bucket?: string; window: number; count: number }) { + Cache.init(); // will only initalize it once + + return async (req: Request, res: Response, next: NextFunction) => { + next(); + }; }