mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Tell clients when the gateway is full; default limit 60
A browser can't see why a WebSocket upgrade was refused, so a full gateway now accepts, sends a gateway_full error and closes. The client shows a translated hint that includes self-hosting. Default MAX_CONNECTIONS drops from 300 to 60 after measuring ~8-9 % of a core per listening client. 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
999262b6df
commit
e02a41d855
8 files changed
+17
-6
No files matched your search
@@ -224,7 +224,7 @@ All optional; add them under `environment:` in your compose override next to `PO
|
||||
| `ALLOW_PRIVATE_TARGETS` | off | By default the gateway refuses to connect to private/internal addresses (loopback, `10.x`, `192.168.x`, CGNAT, link-local, …), no matter how they were reached (IP, DNS, SRV, TSDNS, nickname, file-transfer redirect), so it can't be abused to probe the network it runs in. **If your TeamSpeak server is on the same LAN as the gateway, set this to `1`.** |
|
||||
| `TURN_URLS` | unset | Comma-separated TURN URLs (e.g. `turn:turn.example.com:3478`) offered to browsers for screen streams, for viewers or publishers behind NATs where direct P2P fails. Pair with `TURN_SECRET` (coturn's `use-auth-secret`; each browser gets its own credential valid for 24 h) or with static `TURN_USERNAME` / `TURN_CREDENTIAL`, which every visitor can read. |
|
||||
| `ALLOWED_ORIGINS` | unset (any origin) | Comma-separated list of web origins allowed to open `/ws` (e.g. `https://client.example.com`). Stops other websites from silently using your gateway as their relay. |
|
||||
| `MAX_CONNECTIONS_PER_IP` / `MAX_CONNECTIONS` | `10` / `300` | Concurrent `/ws` connections per client IP and in total (each can hold a live TeamSpeak session). `0` disables a limit. |
|
||||
| `MAX_CONNECTIONS_PER_IP` / `MAX_CONNECTIONS` | `10` / `60` | Concurrent `/ws` connections per client IP and in total (each can hold a live TeamSpeak session). `0` disables a limit. |
|
||||
| `CONNECT_RATE_LIMIT` | `30` | Server connects per client IP per minute. Each one starts a new TeamSpeak session, so this stops anyone from flooding a server through your gateway. `0` disables it. |
|
||||
| `TRUST_PROXY` | off | Only when the gateway is reachable **solely** through a proxy/tunnel, so per-IP limits see the real client instead of the proxy: `cloudflare` (Cloudflare Tunnel or proxy) uses `CF-Connecting-IP`; `1` (your own nginx/Caddy/Traefik) uses the last `X-Forwarded-For` entry, the one your proxy appended. Don't set it on a directly exposed gateway - clients could then fake their IP. |
|
||||
| `TELEMETRY` | on | Set to `0` to turn off the anonymous usage ping (see below). |
|
||||
|
||||
+10
-4
@@ -93,7 +93,9 @@ const ALLOWED_ORIGINS = (process.env.ALLOWED_ORIGINS ?? "")
|
||||
// Each /ws socket can spawn a connector process holding a live TeamSpeak
|
||||
// session, so both need a ceiling. 0 disables the respective limit.
|
||||
const MAX_CONNECTIONS_PER_IP = Number(process.env.MAX_CONNECTIONS_PER_IP ?? 10);
|
||||
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 300);
|
||||
// Measured 2026-10-25: ~8-9 % of a core per client listening to constant audio
|
||||
// (connector + node + cloudflared), ~10-15 MB RAM. 60 suits a 2-core VPS.
|
||||
const MAX_CONNECTIONS = Number(process.env.MAX_CONNECTIONS ?? 60);
|
||||
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
|
||||
@@ -570,9 +572,6 @@ const wss = new WebSocketServer({
|
||||
if (ALLOWED_ORIGINS.length > 0 && origin && !ALLOWED_ORIGINS.includes(origin.toLowerCase())) {
|
||||
return done(false, 403, "Origin not allowed");
|
||||
}
|
||||
if (MAX_CONNECTIONS > 0 && wss.clients.size >= MAX_CONNECTIONS) {
|
||||
return done(false, 503, "Gateway is full");
|
||||
}
|
||||
const ip = clientIp(req);
|
||||
if (MAX_CONNECTIONS_PER_IP > 0 && (connectionsPerIp.get(ip) ?? 0) >= MAX_CONNECTIONS_PER_IP) {
|
||||
return done(false, 429, "Too many connections");
|
||||
@@ -679,6 +678,13 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
// multiple /ws connections in parallel (one per server tab).
|
||||
let connection: Ts3Connection | undefined;
|
||||
|
||||
// Full: accept and say so, since a browser can't read why an upgrade was refused.
|
||||
if (MAX_CONNECTIONS > 0 && wss.clients.size > MAX_CONNECTIONS) {
|
||||
socket.send(JSON.stringify({ type: "error", code: "gateway_full", message: "This gateway is full right now. Please try again in a few minutes." }));
|
||||
socket.close(1013, "Gateway is full");
|
||||
return;
|
||||
}
|
||||
|
||||
const ip = clientIp(req);
|
||||
connectionsPerIp.set(ip, (connectionsPerIp.get(ip) ?? 0) + 1);
|
||||
|
||||
|
||||
+1
-1
@@ -10256,7 +10256,7 @@ function AppInner() {
|
||||
appendLog({ text: data.message, kind: "error" });
|
||||
} else {
|
||||
setConnecting(false);
|
||||
setConnectError(data.message);
|
||||
setConnectError(data.code === "gateway_full" ? t("connectError.gatewayFull") : data.message);
|
||||
updateTabMeta(sessionId, { connecting: false, connected: false });
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -709,6 +709,7 @@
|
||||
"serverContext.addFavorite": "Přidat do oblíbených",
|
||||
"serverContext.disconnect": "Odpojit",
|
||||
"connectError.dismiss": "Zavřít",
|
||||
"connectError.gatewayFull": "Brána je právě plná. Zkus to za pár minut znovu – nebo si WebSpeak3 hostuj sám: github.com/WebSpeak3/webspeak3",
|
||||
"dialog.close": "Zavřít",
|
||||
"sounds.title": "Zvuky",
|
||||
"sounds.subtitle": "Nastavit zvuková upozornění na události",
|
||||
|
||||
@@ -709,6 +709,7 @@
|
||||
"serverContext.addFavorite": "Zu Favoriten hinzufügen",
|
||||
"serverContext.disconnect": "Verbindung trennen",
|
||||
"connectError.dismiss": "Schließen",
|
||||
"connectError.gatewayFull": "Das Gateway ist gerade voll. Versuch es in ein paar Minuten nochmal – oder hoste WebSpeak3 selbst: github.com/WebSpeak3/webspeak3",
|
||||
"dialog.close": "Schließen",
|
||||
"sounds.title": "Sounds",
|
||||
"sounds.subtitle": "Sound-Benachrichtigungen für Ereignisse festlegen",
|
||||
|
||||
@@ -709,6 +709,7 @@
|
||||
"serverContext.addFavorite": "Add to favorites",
|
||||
"serverContext.disconnect": "Disconnect",
|
||||
"connectError.dismiss": "Dismiss",
|
||||
"connectError.gatewayFull": "The gateway is full right now. Try again in a few minutes – or host WebSpeak3 yourself: github.com/WebSpeak3/webspeak3",
|
||||
"dialog.close": "Close",
|
||||
"sounds.title": "Sounds",
|
||||
"sounds.subtitle": "Configure sound notifications for events",
|
||||
|
||||
@@ -708,6 +708,7 @@
|
||||
"serverContext.addFavorite": "افزودن به نشانکها",
|
||||
"serverContext.disconnect": "قطع اتصال",
|
||||
"connectError.dismiss": "رد کردن",
|
||||
"connectError.gatewayFull": "گیتوی در حال حاضر پر است. چند دقیقه دیگر دوباره امتحان کنید – یا WebSpeak3 را خودتان میزبانی کنید: github.com/WebSpeak3/webspeak3",
|
||||
"dialog.close": "بستن",
|
||||
"sounds.title": "صداها",
|
||||
"sounds.subtitle": "پیکربندی اعلانهای صوتی برای رویدادها",
|
||||
|
||||
@@ -554,6 +554,7 @@
|
||||
"serverContext.addFavorite": "添加到收藏夹",
|
||||
"serverContext.disconnect": "断开连接",
|
||||
"connectError.dismiss": "关闭",
|
||||
"connectError.gatewayFull": "网关当前已满。请几分钟后再试,或自行部署 WebSpeak3:github.com/WebSpeak3/webspeak3",
|
||||
"dialog.close": "关闭",
|
||||
"sounds.title": "声音",
|
||||
"sounds.subtitle": "配置事件提示音",
|
||||
|
||||
Reference in new issue
Block a user