diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2022-09-18 14:09:16 +0200 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-12-19 22:17:06 +1100 |
commit | bdda215005b57312512e08cbbdad1286e372c1b7 (patch) | |
tree | 13cf4847dfa9b497d6b3b7d6716f73b5917c1795 /src | |
parent | Registration tokens (diff) | |
download | server-bdda215005b57312512e08cbbdad1286e372c1b7.tar.xz |
Message rate limiting
Diffstat (limited to 'src')
-rw-r--r-- | src/api/routes/channels/#channel_id/messages/index.ts | 23 | ||||
-rw-r--r-- | src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts | 2 |
2 files changed, 23 insertions, 2 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 4b935f19..86864edb 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -6,6 +6,7 @@ import { Config, DmChannelDTO, emitEvent, + FieldErrors, getPermission, Message, MessageCreateEvent, @@ -16,10 +17,13 @@ import { MessageCreateSchema, ReadState, DiscordApiErrors, + getRights, + Rights, } from "@fosscord/util"; import { HTTPError } from "lambert-server"; -import { handleMessage, postHandleMessage, route } from "@fosscord/api"; +import { handleMessage, postHandleMessage, route, getIpAdress } from "@fosscord/api"; import multer from "multer"; +import { yellow } from "picocolors"; import { FindManyOptions, LessThan, MoreThan } from "typeorm"; import { URL } from "url"; @@ -215,6 +219,23 @@ router.post( } } + if (!req.rights.has(Rights.FLAGS.BYPASS_RATE_LIMITS)) { + var limits = Config.get().limits; + if (limits.absoluteRate.register.enabled) { + const count = await Message.count({ + where: { + channel_id, + timestamp: MoreThan(new Date(Date.now() - limits.absoluteRate.sendMessage.window)) + } + }); + + if (count >= limits.absoluteRate.sendMessage.limit) + throw FieldErrors({ + channel_id: { code: "TOO_MANY_MESSAGES", message: req.t("common:toomany.MESSAGE") } + }); + } + } + const files = (req.files as Express.Multer.File[]) ?? []; for (var currFile of files) { try { diff --git a/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts b/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts index 85280d3a..4029abbe 100644 --- a/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts +++ b/src/util/config/types/subconfigurations/limits/GlobalRateLimits.ts @@ -1,6 +1,6 @@ export class GlobalRateLimits { register: GlobalRateLimit = { limit: 25, window: 60 * 60 * 1000, enabled: true }; - sendMessage: GlobalRateLimit = { limit: 50, window: 60 * 1000, enabled: true }; + sendMessage: GlobalRateLimit = { limit: 200, window: 60 * 1000, enabled: true }; } export class GlobalRateLimit { |