2021-04-07 12:23:48 +02:00
|
|
|
|
# The NixOS module does not support extending the configuration with secrets.
|
2021-03-31 12:08:35 +02:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
let
|
|
|
|
|
fqdn = "turn.sbruder.de";
|
|
|
|
|
|
|
|
|
|
ipAddresses = [ "195.201.139.15" "2a01:4f8:1c1c:4397::" ];
|
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
cfg = {
|
2021-03-31 12:08:35 +02:00
|
|
|
|
# config adapted from synapse’s turn howto:
|
|
|
|
|
# https://github.com/matrix-org/synapse/blob/develop/docs/turn-howto.md
|
|
|
|
|
use-auth-secret = true;
|
|
|
|
|
realm = fqdn;
|
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
# not needed for VoIP
|
2021-03-31 12:08:35 +02:00
|
|
|
|
no-tcp-relay = true;
|
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
# only tls
|
|
|
|
|
no-cli = true;
|
|
|
|
|
no-tcp = true;
|
|
|
|
|
no-udp = true;
|
|
|
|
|
|
|
|
|
|
tls-listening-port = 5349;
|
2021-03-31 12:08:35 +02:00
|
|
|
|
cert = "/run/turnserver/fullchain.pem";
|
|
|
|
|
pkey = "/run/turnserver/key.pem";
|
|
|
|
|
|
|
|
|
|
min-port = 49160;
|
|
|
|
|
max-port = 49200;
|
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
listening-ip = ipAddresses;
|
|
|
|
|
relay-ip = ipAddresses;
|
2021-03-31 12:08:35 +02:00
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
pidfile = "/run/turnserver/turnserver.pid";
|
|
|
|
|
|
|
|
|
|
# logging
|
|
|
|
|
no-stdout-log = true;
|
|
|
|
|
syslog = true;
|
2021-03-31 12:08:35 +02:00
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
denied-peer-ip = [
|
|
|
|
|
"10.0.0.0-10.255.255.255"
|
|
|
|
|
"172.16.0.0-172.31.255.255"
|
|
|
|
|
"192.168.0.0-192.168.255.255"
|
|
|
|
|
];
|
2021-03-31 12:08:35 +02:00
|
|
|
|
|
2021-04-07 12:23:48 +02:00
|
|
|
|
user-quota = 12;
|
|
|
|
|
total-quota = 1200;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
extraConfigFiles = with config.sops.secrets; [
|
|
|
|
|
turn-static-auth-secret.path
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
configToText = config: lib.concatStrings
|
|
|
|
|
(lib.mapAttrsToList
|
|
|
|
|
(name: value:
|
|
|
|
|
if lib.isList value
|
|
|
|
|
then
|
|
|
|
|
lib.concatMapStrings
|
|
|
|
|
(value: "${name}=${toString value}\n")
|
|
|
|
|
value
|
|
|
|
|
else
|
|
|
|
|
(if lib.isBool value
|
|
|
|
|
then "${name}\n"
|
|
|
|
|
else "${name}=${toString value}\n"))
|
|
|
|
|
config);
|
|
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "turnserver.conf" (configToText cfg);
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
sops.secrets.turn-static-auth-secret = {
|
|
|
|
|
owner = "turnserver";
|
|
|
|
|
sopsFile = ../secrets.yaml;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.users.turnserver = {
|
|
|
|
|
uid = config.ids.uids.turnserver;
|
|
|
|
|
};
|
|
|
|
|
users.groups.turnserver = {
|
|
|
|
|
gid = config.ids.gids.turnserver;
|
|
|
|
|
members = [ "turnserver" ];
|
2021-03-31 12:08:35 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
systemd.services.coturn = {
|
2021-04-07 12:23:48 +02:00
|
|
|
|
after = [ "network-online.target" "acme-finished-${fqdn}.target" ];
|
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
2021-03-31 12:08:35 +02:00
|
|
|
|
serviceConfig = {
|
2021-04-07 12:23:48 +02:00
|
|
|
|
Type = "simple";
|
|
|
|
|
ExecStartPre = [
|
|
|
|
|
("!" + (pkgs.writeShellScript "coturn-setup-tls" ''
|
|
|
|
|
cp ${config.security.acme.certs."${fqdn}".directory}/{fullchain,key}.pem /run/turnserver/
|
|
|
|
|
chgrp turnserver /run/turnserver/{fullchain,key}.pem
|
|
|
|
|
''))
|
|
|
|
|
(pkgs.writeShellScript "coturn-setup-config" ''
|
|
|
|
|
${pkgs.coreutils}/bin/cat ${configFile} ${lib.concatStringsSep " " extraConfigFiles} > /run/turnserver/turnserver.conf
|
|
|
|
|
'')
|
|
|
|
|
];
|
|
|
|
|
ExecStart = "${pkgs.coturn}/bin/turnserver -c /run/turnserver/turnserver.conf";
|
|
|
|
|
Restart = "on-abort";
|
|
|
|
|
RuntimeDirectory = "turnserver";
|
|
|
|
|
RuntimeDirectoryMode = "0750";
|
|
|
|
|
User = "turnserver";
|
|
|
|
|
Group = "turnserver";
|
|
|
|
|
SupplementaryGroups = lib.singleton "keys";
|
2021-03-31 12:08:35 +02:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
security.acme.certs."${fqdn}".postRun = ''
|
2021-04-07 12:23:48 +02:00
|
|
|
|
if systemctl is-active coturn; then
|
2021-03-31 12:08:35 +02:00
|
|
|
|
systemctl --no-block restart coturn
|
|
|
|
|
fi
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
services.nginx.virtualHosts."${fqdn}" = {
|
|
|
|
|
enableACME = true;
|
|
|
|
|
forceSSL = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networking.firewall = {
|
|
|
|
|
allowedTCPPorts = [ cfg.tls-listening-port ];
|
|
|
|
|
allowedUDPPorts = [ cfg.tls-listening-port ];
|
|
|
|
|
|
|
|
|
|
allowedUDPPortRanges = lib.singleton {
|
|
|
|
|
from = cfg.min-port;
|
|
|
|
|
to = cfg.min-port;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|