Add Coturn TURN server

This commit is contained in:
Schluffe committed 2026-10-09 13:47:56 +02:00
1 parent 752d0cf3af
commit c3ca3e1752
13 files changed
+309 -11

No files matched your search

+81
View File
@@ -73,6 +73,11 @@ relay-only routing or hide your IP from the peer. A remote peer can independentl
proxy their own traffic; the opt-in controls this app's configuration and the
relay candidates it accepts, not arbitrary software run by the other person.
When a credential endpoint is configured (including the local development setup
below), **Use the configured relay with temporary credentials** replaces manual
credential entry. The app fetches fresh credentials for each new connection,
only after TURN opt-in. Uncheck it to use a different relay manually.
TURN credentials stay in the current component's memory; the app does not persist
them or include them in codes. The relay receives its authentication exchange and
can observe addresses, timing and traffic volume, and may charge for bandwidth.
@@ -117,6 +122,82 @@ not a substitute for trusting the host or a portable WebRTC egress filter.
## Development
### Local STUN/TURN environment (Linux)
Keep Vite on the host for hot reload; only coturn runs in Docker. Both bind to
loopback, so use two browser tabs on this machine. No DNS, certificates, firewall
changes or production services are needed. Nothing is enabled at boot, and the
container has no automatic restart policy.
```sh
# Start Docker only when needed (if it isn't already running).
sudo systemctl start docker.service
# If you just joined the docker group, log in again or run: newgrp docker
bun install --frozen-lockfile
bun run dev:setup # creates ignored .env.development, without overwriting it
bun run turn:up
bun run dev # open the printed localhost URL in two tabs
```
Pick a file in the sender tab, choose Receive in the other, and check **Allow a
TURN relay** in both tabs. Leave **Use the configured relay with temporary
credentials** checked. Exchange codes and compare fingerprints as usual. Reset,
failure or completion clears consent, so check it again for each connection.
STUN defaults to the local server; TURN remains opt-in, with direct routes preferred.
```sh
bun run test # protocol and temporary-credential unit tests
bun run test:turn # real Chromium file transfer, forced TURN/UDP
bun run test:turn --tcp # same test, TURN/TCP only
bun run build
bun run test:browser # direct-path browser regressions
docker compose --env-file .env.development logs --tail=50 coturn
```
The TURN checks start and stop their own Vite server and isolated browser profiles;
coturn must already be running. They obtain real temporary credentials, force
relay-only routing, assert selected relay candidates on both ends, verify exact
received bytes and receipts, and exercise cancellation and reconnection. TCP here
means browser-to-TURN transport; relay-to-peer traffic is UDP. These same-machine
checks do not test Internet NATs or a remote server's firewall.
Stop Vite with Ctrl-C, then:
```sh
bun run turn:down
# Optional: stop Docker too, after finishing any other containers.
sudo systemctl stop docker.service docker.socket containerd.service
```
`.env.development` contains the private shared `TURN_SECRET`, server-side
`TURN_HOST`/`TURN_PORT`, and public `VITE_ICE_ENDPOINT`/`VITE_STUN_URL` settings.
See [.env.example](.env.example). The secret never enters the client bundle.
Compose passes it directly to coturn; no config-file environment substitution is
assumed. If changing the port, update both `TURN_PORT` and `VITE_STUN_URL`, then
rerun `turn:up` and restart Vite. The image is pinned to a concrete release;
loopback listeners and UDP relay ports 49160–49200 are defined in [compose.yaml](compose.yaml).
Loopback peer access is explicitly allowed for these local tests.
The credential endpoint is **development-only**, implemented as Vite middleware.
It isn't included in `dist/` or `vite preview`. Production builds ignore
`.env.development` and retain the existing static/manual configuration. To reuse
the client integration later, set `VITE_ICE_ENDPOINT` and `VITE_STUN_URL` when
building and implement the same JSON endpoint behind your production access
policy. [dev/ice-server.js](dev/ice-server.js) contains the HMAC issuance logic.
There are no accounts or room sessions in this app to authorize production
credential issuance automatically. Don't expose this development endpoint or
reuse the loopback-permitting Compose configuration as a public relay.
Credentials expire after one hour; each new code exchange fetches fresh ones.
Automatic renewal during long transfers is not implemented because this app
uses manual signaling. Keep development transfers below that duration; expired
or failed sessions need fresh codes.
Configuration references: [coturn Docker usage](https://github.com/coturn/coturn/blob/master/docker/coturn/README.md)
and [coturn configuration](https://github.com/coturn/coturn/blob/master/examples/etc/turnserver.conf).
### Existing checks and static build
```sh
bun install --frozen-lockfile
bun run dev