4.5 KiB
4.5 KiB
Calls, the relay's part: long-poll, TURN credentials, faster wakes
Designed 2026-12-03 for Amicitia F36 (amicitia/.agents/features/calls.md). The call's own messages (offer, answer,
ICE, end) are MLS application kinds in the client, a separate change; this one is what the relay must do.
1. Why and what
A call must ring within about a second and get through any NAT without either side learning the other's address.
- Long-poll.
GET /v0/mailboxes/{id}/blobs?wait=N(N in seconds, at most 25): if nothing is waiting, the relay holds the request until a blob arrives for that mailbox or N seconds pass, then answers as today. Withoutwaitnothing changes. - TURN credentials.
POST /v0/mailboxes/{id}/turnwith the mailbox's fetch token answers{username, password, ttl, urls}in the TURN REST API's form (coturnuse-auth-secret):usernameis<expiry>:<random>,passwordis base64(HMAC-SHA1(secret, username)), valid 10 minutes. The relay and the TURN server share one secret; a relay started without--turn-secret-fileanswers 404. - Wakes every 10 s at most per mailbox (was 60 s), so a call right after a message still wakes a closed app.
2. Design
flowchart LR
c[client] -->|"GET …/blobs?wait=25 (fetch token)"| r[relay]
p[sender] -->|"POST …/blobs (post token)"| r
r -. "in-memory notify, per mailbox" .-> r
c -->|"POST …/turn (fetch token)"| r -->|"username, password, urls"| c
c -->|"TURN allocate (username, password)"| t[coturn]
r -. "shared secret, never sent" .- t
- Arrivals (
relay/src/arrivals.rs): a map from mailbox id to atokio::sync::Notify, in memory only, an entry only while someone waits. The waiter registers before it reads the store, so a blob posted between the read and the wait is never missed. At most 1 024 waiters relay-wide; one more is answered at once, as withoutwait. The token is checked first: a wrong token is 404 at once, never held. - TURN (
relay/src/turn.rs): the secret is read once at start from a file (Custos on the server writes it); never logged, never sent. At most 30 credentials per mailbox per hour, counted in memory (a map pruned as it grows); one more is 429. The answer names no mailbox.urlscome from--turn-url(repeatable), e.g.turn:turn.marek-novak.cz:3478?transport=udp. - The NixOS module gains
services.tabellarius.turn = { enable; domain; secretFile; }. Enabled, it also runs coturn withuse-auth-secret, the same secret,no-cli,no-tcp-relayoff, a relay port range, and no log (log-file=/dev/null,no-stdout-log,simple-log, syslog off), and opens 3478 (UDP and TCP) and the relay ports. TURN over TLS (5349) is left out: the media inside is DTLS-SRTP and frame-encrypted already, and the TURN credentials are HMAC-based, never sent in clear.
3. Risks
- Privacy surface. The relay learns this mailbox wants TURN now (it already sees each mailbox's fetches and
their timing), and that a client is online while a long-poll is held. Nothing new is stored (§5 unchanged); the
waiter map and the TURN counters live in memory only, like the wake times (PROTOCOL.md §5). coturn sees both
devices' IP addresses during a call — said to users on the first call (Amicitia
calls.md); the VM test proves coturn writes no address to disk. - Abuse. Held connections are capped; TURN credentials are per mailbox, rate-limited and short-lived; coturn
relays only for valid HMAC credentials and denies relaying to private ranges (
denied-peer-ip). - Reproducibility. One new crate,
sha1(RustCrypto), for coturn's HMAC-SHA1;hmacis already in the tree.
4. Tests — written first
- A long-poll answers within 1 s of a post to its mailbox; with nothing posted it answers empty after
wait; a post to another mailbox does not wake it; a wrong token is 404 without waiting;waitover 25 is held 25 s at most (clock: real, short waits); the 1 025th waiter is answered at once. - TURN: without the secret, 404; with it and a right fetch token, a pair whose password is the HMAC-SHA1 of the username under the secret and whose expiry is now + 600; a wrong token 404; the 31st in an hour 429; the relay's database is byte-identical before and after (nothing stored).
- Wakes: a mailbox is woken again after 10 s, not within them.
- The NixOS VM test: with
turn.enable, the client gets credentials from the relay and allocates on coturn with them (turnutils_uclient); a wrong password is refused; afterwards the client's address appears nowhere on the server's disk or journal.