mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
fix(gateway): rate-limit server connects per IP
Each "connect" spawns a new connector and a socket could send any number of them, so a handful of sockets could join-flood a TeamSpeak server through the gateway. New CONNECT_RATE_LIMIT (default 30/min per IP). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
bbb44b3eaa
commit
c2b52b323d
2 files changed
+14
No files matched your search
@@ -93,6 +93,12 @@ const ALLOWED_ORIGINS = (process.env.ALLOWED_ORIGINS ?? "")
|
||||
const MAX_CONNECTIONS_PER_IP = Number(process.env.MAX_CONNECTIONS_PER_IP ?? 10);
|
||||
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 300);
|
||||
const connectionsPerIp = new Map<string, number>();
|
||||
// Every "connect" spawns a fresh connector, and a socket can send any number
|
||||
// of them - without this, 10 sockets were enough to join-flood a TS server
|
||||
// through this gateway at several joins per second. Real clients (reconnect
|
||||
// backoff, restoring a handful of tabs) stay far below it.
|
||||
const CONNECT_RATE_LIMIT = Number(process.env.CONNECT_RATE_LIMIT ?? 30);
|
||||
const isConnectRateLimited = createRateLimiter(CONNECT_RATE_LIMIT, 60_000);
|
||||
|
||||
// Largest single WS message: file uploads travel base64-encoded inside one,
|
||||
// so this is ~4/3 of the biggest uploadable file (see MAX_UPLOAD_BYTES in
|
||||
@@ -708,6 +714,13 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
);
|
||||
break;
|
||||
}
|
||||
if (CONNECT_RATE_LIMIT > 0 && isConnectRateLimited(ip)) {
|
||||
console.warn(`[ratelimit] too many connects from one IP, target host=${msg.host}`);
|
||||
socket.send(
|
||||
JSON.stringify({ type: "error", message: "Too many connection attempts. Please wait a minute." }),
|
||||
);
|
||||
break;
|
||||
}
|
||||
// Replacing a connection on the same socket: tear down the previous
|
||||
// connector so we don't leak processes.
|
||||
if (connection) {
|
||||
|
||||
Reference in new issue
Block a user