diff --git a/README.md b/README.md index 7c774c4..5bbbc0e 100644 --- a/README.md +++ b/README.md @@ -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). | diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 51f8a79..2dad996 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -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(); // 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); diff --git a/web/src/App.tsx b/web/src/App.tsx index cac434e..515f144 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -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; diff --git a/web/src/locales/cs-CZ.json b/web/src/locales/cs-CZ.json index 9a89bb6..e5a0a0e 100644 --- a/web/src/locales/cs-CZ.json +++ b/web/src/locales/cs-CZ.json @@ -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", diff --git a/web/src/locales/de.json b/web/src/locales/de.json index ccf37d3..352c0e9 100644 --- a/web/src/locales/de.json +++ b/web/src/locales/de.json @@ -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", diff --git a/web/src/locales/en.json b/web/src/locales/en.json index 9b1534c..add4e7f 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -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", diff --git a/web/src/locales/fa.json b/web/src/locales/fa.json index fdb5e9e..cf26a57 100644 --- a/web/src/locales/fa.json +++ b/web/src/locales/fa.json @@ -708,6 +708,7 @@ "serverContext.addFavorite": "افزودن به نشانک‌ها", "serverContext.disconnect": "قطع اتصال", "connectError.dismiss": "رد کردن", + "connectError.gatewayFull": "گیت‌وی در حال حاضر پر است. چند دقیقه دیگر دوباره امتحان کنید – یا WebSpeak3 را خودتان میزبانی کنید: github.com/WebSpeak3/webspeak3", "dialog.close": "بستن", "sounds.title": "صداها", "sounds.subtitle": "پیکربندی اعلان‌های صوتی برای رویدادها", diff --git a/web/src/locales/zh-CN.json b/web/src/locales/zh-CN.json index 341813e..384dc76 100644 --- a/web/src/locales/zh-CN.json +++ b/web/src/locales/zh-CN.json @@ -554,6 +554,7 @@ "serverContext.addFavorite": "添加到收藏夹", "serverContext.disconnect": "断开连接", "connectError.dismiss": "关闭", + "connectError.gatewayFull": "网关当前已满。请几分钟后再试,或自行部署 WebSpeak3:github.com/WebSpeak3/webspeak3", "dialog.close": "关闭", "sounds.title": "声音", "sounds.subtitle": "配置事件提示音",