diff --git a/README.md b/README.md index 1768f32..ebfda82 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ All optional; add them under `environment:` in your compose override next to `PO | `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. | -| `TRUST_PROXY` | off | Set to `1` when the gateway is **only** reachable through a reverse proxy/tunnel (e.g. cloudflared): per-IP limits then use `CF-Connecting-IP` / `X-Forwarded-For` instead of the proxy's own address. Don't set it on a directly exposed gateway - clients could then spoof their IP. | +| `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. | > **Trust model:** the gateway speaks the TeamSpeak protocol on the user's behalf, so whoever runs it necessarily handles their identity key, server/channel passwords and privilege keys (they're kept out of process listings, but the operator can still see them). Only use gateways you trust, and serve them over HTTPS/WSS (`TLS_CERT`/`TLS_KEY` or a TLS-terminating proxy) - plain HTTP exposes all of that on the wire. File uploads are limited to 24 MiB, downloads to 25 MiB. diff --git a/docker-compose.yml b/docker-compose.yml index b063144..6248530 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,9 @@ services: # This file publishes no port - the gateway is only reachable through # the cloudflared tunnel below, so the client IP it forwards can be # trusted (per-IP limits would otherwise see every user as the tunnel). - # Remove this if you publish a port directly instead. - - TRUST_PROXY=1 + # Remove this if you publish a port directly instead; behind your own + # reverse proxy use TRUST_PROXY=1. + - TRUST_PROXY=cloudflare # Nothing in here needs Linux capabilities (unprivileged port, plain # TCP/UDP sockets), so drop them all. cap_drop: [ALL] diff --git a/gateway/src/net.ts b/gateway/src/net.ts index e55d38b..df33128 100644 --- a/gateway/src/net.ts +++ b/gateway/src/net.ts @@ -1,19 +1,20 @@ import type { IncomingMessage } from "node:http"; -// Behind a reverse proxy / tunnel (e.g. cloudflared) every request arrives -// from the proxy's own address, so per-IP limits would lump all users into -// one bucket. TRUST_PROXY=1 makes us read the client IP the proxy reports -// instead - only set it when the gateway is reachable *solely* through that -// proxy, otherwise anyone can send the header and pick their own "IP". -const TRUST_PROXY = process.env.TRUST_PROXY === "1"; +// Behind a reverse proxy / tunnel every request arrives from the proxy's own +// address, so per-IP limits would lump all users into one bucket. Only set +// TRUST_PROXY when the gateway is reachable *solely* through that proxy: +// - "cloudflare": CF-Connecting-IP, which Cloudflare always sets itself. +// - "1": the last X-Forwarded-For entry, the one our own proxy appended; +// anything before it came from the client and can be made up. +const TRUST_PROXY = process.env.TRUST_PROXY; export function clientIp(req: IncomingMessage): string { - if (TRUST_PROXY) { + if (TRUST_PROXY === "cloudflare") { const cf = req.headers["cf-connecting-ip"]; - if (typeof cf === "string" && cf) return cf.trim(); - const xff = req.headers["x-forwarded-for"]; - const first = (Array.isArray(xff) ? xff[0] : xff)?.split(",")[0]?.trim(); - if (first) return first; + if (typeof cf === "string" && cf.trim()) return cf.trim(); + } else if (TRUST_PROXY === "1") { + const last = String(req.headers["x-forwarded-for"] ?? "").split(",").at(-1)?.trim(); + if (last) return last; } return (req.socket.remoteAddress ?? "unknown").replace(/^::ffff:/, ""); }