feat(voice): support Cloudflare Tunnel deployments and WebRTC fallback

Route page and signaling traffic through Cloudflare Tunnel while sending
WebRTC media over direct UDP, with optional TURN REST as a fallback.
When WebRTC was already offered and then fails, stay in the current
session, automatically resume compatibility audio, and keep a Reconnect
WebRTC control. A reconnect retries WebRTC only, leaves compatibility
running until it succeeds, and stays on compatibility if it fails again.

Database schema 14 adds nullable voice override columns and is a one-way
boundary: older binaries reject this database. This change is for review,
not a production rollout.
This commit is contained in:
OddTurtleHead committed 2026-11-26 23:47:50 +08:00
1 parent 9a12e2ee7b
commit b6e7b1e4da
68 files changed
+3525 -189

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
# Copy to .env and replace the secret. Keep this file out of git.
TURN_SECRET=replace-with-at-least-32-random-bytes
TURN_REALM=webspeak
# Public IPv4 of this host. Behind 1:1 NAT, use PUBLIC/PRIVATE, e.g.
# 192.0.2.10/10.0.0.5
TURN_EXTERNAL_IP=203.0.113.10
TURN_MIN_PORT=49160
TURN_MAX_PORT=49200
+82
View File
@@ -0,0 +1,82 @@
# WebSpeak TURN (coturn) deployment
TURN is a **fallback** for voice media when a browser cannot reach the WebSpeak
gateway over UDP, for example on mobile networks or networks that block UDP.
It is not required for the normal direct UDP path.
## 1. Configure
```bash
cp .env.example .env
# Generate a secret and put the same value in WEBSPEAK_TURN_REST_SECRET:
openssl rand -hex 32
```
Set `TURN_EXTERNAL_IP` to the host's public IPv4. If the host is behind 1:1
NAT, use `PUBLIC/PRIVATE` (for example `203.0.113.10/10.0.0.5`). The relay
range `49160–49200/UDP` is separate from WebSpeak's WebRTC range
`40000–40099/UDP`; do not reuse the WebRTC range.
## 2. Start and expose
```bash
docker compose --env-file .env up -d
docker compose logs -f coturn
```
Allow inbound:
- `3478/UDP` and `3478/TCP` for `turn:`
- `49160–49200/UDP` for relayed media
- `5349/TCP` only if you add TLS certificates for `turns:`
Do **not** put the TURN relay range behind Cloudflare Tunnel; Cloudflare Tunnel
does not carry this UDP traffic.
## 3. Connect WebSpeak
Use the same secret as coturn's `static-auth-secret`:
```bash
WEBSPEAK_TURN_REST_URLS='turn:turn.example.com:3478?transport=udp,turn:turn.example.com:3478?transport=tcp'
WEBSPEAK_TURN_REST_SECRET='<same value as TURN_SECRET>'
WEBSPEAK_TURN_REST_TTL_SECONDS=21600
# Keep direct UDP first. Use "relay" only where UDP is known to be blocked.
WEBSPEAK_VOICE_ICE_TRANSPORT_POLICY=all
# Auto keeps direct and untrusted-proxy traffic on the legacy audio path.
WEBSPEAK_VOICE_PROFILE=auto
```
TURN does not require Opus; `WEBSPEAK_VOICE_OPUS_WS` remains off by default.
`WEBSPEAK_TURN_REST_URLS` accepts only `turn:`/`turns:` URL tokens. For static
ICE credentials, use `WEBSPEAK_VOICE_ICE_SERVERS` with a JSON array of ICE
server objects instead. WebSpeak generates short-lived `username`/`credential`
pairs with HMAC-SHA1, compatible with coturn's `use-auth-secret`.
## 4. Verify
From a network that blocks direct UDP, open the voice page and check:
- `chrome://webrtc-internals` (Chrome/Edge) or `about:webrtc` (Firefox)
- The selected candidate pair should become `relay` only when direct UDP fails.
- `WEBSPEAK_VOICE_ICE_TRANSPORT_POLICY=all` must not be changed to `relay`
merely because a Tunnel is in front of HTTPS.
If TURN succeeds but audio still stutters, check whether the connection's
`voiceProfile` is `adaptive`; on a direct or legacy profile the old 80 ms
compatibility window is expected. TURN reachability alone does not enable the
adaptive jitter buffer.
## Security notes
- Keep the secret out of shell history, process listings where possible, and
git. Prefer an `.env` file with restrictive permissions.
- The compose file denies private, loopback and link-local peer ranges to
reduce abuse. If the WebSpeak gateway itself is on a private address, add a
narrowly scoped `--allowed-peer-ip` for that address and review the risk.
- This template has no TLS listener. Add certificates and a `turns:` URL only
when you need TURN over TLS on `5349/TCP`; test certificate renewal.
- This template has not been exercised end-to-end with a live Tunnel and
browser deployment; validate TURN reachability and credentials in the target
network before relying on it.
+40
View File
@@ -0,0 +1,40 @@
services:
coturn:
image: coturn/coturn:4.6.3
container_name: webspeak-coturn
restart: unless-stopped
# Host networking keeps the configured relay UDP range and external-ip
# mapping predictable. On Linux, replace it with explicit ports only if
# your platform does not support host mode.
network_mode: host
environment:
TURN_SECRET: ${TURN_SECRET:?set TURN_SECRET in .env}
TURN_REALM: ${TURN_REALM:-webspeak}
TURN_EXTERNAL_IP: ${TURN_EXTERNAL_IP:?set TURN_EXTERNAL_IP in .env}
TURN_MIN_PORT: ${TURN_MIN_PORT:-49160}
TURN_MAX_PORT: ${TURN_MAX_PORT:-49200}
command:
- -n
- --log-file=stdout
- --no-cli
- --fingerprint
- --lt-cred-mech
- --use-auth-secret
- --static-auth-secret=${TURN_SECRET}
- --realm=${TURN_REALM}
- --external-ip=${TURN_EXTERNAL_IP}
- --listening-port=3478
- --min-port=${TURN_MIN_PORT}
- --max-port=${TURN_MAX_PORT}
- --stale-nonce=600
- --no-multicast-peers
- --denied-peer-ip=0.0.0.0-0.255.255.255
- --denied-peer-ip=10.0.0.0-10.255.255.255
- --denied-peer-ip=127.0.0.0-127.255.255.255
- --denied-peer-ip=169.254.0.0-169.254.255.255
- --denied-peer-ip=172.16.0.0-172.31.255.255
- --denied-peer-ip=192.168.0.0-192.168.255.255
- --denied-peer-ip=::1
- --denied-peer-ip=fc00::-fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
- --denied-peer-ip=fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff
- --denied-peer-ip=ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff