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
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

View File

@ -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();
}
}
}

View File

@ -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",

View File

@ -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);
}
};

View File

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

View File

@ -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: () =>

View File

@ -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: () =>