add ws rate limiter

This commit is contained in:
Evan
2025-02-23 19:41:38 -08:00
parent 59ae10771c
commit a072a24433
4 changed files with 45 additions and 9 deletions
+13 -1
View File
@@ -18,6 +18,7 @@ import WebSocket from "ws";
import { slog } from "./StructuredLog";
import { CreateGameRecord } from "../core/Util";
import { archive } from "./Archive";
import { RateLimiterMemory } from "rate-limiter-flexible";
export enum GamePhase {
Lobby = "LOBBY",
@@ -26,6 +27,11 @@ export enum GamePhase {
}
export class GameServer {
private rateLimiter = new RateLimiterMemory({
points: 20, // 20 messages
duration: 1, // per 1 second
});
private maxGameDuration = 5 * 60 * 60 * 1000; // 5 hours
private turns: Turn[] = [];
@@ -98,7 +104,13 @@ export class GameServer {
this.allClients.set(client.clientID, client);
client.ws.on("message", (message: string) => {
client.ws.on("message", async (message: string) => {
try {
await this.rateLimiter.consume(client.ip);
} catch (error) {
console.warn(`Rate limit exceeded for ${client.ip}`);
return;
}
try {
const clientMsg: ClientMessage = ClientMessageSchema.parse(
JSON.parse(message),