[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.