Rework rate limits

This commit is contained in:
Hri7566 2024-07-09 11:48:05 -04:00
parent d61eeac221
commit fb4c1f9b4e
7 changed files with 55 additions and 19 deletions

View File

@ -3,25 +3,28 @@ user:
a: 1500 a: 1500
m: 50 m: 50
ch: 1000 ch: 1000
kickban: 250
chains: chains:
userset: userset:
interval: 1800000, interval: 1800000
num: 1000 num: 1000
crown: crown:
normal: normal:
a: 600 a: 600
m: 50 m: 50
ch: 1000 ch: 1000
kickban: 250
chains: chains:
userset: userset:
interval: 1800000, interval: 1800000
num: 1000 num: 1000
admin: admin:
normal: normal:
a: 120 a: 120
m: 16.6666666667 m: 16.666666666666668
ch: 100 ch: 100
kickban: 31.25
chains: chains:
userset: userset:
interval: 1800000, interval: 1800000
num: 1000 num: 1000

View File

@ -17,6 +17,7 @@ import { ChannelList } from "./ChannelList";
import { config } from "./config"; import { config } from "./config";
import { saveChatHistory, getChatHistory } from "../data/history"; import { saveChatHistory, getChatHistory } from "../data/history";
import { mixin } from "../util/helpers"; import { mixin } from "../util/helpers";
import { NoteQuota } from "../ws/ratelimit/NoteQuota";
interface CachedKickban { interface CachedKickban {
userId: string; userId: string;
@ -106,6 +107,9 @@ export class Channel extends EventEmitter {
this.getInfo(), this.getInfo(),
this.getParticipantList() this.getParticipantList()
); );
// Set their rate limits to match channel status
socket.resetRateLimits();
} }
} }
} }

View File

@ -29,6 +29,7 @@ import { adminLimits } from "./ratelimit/limits/admin";
import { userLimits } from "./ratelimit/limits/user"; import { userLimits } from "./ratelimit/limits/user";
import { NoteQuota } from "./ratelimit/NoteQuota"; import { NoteQuota } from "./ratelimit/NoteQuota";
import { config } from "./usersConfig"; import { config } from "./usersConfig";
import { crownLimits } from "./ratelimit/limits/crown";
const logger = new Logger("Sockets"); const logger = new Logger("Sockets");
@ -103,10 +104,8 @@ export class Socket extends EventEmitter {
(async () => { (async () => {
await this.loadUser(); await this.loadUser();
// TODO Permissions this.resetRateLimits();
let isAdmin = false; this.setNoteQuota(NoteQuota.PARAMS_RIDICULOUS);
this.setRateLimits(isAdmin ? adminLimits : userLimits);
this.bindEventListeners(); this.bindEventListeners();
})(); })();
@ -400,10 +399,32 @@ export class Socket extends EventEmitter {
for (const key of Object.keys(list.chains)) { for (const key of Object.keys(list.chains)) {
(this.rateLimits.chains as any)[key] = (list.chains as any)[key](); (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([ this.sendArray([
{ {
m: "nq", m: "nq",

View File

@ -3,9 +3,13 @@ import { ServerEventListener } from "../../../../util/types";
export const kickban: ServerEventListener<"kickban"> = { export const kickban: ServerEventListener<"kickban"> = {
id: "kickban", id: "kickban",
callback: (msg, socket) => { callback: (msg, socket) => {
// Kickbanning some asshole from channel // Kickbanning someone from channel
if (typeof msg._id !== "string") return; if (typeof msg._id !== "string") return;
if (typeof msg.ms !== "number") return; if (typeof msg.ms !== "number") return;
if (socket.rateLimits)
if (!socket.rateLimits.normal.kickban.attempt()) return;
socket.kickban(msg._id, msg.ms); socket.kickban(msg._id, msg.ms);
} }
}; };

View File

@ -10,6 +10,7 @@ export interface RateLimitConfigList<
m: RL; m: RL;
a: RL; a: RL;
ch: RL; ch: RL;
kickban: RL;
}; };
chains: { chains: {
@ -35,7 +36,8 @@ export const config = loadConfig<RateLimitsConfig>("config/ratelimits.yml", {
normal: { normal: {
a: 6000 / 4, a: 6000 / 4,
m: 1000 / 20, m: 1000 / 20,
ch: 1000 / 1 ch: 1000 / 1,
kickban: 1000 / 4
}, },
chains: { chains: {
userset: { userset: {
@ -48,7 +50,8 @@ export const config = loadConfig<RateLimitsConfig>("config/ratelimits.yml", {
normal: { normal: {
a: 6000 / 10, a: 6000 / 10,
m: 1000 / 20, m: 1000 / 20,
ch: 1000 / 1 ch: 1000 / 1,
kickban: 1000 / 4
}, },
chains: { chains: {
userset: { userset: {
@ -61,7 +64,8 @@ export const config = loadConfig<RateLimitsConfig>("config/ratelimits.yml", {
normal: { normal: {
a: 6000 / 50, a: 6000 / 50,
m: 1000 / 60, m: 1000 / 60,
ch: 1000 / 10 ch: 1000 / 10,
kickban: 1000 / 32
}, },
chains: { chains: {
userset: { userset: {

View File

@ -6,7 +6,8 @@ export const adminLimits: RateLimitConstructorList = {
normal: { normal: {
a: () => new RateLimit(config.admin.normal.a), a: () => new RateLimit(config.admin.normal.a),
m: () => new RateLimit(config.admin.normal.m), 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: { chains: {
userset: () => userset: () =>

View File

@ -2,14 +2,13 @@ import { RateLimit } from "../RateLimit";
import { RateLimitChain } from "../RateLimitChain"; import { RateLimitChain } from "../RateLimitChain";
import { RateLimitConstructorList, config } from "../config"; import { RateLimitConstructorList, config } from "../config";
// I have no idea why these things exist but I made it apparently // All this does is instantiate rate limits from the config
// All it does it construct the rate limits from the config instead
// of using random numbers I found on the internet
export const userLimits: RateLimitConstructorList = { export const userLimits: RateLimitConstructorList = {
normal: { normal: {
a: () => new RateLimit(config.user.normal.a), a: () => new RateLimit(config.user.normal.a),
m: () => new RateLimit(config.user.normal.m), 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: { chains: {
userset: () => userset: () =>