From fb4c1f9b4e02573d0aa5fd40c527f36e3a6bc098 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Tue, 9 Jul 2024 11:48:05 -0400 Subject: [PATCH] Rework rate limits --- config/ratelimits.yml | 11 +++++---- src/channel/Channel.ts | 4 ++++ src/ws/Socket.ts | 33 +++++++++++++++++++++----- src/ws/events/user/handlers/kickban.ts | 6 ++++- src/ws/ratelimit/config.ts | 10 +++++--- src/ws/ratelimit/limits/admin.ts | 3 ++- src/ws/ratelimit/limits/user.ts | 7 +++--- 7 files changed, 55 insertions(+), 19 deletions(-) diff --git a/config/ratelimits.yml b/config/ratelimits.yml index b46174f..2063bf8 100644 --- a/config/ratelimits.yml +++ b/config/ratelimits.yml @@ -3,25 +3,28 @@ user: a: 1500 m: 50 ch: 1000 + kickban: 250 chains: userset: - interval: 1800000, + interval: 1800000 num: 1000 crown: normal: a: 600 m: 50 ch: 1000 + kickban: 250 chains: userset: - interval: 1800000, + interval: 1800000 num: 1000 admin: normal: a: 120 - m: 16.6666666667 + m: 16.666666666666668 ch: 100 + kickban: 31.25 chains: userset: - interval: 1800000, + interval: 1800000 num: 1000 diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index d503197..cc5bbd1 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -17,6 +17,7 @@ import { ChannelList } from "./ChannelList"; import { config } from "./config"; import { saveChatHistory, getChatHistory } from "../data/history"; import { mixin } from "../util/helpers"; +import { NoteQuota } from "../ws/ratelimit/NoteQuota"; interface CachedKickban { userId: string; @@ -106,6 +107,9 @@ export class Channel extends EventEmitter { this.getInfo(), this.getParticipantList() ); + + // Set their rate limits to match channel status + socket.resetRateLimits(); } } } diff --git a/src/ws/Socket.ts b/src/ws/Socket.ts index 270b4bc..cfde1c3 100644 --- a/src/ws/Socket.ts +++ b/src/ws/Socket.ts @@ -29,6 +29,7 @@ import { adminLimits } from "./ratelimit/limits/admin"; import { userLimits } from "./ratelimit/limits/user"; import { NoteQuota } from "./ratelimit/NoteQuota"; import { config } from "./usersConfig"; +import { crownLimits } from "./ratelimit/limits/crown"; const logger = new Logger("Sockets"); @@ -103,10 +104,8 @@ export class Socket extends EventEmitter { (async () => { await this.loadUser(); - // TODO Permissions - let isAdmin = false; - - this.setRateLimits(isAdmin ? adminLimits : userLimits); + this.resetRateLimits(); + this.setNoteQuota(NoteQuota.PARAMS_RIDICULOUS); this.bindEventListeners(); })(); @@ -400,10 +399,32 @@ export class Socket extends EventEmitter { for (const key of Object.keys(list.chains)) { (this.rateLimits.chains as any)[key] = (list.chains as any)[key](); } + } - this.noteQuota.setParams(NoteQuota.PARAMS_NORMAL as any); + public resetRateLimits() { + // TODO Permissions + let isAdmin = false; + let ch = this.getCurrentChannel(); - // Send note quota + if (isAdmin) { + this.setRateLimits(adminLimits); + this.setNoteQuota(NoteQuota.PARAMS_OFFLINE); + } else if (this.isOwner()) { + this.setRateLimits(crownLimits); + this.setNoteQuota(NoteQuota.PARAMS_RIDICULOUS); + } else if (ch && ch.isLobby()) { + this.setRateLimits(userLimits) + this.setNoteQuota(NoteQuota.PARAMS_LOBBY); + } else { + this.setRateLimits(userLimits); + this.setNoteQuota(NoteQuota.PARAMS_NORMAL); + } + } + + public setNoteQuota(params = NoteQuota.PARAMS_NORMAL) { + this.noteQuota.setParams(params as any); // TODO why any + + // Send note quota to client this.sendArray([ { m: "nq", diff --git a/src/ws/events/user/handlers/kickban.ts b/src/ws/events/user/handlers/kickban.ts index 9168f6b..666d7ed 100644 --- a/src/ws/events/user/handlers/kickban.ts +++ b/src/ws/events/user/handlers/kickban.ts @@ -3,9 +3,13 @@ import { ServerEventListener } from "../../../../util/types"; export const kickban: ServerEventListener<"kickban"> = { id: "kickban", callback: (msg, socket) => { - // Kickbanning some asshole from channel + // Kickbanning someone from channel if (typeof msg._id !== "string") return; if (typeof msg.ms !== "number") return; + + if (socket.rateLimits) + if (!socket.rateLimits.normal.kickban.attempt()) return; + socket.kickban(msg._id, msg.ms); } }; diff --git a/src/ws/ratelimit/config.ts b/src/ws/ratelimit/config.ts index d79b32c..1ab1cf3 100644 --- a/src/ws/ratelimit/config.ts +++ b/src/ws/ratelimit/config.ts @@ -10,6 +10,7 @@ export interface RateLimitConfigList< m: RL; a: RL; ch: RL; + kickban: RL; }; chains: { @@ -35,7 +36,8 @@ export const config = loadConfig("config/ratelimits.yml", { normal: { a: 6000 / 4, m: 1000 / 20, - ch: 1000 / 1 + ch: 1000 / 1, + kickban: 1000 / 4 }, chains: { userset: { @@ -48,7 +50,8 @@ export const config = loadConfig("config/ratelimits.yml", { normal: { a: 6000 / 10, m: 1000 / 20, - ch: 1000 / 1 + ch: 1000 / 1, + kickban: 1000 / 4 }, chains: { userset: { @@ -61,7 +64,8 @@ export const config = loadConfig("config/ratelimits.yml", { normal: { a: 6000 / 50, m: 1000 / 60, - ch: 1000 / 10 + ch: 1000 / 10, + kickban: 1000 / 32 }, chains: { userset: { diff --git a/src/ws/ratelimit/limits/admin.ts b/src/ws/ratelimit/limits/admin.ts index 75a83a6..817d205 100644 --- a/src/ws/ratelimit/limits/admin.ts +++ b/src/ws/ratelimit/limits/admin.ts @@ -6,7 +6,8 @@ export const adminLimits: RateLimitConstructorList = { normal: { a: () => new RateLimit(config.admin.normal.a), m: () => new RateLimit(config.admin.normal.m), - ch: () => new RateLimit(config.admin.normal.ch) + ch: () => new RateLimit(config.admin.normal.ch), + kickban: () => new RateLimit(config.admin.normal.kickban) }, chains: { userset: () => diff --git a/src/ws/ratelimit/limits/user.ts b/src/ws/ratelimit/limits/user.ts index 5904696..27c0210 100644 --- a/src/ws/ratelimit/limits/user.ts +++ b/src/ws/ratelimit/limits/user.ts @@ -2,14 +2,13 @@ import { RateLimit } from "../RateLimit"; import { RateLimitChain } from "../RateLimitChain"; import { RateLimitConstructorList, config } from "../config"; -// I have no idea why these things exist but I made it apparently -// All it does it construct the rate limits from the config instead -// of using random numbers I found on the internet +// All this does is instantiate rate limits from the config export const userLimits: RateLimitConstructorList = { normal: { a: () => new RateLimit(config.user.normal.a), m: () => new RateLimit(config.user.normal.m), - ch: () => new RateLimit(config.user.normal.ch) + ch: () => new RateLimit(config.user.normal.ch), + kickban: () => new RateLimit(config.user.normal.kickban) }, chains: { userset: () =>