From c72953e1ef3dbb6cff1e9944d2be142411aae526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Sat, 29 Oct 2022 00:13:44 +0100 Subject: [PATCH] matrix: Add coturn support for calls --- configuration/default.nix | 40 ++++++++++++----- configuration/services/conduit.nix | 69 ++++++++++++++++++++++++++++++ configuration/sops.nix | 4 ++ keys/production.yaml | 9 ++-- keys/staging.yaml | 9 ++-- 5 files changed, 114 insertions(+), 17 deletions(-) diff --git a/configuration/default.nix b/configuration/default.nix index eb9d05e..ff97436 100644 --- a/configuration/default.nix +++ b/configuration/default.nix @@ -1,4 +1,5 @@ { + config, pkgs, lib, modulesPath, @@ -50,17 +51,34 @@ useDHCP = false; interfaces.eth0.useDHCP = true; - firewall.allowedTCPPorts = [ - # http - 80 - 443 - # ssh - 2222 - # matrix - 8448 - # starbound - 21025 - ]; + firewall = { + allowedTCPPorts = [ + # http + 80 + 443 + # ssh + 2222 + # matrix + 8448 + # starbound + 21025 + + config.services.coturn.listening-port + config.services.coturn.tls-listening-port + ]; + + allowedUDPPorts = [ + config.services.coturn.listening-port + config.services.coturn.tls-listening-port + ]; + + allowedUDPPortRanges = [ + { + from = config.services.coturn.min-port; + to = config.services.coturn.max-port; + } + ]; + }; }; time.timeZone = "Europe/London"; diff --git a/configuration/services/conduit.nix b/configuration/services/conduit.nix index e8bbb6e..b60b668 100644 --- a/configuration/services/conduit.nix +++ b/configuration/services/conduit.nix @@ -1,6 +1,7 @@ {config, ...}: let cfg = config.services.matrix-conduit; domain = "matrix.${config.services.nginx.domain}"; + turn-realm = "turn.${config.services.nginx.domain}"; in { services.matrix-conduit = { enable = true; @@ -8,9 +9,77 @@ in { address = "127.0.0.1"; server_name = domain; database_backend = "rocksdb"; + + turn_uris = let + address = "${config.services.coturn.realm}:${toString config.services.coturn.listening-port}"; + tls-address = "${config.services.coturn.realm}:${toString config.services.coturn.tls-listening-port}"; + in [ + "turn:${address}?transport=udp" + "turn:${address}?transport=tcp" + "turns:${tls-address}?transport=udp" + "turns:${tls-address}?transport=tcp" + ]; }; }; + # Pass in the TURN secret via EnvironmentFile, not supported by + # upstream module currently. + # + # See also https://gitlab.com/famedly/conduit/-/issues/314 + systemd.services.conduit.serviceConfig.EnvironmentFile = config.sops.secrets."turn/env".path; + + services.coturn = { + enable = true; + use-auth-secret = true; + static-auth-secret-file = config.sops.secrets."turn/secret".path; + realm = turn-realm; + relay-ips = [ + "178.79.137.55" + ]; + + # Based on suggestions from + # https://github.com/matrix-org/synapse/blob/develop/docs/turn-howto.md + # and + # https://www.foxypossibilities.com/2018/05/19/setting-up-a-turn-sever-for-matrix-on-nixos/ + no-tcp-relay = true; + secure-stun = true; + extraConfig = '' + # Deny various local IP ranges, see + # https://www.rtcsec.com/article/cve-2020-26262-bypass-of-coturns-access-control-protection/ + no-multicast-peers + denied-peer-ip=0.0.0.0-0.255.255.255 + denied-peer-ip=10.0.0.0-10.255.255.255 + denied-peer-ip=100.64.0.0-100.127.255.255 + denied-peer-ip=127.0.0.0-127.255.255.255 + denied-peer-ip=169.254.0.0-169.254.255.255 + denied-peer-ip=172.16.0.0-172.31.255.255 + denied-peer-ip=192.0.0.0-192.0.0.255 + denied-peer-ip=192.0.2.0-192.0.2.255 + denied-peer-ip=192.88.99.0-192.88.99.255 + denied-peer-ip=192.168.0.0-192.168.255.255 + denied-peer-ip=198.18.0.0-198.19.255.255 + denied-peer-ip=198.51.100.0-198.51.100.255 + denied-peer-ip=203.0.113.0-203.0.113.255 + denied-peer-ip=240.0.0.0-255.255.255.255 denied-peer-ip=::1 + denied-peer-ip=64:ff9b::-64:ff9b::ffff:ffff + denied-peer-ip=::ffff:0.0.0.0-::ffff:255.255.255.255 + denied-peer-ip=100::-100::ffff:ffff:ffff:ffff + denied-peer-ip=2001::-2001:1ff:ffff:ffff:ffff:ffff:ffff:ffff + denied-peer-ip=2002::-2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff + denied-peer-ip=fc00::-fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff + denied-peer-ip=fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff + allowed-peer-ip=178.79.137.55 + + # Limit number of rooms + user-quota=12 + total-quota=36 + + # Various other security settings + no-tlsv1 + no-tlsv1_1 + ''; + }; + services.nginx.virtualHosts."${domain}" = { enableACME = true; diff --git a/configuration/sops.nix b/configuration/sops.nix index 8efa3af..6cc395b 100644 --- a/configuration/sops.nix +++ b/configuration/sops.nix @@ -6,5 +6,9 @@ group = "nextcloud"; }; secrets."steam/tlater" = {}; + secrets."turn/env" = {}; + secrets."turn/secret" = { + owner = "turnserver"; + }; }; } diff --git a/keys/production.yaml b/keys/production.yaml index bfc494f..d666be9 100644 --- a/keys/production.yaml +++ b/keys/production.yaml @@ -2,14 +2,17 @@ nextcloud: tlater: ENC[AES256_GCM,data:zNsPm4uFaIRe3LjcwmayRg==,iv:5wam6bP5zP708jC9UrLV0s8qspl3Pm4fPzbMFYBUyPQ=,tag:apnJUMeJwMn9q0NhO4ptmA==,type:str] steam: tlater: ENC[AES256_GCM,data:HNsve/Wid40ftclO9n09yXg=,iv:VQxAz4eR9lfxEvM0zl1FpJpbKrEFxjIYLyCqL9Aool0=,tag:LHcpHCXAHe8p2kOvOnKXyw==,type:str] +turn: + env: ENC[AES256_GCM,data:kt5nhVo9pb/ZbPUEcqSYXxN9YMgQKnFb5VRfFFS/qoIaJ73uD2fuJKqcxAyVRrdLqnSAWSQBgTgunBzdP7xqLAK2qt8DYAQWHkIe9uxFbSXZpdmw,iv:9lq6SFwTFN4GGm6gPiJpUMasMdnHVF6XLGYrsyG3kjU=,tag:428Qf9DOiiHt/Wjb188b8g==,type:str] + secret: ENC[AES256_GCM,data:si7ee6Xfhdgdyzbp6aQpF7pz3TmTBb7iQ82lRPVXNDg9JfHI+lbmgAsSnRLX5qMCA6P9R045sSMosqidL8QwRg==,iv:SrhpZKK8D45yxCEfDb9P3TwtA14+qEI+wcRqcN/a6pw=,tag:PiwV+mOL9xHJgJft6sc61g==,type:str] sops: kms: [] gcp_kms: [] azure_kv: [] hc_vault: [] age: [] - lastmodified: "2022-10-12T13:13:37Z" - mac: ENC[AES256_GCM,data:+EuA0rblxZYk+0tZs3vUFtr1cVKhdrLi4Ww0QjeITZn2k+SL8Y2gRl3gNVQOe00WHUgSKN53QKhxDj4q6Rd0LfwASxRRjz78Mk8yHDRDIfdDS960EasgKON4HPW/eMd2Fp4+flv57KYywQQWp3AlD8JqxIf5wNhyywn5LlW3PCQ=,iv:YFIk0LrRjV8417QJ5cp5EuIm7bezyG8ZulKcu1xhIF4=,tag:vtq5hCuLEXOvRjE2D/5cCQ==,type:str] + lastmodified: "2022-10-28T22:53:26Z" + mac: ENC[AES256_GCM,data:fNcL/hQUsjSDULz9hOrr8268wNfRzARvuD+QQoUjhj5oDN3/zq800FjHrMBygTr962ueYdpfF7rW3exJqsf0TRVxlrSfvIX0U6Hwgpx7Ts6vMAmJeYdr28EuvbITuq9k14jhJrWmcqHOplJFSE1SoBedxF9LLor5gsc8WZDuBm8=,iv:d3roSZ//iosFzLj6dDWQl35Ya0Vlu2M9bAsWVWorBjI=,tag:8sX4R0WRoifYMJXiamSm9w==,type:str] pgp: - created_at: "2022-10-12T00:46:51Z" enc: | @@ -47,4 +50,4 @@ sops: -----END PGP MESSAGE----- fp: 8a3737d48f1035fe6c3a0a8fd6a1976ca74c7f3b unencrypted_suffix: _unencrypted - version: 3.7.2 + version: 3.7.3 diff --git a/keys/staging.yaml b/keys/staging.yaml index 14a683a..9adfde8 100644 --- a/keys/staging.yaml +++ b/keys/staging.yaml @@ -2,14 +2,17 @@ nextcloud: tlater: ENC[AES256_GCM,data:91kDcO4hpng=,iv:ayuILRmRru4ZxTCur9H2xHuLjkDzwPdS/4lEog/tesU=,tag:qYhJxnNDcCwUM7xe7Tlcjw==,type:str] steam: tlater: ENC[AES256_GCM,data:jcW4wacGzOQ=,iv:KstKGHflscSWDFXGbnAZUcsqGN4Ot+w7sRbsAUwZNHQ=,tag:n9sRWvaKSgagpIgV/NF/Og==,type:str] +turn: + env: ENC[AES256_GCM,data:xjIz/AY109lyiL5N01p5T3HcYco/rM5CJSRTtg==,iv:16bW6OpyOK/QL0QPGQp/Baa9xyT8E3ZsYkwqmjuofk0=,tag:J5re3uKxIykw3YunvQWBgg==,type:str] + secret: ENC[AES256_GCM,data:eQ7dAocoZtg=,iv:fgzjTPv30WqTKlLy+yMn5MsKQgjhPnwlGFFwYEg3gWs=,tag:1ze33U1NBkgMX/9SiaBNQg==,type:str] sops: kms: [] gcp_kms: [] azure_kv: [] hc_vault: [] age: [] - lastmodified: "2022-10-12T13:14:20Z" - mac: ENC[AES256_GCM,data:IlU8Jr+HD/ZHHsd7eaaSGp3tRxGy8/yhbSejkWmHFeL1WsvdWsToHM7yah2WzX+uY7s/i7atHQdhbHITCi6gBIFociVVPwziK5YOmTXv1fHlcD60U4ClRbTtgMVMtvc5tXrxdLQGhaX+DJ5xXBhTlCSwwqgYP0I7vJmEUF9mz7g=,iv:IM1ebqQB1UO5EN92kipHL20iGtFTKJhUiN/XR6psWBM=,tag:WweauZ5pA7/YMuUuq8C/xQ==,type:str] + lastmodified: "2022-10-28T22:54:01Z" + mac: ENC[AES256_GCM,data:1nsv+Dl7lzRZNNb9kSuqFrXrcncIklw/A2uwd/yQQ546Rm/4gzpBZqCi6cv5VBCdc1iNuBcAM74DnZHMDmeWAiW0WfACPJMQjCes21P6IUsP2gu+bV2f9qqqnP2a5voxzFHp1aclklzMiiZJBEB1Y3UNz0ZG7A43hsOAE0/fJ9o=,iv:kY10PF5ErkKHXx8m0OyX2eU6kcFQsrsP3V2scVBMsuA=,tag:Uth0XfP2c0LBJQ7+7Uc0BQ==,type:str] pgp: - created_at: "2022-10-12T16:48:23Z" enc: | @@ -47,4 +50,4 @@ sops: -----END PGP MESSAGE----- fp: 7762ec55a5727cabada621d961e53f94caa314e4 unencrypted_suffix: _unencrypted - version: 3.7.2 + version: 3.7.3