[client] Calls: four MLS kinds (call, callAnswer, callIce, callEnd) with the frame key in the offer; coturn in the NixOS module; Caddy's global logger mkDefault; v0.3.0

This commit is contained in:
2026-12-03 09:20:13 +01:00
parent cb7cbd4941
commit 7c6a1680af
14 changed files with 1107 additions and 23 deletions
+95 -7
View File
@@ -4,12 +4,27 @@
# services.tabellarius = {
# enable = true;
# domain = "relay.example.cz"; # its DNS name points at this machine; ports 80 and 443 open
# # Calls (optional): TURN credentials from the relay, coturn on this machine with the same secret.
# # turn.secretFile = "/run/secrets/tabellarius-turn"; # outside the Nix store
# # turn.urls = [ "turn:relay.example.cz:3478?transport=udp" "turn:relay.example.cz:3478?transport=tcp" ];
# # turn.coturn.enable = true;
# };
{ self }:
{ config, lib, pkgs, ... }:
let
cfg = config.services.tabellarius;
system = pkgs.stdenv.hostPlatform.system;
coturn = cfg.turn.coturn;
# Peers TURN may never relay to: private, loopback, link-local, shared, documentation and reserved ranges, so a
# call cannot be turned into a way into the server's own network (call-kinds.md section 2).
deniedPeers = [
"0.0.0.0-0.255.255.255" "10.0.0.0-10.255.255.255" "100.64.0.0-100.127.255.255" "127.0.0.0-127.255.255.255"
"169.254.0.0-169.254.255.255" "172.16.0.0-172.31.255.255" "192.0.0.0-192.0.0.255" "192.0.2.0-192.0.2.255"
"192.168.0.0-192.168.255.255" "198.18.0.0-198.19.255.255" "198.51.100.0-198.51.100.255"
"203.0.113.0-203.0.113.255" "224.0.0.0-255.255.255.255"
"::1" "::ffff:0:0-::ffff:ffff:ffff" "64:ff9b::-64:ff9b::ffff:ffff" "fc00::-fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
"fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff" "ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
];
in
{
options.services.tabellarius = {
@@ -80,6 +95,43 @@ in
example = [ "turn:turn.example.org:3478?transport=udp" "turn:turn.example.org:3478?transport=tcp" ];
description = "The TURN servers named in each credential. Required with `secretFile`.";
};
coturn = {
enable = lib.mkEnableOption ''
coturn on this machine, for calls, with the relay's TURN secret (`use-auth-secret`), no logs, and no
relaying to private or reserved addresses. Needs `turn.secretFile`. Opens 3478 (UDP and TCP) and the relay
ports (UDP) when `openFirewall` is on'';
realm = lib.mkOption {
type = lib.types.str;
default = cfg.domain;
defaultText = lib.literalExpression "config.services.tabellarius.domain";
description = "coturn's realm.";
};
minPort = lib.mkOption {
type = lib.types.port;
default = 49152;
description = "The first UDP port coturn relays media on.";
};
maxPort = lib.mkOption {
type = lib.types.port;
default = 49999;
description = "The last UDP port coturn relays media on.";
};
externalIp = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "198.51.100.7/192.168.1.10";
description = ''
Behind NAT: the public address, then `/` and this machine's own (coturn `external-ip`). The router
forwards 3478 (UDP and TCP) and the relay ports (UDP) to this machine.
'';
};
allowedPeerIps = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "192.168.1.0-192.168.1.255" ];
description = "Ranges coturn may relay to although they are denied by default (a test network). Leave empty.";
};
};
};
openFirewall = lib.mkOption {
@@ -90,10 +142,16 @@ in
};
config = lib.mkIf cfg.enable {
assertions = [{
assertion = (cfg.turn.secretFile == null) == (cfg.turn.urls == [ ]);
message = "services.tabellarius.turn: secretFile and urls come together, or neither.";
}];
assertions = [
{
assertion = (cfg.turn.secretFile == null) == (cfg.turn.urls == [ ]);
message = "services.tabellarius.turn: secretFile and urls come together, or neither.";
}
{
assertion = coturn.enable -> cfg.turn.secretFile != null;
message = "services.tabellarius.turn.coturn needs turn.secretFile: coturn and the relay share it.";
}
];
systemd.services.tabellarius = {
description = "Tabellarius message relay (loopback only; Caddy in front)";
@@ -161,8 +219,9 @@ in
services.caddy = {
enable = true;
# Caddy's own logger: errors about TLS handshakes or upstreams can name a client's address.
# No log is the rule; an operator who needs one turns it on for a while, knowing why.
logFormat = ''
# No log is the rule; an operator who needs one turns it on for a while, knowing why. mkDefault: it is the
# logger of every site on the machine, so a host that serves others may keep errors (home-relay.md).
logFormat = lib.mkDefault ''
output discard
'';
virtualHosts.${cfg.domain} = {
@@ -181,7 +240,36 @@ in
};
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 80 443 ];
# TURN for calls (call-kinds.md section 2): the relay's secret reaches coturn as a systemd credential, read by
# its start script into /run/coturn (0700); never the Nix store. No log: coturn's sessions name both devices'
# addresses. TLS and DTLS are off: the media inside is DTLS-SRTP and frame-sealed, and the credentials are HMACs.
services.coturn = lib.mkIf coturn.enable {
enable = true;
realm = coturn.realm;
use-auth-secret = true;
static-auth-secret-file = "/run/credentials/coturn.service/turn-secret";
listening-port = 3478;
min-port = coturn.minPort;
max-port = coturn.maxPort;
no-cli = true;
no-tls = true;
no-dtls = true;
no-tcp-relay = true;
extraConfig = lib.concatStringsSep "\n" (
[ "no-stdout-log" "log-file=/dev/null" "simple-log" "no-multicast-peers" ]
++ map (r: "denied-peer-ip=${r}") deniedPeers
++ map (r: "allowed-peer-ip=${r}") coturn.allowedPeerIps
++ lib.optional (coturn.externalIp != null) "external-ip=${coturn.externalIp}"
);
};
systemd.services.coturn.serviceConfig.LoadCredential =
lib.mkIf coturn.enable [ "turn-secret:${toString cfg.turn.secretFile}" ];
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall ([ 80 443 ] ++ lib.optional coturn.enable 3478);
networking.firewall.allowedUDPPorts = lib.mkIf (cfg.openFirewall && coturn.enable) [ 3478 ];
networking.firewall.allowedUDPPortRanges = lib.mkIf (cfg.openFirewall && coturn.enable) [
{ from = coturn.minPort; to = coturn.maxPort; }
];
# NixOS logs every refused connection with its source address by default. Those are strangers,
# not users, but a relay that keeps no address should not keep theirs either. mkDefault: the
# machine's owner can turn it back on.
+41 -7
View File
@@ -1,12 +1,13 @@
# `nix flake check` (Linux, KVM): two machines. `server` runs the module; `client` talks to it
# through Caddy with TLS, does everything the protocol allows and some things it refuses, fills a
# mailbox cap to its 507, and then the test fails if the client's address appears anywhere in the
# server's journal or under /var/log, or in the relay's database (.agents/features/home-relay.md).
# mailbox cap to its 507, relays a call's packets through coturn with credentials from the relay, and then
# the test fails if the client's address appears anywhere in the server's journal, under /var/log, in the
# relay's database or where coturn runs (.agents/features/home-relay.md, call-kinds.md).
{ self, pkgs }:
pkgs.testers.runNixOSTest {
name = "tabellarius-relay-keeps-no-address";
nodes.server = { lib, ... }: {
nodes.server = { config, lib, ... }: {
imports = [ self.nixosModules.default ];
services.tabellarius = {
enable = true;
@@ -15,10 +16,18 @@ pkgs.testers.runNixOSTest {
# Calls (calls-relay.md): the TURN route, with a secret made at boot, never in the repository or the store.
turn.secretFile = "/run/turn-test/secret";
turn.urls = [ "turn:relay.test:3478?transport=udp" ];
# coturn with the same secret. The test network is private, so the client's range is let through here;
# every other private range stays denied.
turn.coturn.enable = true;
turn.coturn.allowedPeerIps = [ "192.168.1.0-192.168.1.255" ];
};
# The VM's first NIC is QEMU's user network, which every node shares as 10.0.2.15: coturn listens and relays on
# the test network only.
services.coturn.listening-ips = [ config.networking.primaryIPAddress ];
services.coturn.relay-ips = [ config.networking.primaryIPAddress ];
systemd.services.turn-test-secret = {
wantedBy = [ "tabellarius.service" ];
before = [ "tabellarius.service" ];
wantedBy = [ "tabellarius.service" "coturn.service" ];
before = [ "tabellarius.service" "coturn.service" ];
serviceConfig.Type = "oneshot";
script = "install -d -m 0700 /run/turn-test && head -c 32 /dev/urandom | base64 > /run/turn-test/secret";
};
@@ -27,11 +36,14 @@ pkgs.testers.runNixOSTest {
};
nodes.client = { pkgs, ... }: {
environment.systemPackages = [ pkgs.curl pkgs.python3 ];
# coturn for its test tools: turnutils_uclient (a TURN client) and turnutils_peer (the far end).
environment.systemPackages = [ pkgs.curl pkgs.python3 pkgs.coturn ];
# The peer answers the relayed packets on 3480/udp.
networking.firewall.enable = false;
};
testScript = ''
import base64, hashlib, json, os
import base64, hashlib, json, os, re
def b64(raw):
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
@@ -39,7 +51,10 @@ pkgs.testers.runNixOSTest {
start_all()
server.wait_for_unit("tabellarius.service")
server.wait_for_unit("caddy.service")
server.wait_for_unit("coturn.service")
server.wait_for_open_port(443)
# coturn listens on the test network only, not on localhost, which is where wait_for_open_port looks.
server.wait_until_succeeds("ss -Hltn 'sport = :3478' | grep -q .")
client.wait_for_unit("multi-user.target")
client_ip = client.succeed("ip -4 -o addr show dev eth1 | awk '{print $4}' | cut -d/ -f1").strip()
@@ -77,6 +92,22 @@ pkgs.testers.runNixOSTest {
# Calls: TURN credentials for the fetch token only; a long-poll with nothing arriving answers after its wait.
assert call("POST", "/v0/mailboxes/" + mid + "/turn", token=fetch) == "200"
assert call("POST", "/v0/mailboxes/" + mid + "/turn", token=post) == "404"
creds = json.loads(client.succeed(
"curl -sk --resolve relay.test:443:" + server_ip + " -X POST -H 'Authorization: Bearer " + fetch + "'"
+ " https://relay.test/v0/mailboxes/" + mid + "/turn"))
assert creds["urls"] == ["turn:relay.test:3478?transport=udp"], creds
# coturn relays a call's packets for those credentials, to a peer on the client, and refuses a wrong password.
client.succeed("turnutils_peer -L " + client_ip + " -p 3480 >/dev/null 2>&1 &")
client.succeed("sleep 1")
uclient = ("timeout 60 turnutils_uclient -n 10 -m 1 -l 120 -e " + client_ip + " -r 3480 -u '"
+ creds["username"] + "' -w '{}' " + server_ip)
out = client.succeed(uclient.format(creds["password"]) + " 2>&1")
print(out)
sent = re.search(r"tot_recv_msgs=(\d+)", out)
assert sent and int(sent.group(1)) > 0, "no packet came back through coturn"
refused = client.fail(uclient.format(b64(os.urandom(20))) + " 2>&1")
assert "401" in refused, refused
assert call("GET", "/v0/mailboxes/" + mid + "/blobs?wait=1", token=fetch) == "200"
# Refusals: a wrong token, a malformed id, a body that is not JSON.
assert call("GET", "/v0/mailboxes/" + mid + "/blobs", token=b64(os.urandom(32))) == "404"
@@ -91,6 +122,9 @@ pkgs.testers.runNixOSTest {
server.succeed("journalctl --sync")
server.fail("journalctl --no-pager -o verbose | grep -F '" + client_ip + "'")
server.fail("grep -rF '" + client_ip + "' /var/log /var/lib/private/tabellarius")
# Nor where coturn runs: its config and pid in /run, any temporary file.
# -s: a missing directory must not turn a match into exit 2, which `fail` would accept.
server.fail("grep -rsF '" + client_ip + "' /run/coturn /run/turnserver /tmp /var/tmp")
# The TURN secret reached the relay through systemd's credentials, never its database.
secret = server.succeed("cat /run/turn-test/secret").strip()
server.fail("grep -rF '" + secret + "' /var/lib/private/tabellarius /var/log")