| author | |
| committer | |
| log | 5cfea3f1ff45315f1533925d70d966845fdc7cce |
| tree | b1f1bfd945fd9213c3c0d0de531aa199d92d90ad |
| parent | 978a08d526f29bdf8e6ecc15dfa684ef37a7c31f |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Run isolated previews and a paperclover.net production account with managed TLS, DNS discovery, persistent firewall rules and generated secrets. Support multiple native service ports without changing existing jobs.
Assisted-by: gpt-618 files changed, 512 insertions(+), 31 deletions(-)
config/Service.pkl+1-1| ... | @@ -78,7 +78,7 @@ class Container { | ... | @@ -78,7 +78,7 @@ class Container { |
| 78 | build: String? | 78 | build: String? |
| 79 | entrypoint: String? | 79 | entrypoint: String? |
| 80 | http: Http? | 80 | http: Http? |
| 81 | tcp: Tcp? | 81 | tcp: Listing<Tcp> = new {} |
| 82 | volumes: Mapping<String, Volume> = new {} | 82 | volumes: Mapping<String, Volume> = new {} |
| 83 | /// Podman tmpfs mounts for data that must not persist in service storage. | 83 | /// Podman tmpfs mounts for data that must not persist in service storage. |
| 84 | tmpfs: Listing<String> = new {} | 84 | tmpfs: Listing<String> = new {} |
nixos/zenith.nix+4-1| ... | @@ -2,6 +2,7 @@ | ... | @@ -2,6 +2,7 @@ |
| 2 | let | 2 | let |
| 3 | adminKey = lib.fileContents ../config/admin.pub; | 3 | adminKey = lib.fileContents ../config/admin.pub; |
| 4 | pool = "globe"; | 4 | pool = "globe"; |
| 5 | xmppNetwork = builtins.fromJSON (builtins.readFile ../service/xmpp/network.json); | ||
| 5 | encryptionRoots = { | 6 | encryptionRoots = { |
| 6 | apps = "apps"; | 7 | apps = "apps"; |
| 7 | clover = "clover"; | 8 | clover = "clover"; |
| ... | @@ -16,7 +17,9 @@ in | ... | @@ -16,7 +17,9 @@ in |
| 16 | 17 | ||
| 17 | networking.hostName = "zenith"; | 18 | networking.hostName = "zenith"; |
| 18 | networking.hostId = "4fa19ccb"; | 19 | networking.hostId = "4fa19ccb"; |
| 19 | networking.firewall.allowedTCPPorts = [ 80 443 ]; | 20 | networking.firewall.allowedTCPPorts = [ 80 443 ] ++ map (name: xmppNetwork.${name}) [ "c2s" "c2s_tls" "s2s" "turn" "turn_tls" ]; |
| 21 | networking.firewall.allowedUDPPorts = [ xmppNetwork.turn xmppNetwork.turn_tls ]; | ||
| 22 | networking.firewall.allowedUDPPortRanges = [{ from = xmppNetwork.relay_first; to = xmppNetwork.relay_last; }]; | ||
| 20 | networking.firewall.interfaces.enp4s0.allowedTCPPorts = [ 445 ]; | 23 | networking.firewall.interfaces.enp4s0.allowedTCPPorts = [ 445 ]; |
| 21 | 24 | ||
| 22 | boot.loader.systemd-boot.enable = true; | 25 | boot.loader.systemd-boot.enable = true; |
service/dawarich/service.pkl+5-3| ... | @@ -122,9 +122,11 @@ containers { | ... | @@ -122,9 +122,11 @@ containers { |
| 122 | args { "redis-server"; "--save"; "900"; "1"; "--appendonly"; "no" } | 122 | args { "redis-server"; "--save"; "900"; "1"; "--appendonly"; "no" } |
| 123 | memory = 256 | 123 | memory = 256 |
| 124 | tcp { | 124 | tcp { |
| 125 | name = "redis" | 125 | new { |
| 126 | containerPort = 6379 | 126 | name = "redis" |
| 127 | loopback = false | 127 | containerPort = 6379 |
| 128 | loopback = false | ||
| 129 | } | ||
| 128 | } | 130 | } |
| 129 | volumes { ["/data"] {} } | 131 | volumes { ["/data"] {} } |
| 130 | } | 132 | } |
service/ddns-updater/provide.py created+44| ... | @@ -0,0 +1,44 @@ | ||
| 1 | #!/usr/bin/env python3 | ||
| 2 | import json | ||
| 3 | import sys | ||
| 4 | import urllib.parse | ||
| 5 | import urllib.request | ||
| 6 | |||
| 7 | data = json.load(sys.stdin) | ||
| 8 | if data["request"]["kind"] != "dns-credentials": | ||
| 9 | raise ValueError("unsupported DNS input") | ||
| 10 | secrets = data["providerSecrets"] | ||
| 11 | base = "https://api.cloudflare.com/client/v4/zones/" + secrets["cloudflare_zone_id"] + "/dns_records" | ||
| 12 | |||
| 13 | |||
| 14 | def request(method, path, body=None): | ||
| 15 | req = urllib.request.Request(base + path, method=method, headers={ | ||
| 16 | "Authorization": "Bearer " + secrets["cloudflare_api_token"], | ||
| 17 | "Content-Type": "application/json", | ||
| 18 | }, data=json.dumps(body).encode() if body is not None else None) | ||
| 19 | with urllib.request.urlopen(req, timeout=30) as response: | ||
| 20 | result = json.load(response) | ||
| 21 | if not result["success"]: | ||
| 22 | raise RuntimeError("Cloudflare DNS request failed") | ||
| 23 | return result["result"] | ||
| 24 | |||
| 25 | |||
| 26 | for record in data["request"].get("srv", []): | ||
| 27 | records = request("GET", "?" + urllib.parse.urlencode({"type": "SRV", "name": record["name"]})) | ||
| 28 | if len(records) > 1: | ||
| 29 | raise ValueError("multiple SRV records exist for " + record["name"]) | ||
| 30 | if data.get("operation") == "delete": | ||
| 31 | if records: | ||
| 32 | request("DELETE", "/" + records[0]["id"]) | ||
| 33 | continue | ||
| 34 | body = { | ||
| 35 | "type": "SRV", "name": record["name"], "ttl": 300, | ||
| 36 | "data": {"priority": 0, "weight": 0, "port": record["port"], "target": record["target"]}, | ||
| 37 | } | ||
| 38 | if records: | ||
| 39 | request("PUT", "/" + records[0]["id"], body) | ||
| 40 | else: | ||
| 41 | request("POST", "", body) | ||
| 42 | |||
| 43 | if data.get("operation") != "delete": | ||
| 44 | print(json.dumps({"api_token": secrets["cloudflare_api_token"]})) | ||
service/ddns-updater/service.pkl+16-1| ... | @@ -1,8 +1,23 @@ | ... | @@ -1,8 +1,23 @@ |
| 1 | amends "../../config/Service.pkl" | 1 | extends "../../config/Service.pkl" |
| 2 | 2 | ||
| 3 | import "../../config/site.pkl" as site | 3 | import "../../config/site.pkl" as site |
| 4 | import "../../config/Service.pkl" as service | ||
| 5 | |||
| 6 | class SrvRecord { | ||
| 7 | name: String | ||
| 8 | target: String | ||
| 9 | port: UInt16 | ||
| 10 | } | ||
| 11 | |||
| 12 | class DnsCredentials extends service.Requirement { | ||
| 13 | alias: String = "dns" | ||
| 14 | fixed provider = "ddns-updater" | ||
| 15 | fixed kind = "dns-credentials" | ||
| 16 | srv: Listing<SrvRecord> = new {} | ||
| 17 | } | ||
| 4 | 18 | ||
| 5 | meta { name = "DDNS" } | 19 | meta { name = "DDNS" } |
| 20 | provide = "provide.py" | ||
| 6 | enabled = !site.preview && !site.domain.endsWith(".test") | 21 | enabled = !site.preview && !site.domain.endsWith(".test") |
| 7 | 22 | ||
| 8 | requiredSecrets { "cloudflare_zone_id"; "cloudflare_api_token" } | 23 | requiredSecrets { "cloudflare_zone_id"; "cloudflare_api_token" } |
service/jackett/service.pkl+6-4| ... | @@ -14,10 +14,12 @@ container { | ... | @@ -14,10 +14,12 @@ container { |
| 14 | checkPath = "/health" | 14 | checkPath = "/health" |
| 15 | } | 15 | } |
| 16 | tcp { | 16 | tcp { |
| 17 | name = "internal" | 17 | new { |
| 18 | containerPort = 9117 | 18 | name = "internal" |
| 19 | hostPort = 30017 | 19 | containerPort = 9117 |
| 20 | loopback = false | 20 | hostPort = 30017 |
| 21 | loopback = false | ||
| 22 | } | ||
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | volumes { | 25 | volumes { |
service/postgres/service.pkl+6-4| ... | @@ -24,10 +24,12 @@ container { | ... | @@ -24,10 +24,12 @@ container { |
| 24 | cpu = 300 | 24 | cpu = 300 |
| 25 | 25 | ||
| 26 | tcp { | 26 | tcp { |
| 27 | name = "db" | 27 | new { |
| 28 | containerPort = 5432 | 28 | name = "db" |
| 29 | hostPort = 15432 | 29 | containerPort = 5432 |
| 30 | loopback = false | 30 | hostPort = 15432 |
| 31 | loopback = false | ||
| 32 | } | ||
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | volumes { | 35 | volumes { |
service/qbittorrent/service.pkl+6-4| ... | @@ -26,10 +26,12 @@ container { | ... | @@ -26,10 +26,12 @@ container { |
| 26 | authRole = "media-manage" | 26 | authRole = "media-manage" |
| 27 | } | 27 | } |
| 28 | tcp { | 28 | tcp { |
| 29 | name = "internal" | 29 | new { |
| 30 | containerPort = 23938 | 30 | name = "internal" |
| 31 | hostPort = 30038 | 31 | containerPort = 23938 |
| 32 | loopback = false | 32 | hostPort = 30038 |
| 33 | loopback = false | ||
| 34 | } | ||
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | volumes { | 37 | volumes { |
service/samba/service.pkl+6-4| ... | @@ -14,10 +14,12 @@ container { | ... | @@ -14,10 +14,12 @@ container { |
| 14 | memory = 256 | 14 | memory = 256 |
| 15 | 15 | ||
| 16 | tcp { | 16 | tcp { |
| 17 | name = "smb" | 17 | new { |
| 18 | containerPort = 445 | 18 | name = "smb" |
| 19 | hostPort = 445 | 19 | containerPort = 445 |
| 20 | loopback = false | 20 | hostPort = 445 |
| 21 | loopback = false | ||
| 22 | } | ||
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | volumes { | 25 | volumes { |
service/xmpp/Dockerfile created+10| ... | @@ -0,0 +1,10 @@ | ||
| 1 | FROM docker.io/prosodyim/prosody@sha256:0d4efc7c11018dfd863c9a25495efe706027de357a58a18ef02f8c484fb264ed | ||
| 2 | RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
| 3 | python3 certbot python3-certbot-dns-cloudflare coturn \ | ||
| 4 | && rm -rf /var/lib/apt/lists/* | ||
| 5 | ADD https://hg.prosody.im/prosody-modules/archive/bd56316b2b1389fe57dbd78f93ac4fe8428dee5e.tar.gz /tmp/modules.tar.gz | ||
| 6 | RUN echo '18c122cd7a294a052c062fc65440ee9e289e8212f71e68ab3442f3c74004f285 /tmp/modules.tar.gz' | sha256sum -c - \ | ||
| 7 | && mkdir /opt/prosody-modules \ | ||
| 8 | && tar -xzf /tmp/modules.tar.gz -C /opt/prosody-modules --strip-components=1 \ | ||
| 9 | && rm /tmp/modules.tar.gz | ||
| 10 | COPY network.json /etc/xmpp-network.json | ||
service/xmpp/network.json created+9| ... | @@ -0,0 +1,9 @@ | ||
| 1 | { | ||
| 2 | "c2s": 5222, | ||
| 3 | "c2s_tls": 5223, | ||
| 4 | "s2s": 5269, | ||
| 5 | "turn": 3478, | ||
| 6 | "turn_tls": 5349, | ||
| 7 | "relay_first": 49152, | ||
| 8 | "relay_last": 49183 | ||
| 9 | } | ||
service/xmpp/prosody.cfg.lua created+72| ... | @@ -0,0 +1,72 @@ | ||
| 1 | local domain = ENV_XMPP_DOMAIN | ||
| 2 | local host = ENV_XMPP_HOST | ||
| 3 | local network = Lua.require("prosody.util.json").decode(Lua.io.open("/etc/xmpp-network.json"):read("*a")) | ||
| 4 | |||
| 5 | data_path = "/var/lib/prosody" | ||
| 6 | pidfile = "/var/lib/prosody/prosody.pid" | ||
| 7 | plugin_paths = { "/opt/prosody-modules", "/studio" } | ||
| 8 | admins = { "clover@" .. domain } | ||
| 9 | bootstrap_password = ENV_XMPP_PASSWORD | ||
| 10 | authentication = "internal_hashed" | ||
| 11 | allow_registration = false | ||
| 12 | storage = "sql" | ||
| 13 | sql = { driver = "SQLite3", database = "/var/lib/prosody/prosody.sqlite" } | ||
| 14 | archive_expires_after = "never" | ||
| 15 | default_archive_policy = true | ||
| 16 | |||
| 17 | modules_enabled = { | ||
| 18 | "disco", "roster", "saslauth", "tls", "blocklist", "bookmarks", | ||
| 19 | "carbons", "limits", "pep", "private", "smacks", "vcard4", "vcard_legacy", | ||
| 20 | "csi_simple", "mam", "ping", "time", "version", "uptime", | ||
| 21 | "invites", "invites_adhoc", "invites_register", "register", | ||
| 22 | "admin_adhoc", "admin_shell", "cloud_notify", "cloud_notify_extensions", | ||
| 23 | "sasl2", "sasl2_bind2", "sasl2_sm", "sasl2_fast", | ||
| 24 | "bosh", "websocket", "http_health", "http_openmetrics", "turn_external", | ||
| 25 | "bootstrap" | ||
| 26 | } | ||
| 27 | log = { info = "*console" } | ||
| 28 | statistics = "internal" | ||
| 29 | http_openmetrics_public = false | ||
| 30 | push_notification_with_body = false | ||
| 31 | push_notification_with_sender = false | ||
| 32 | |||
| 33 | c2s_ports = { network.c2s } | ||
| 34 | c2s_direct_tls_ports = { network.c2s_tls } | ||
| 35 | s2s_ports = { network.s2s } | ||
| 36 | s2s_secure_auth = true | ||
| 37 | c2s_require_encryption = true | ||
| 38 | s2s_require_encryption = true | ||
| 39 | ssl = { key = "/tls/live/server/privkey.pem", certificate = "/tls/live/server/fullchain.pem" } | ||
| 40 | certificates = "/tls/live/server" | ||
| 41 | component_ports = {} | ||
| 42 | https_ports = {} | ||
| 43 | http_ports = { Lua.tonumber(ENV_HTTP_PORT) } | ||
| 44 | http_interfaces = { "127.0.0.1" } | ||
| 45 | http_default_host = host | ||
| 46 | http_host = host | ||
| 47 | http_external_url = "https://" .. host .. "/" | ||
| 48 | consider_bosh_secure = true | ||
| 49 | consider_websocket_secure = true | ||
| 50 | trusted_proxies = { "127.0.0.1", "::1" } | ||
| 51 | |||
| 52 | turn_external_host = host | ||
| 53 | turn_external_port = network.turn | ||
| 54 | turn_external_tls_port = network.turn_tls | ||
| 55 | turn_external_tcp = true | ||
| 56 | turn_external_secret = ENV_TURN_SECRET | ||
| 57 | |||
| 58 | VirtualHost(domain) | ||
| 59 | Component("groups." .. domain) "muc" | ||
| 60 | modules_enabled = { "muc_mam", "muc_offline_delivery" } | ||
| 61 | restrict_room_creation = "local" | ||
| 62 | muc_room_default_persistent = true | ||
| 63 | muc_room_default_members_only = true | ||
| 64 | muc_room_default_public = false | ||
| 65 | muc_room_default_public_jids = true | ||
| 66 | muc_log_by_default = true | ||
| 67 | Component("upload." .. domain) "http_file_share" | ||
| 68 | modules_disabled = { "s2s" } | ||
| 69 | http_file_share_size_limit = 100 * 1024 * 1024 + 16 | ||
| 70 | http_file_share_daily_quota = 1024 * 1024 * 1024 | ||
| 71 | http_file_share_global_quota = 10 * 1024 * 1024 * 1024 | ||
| 72 | http_file_share_expires_after = "never" | ||
service/xmpp/runtime/mod_bootstrap.lua created+9| ... | @@ -0,0 +1,9 @@ | ||
| 1 | local usermanager = require "core.usermanager" | ||
| 2 | local jid = require "util.jid" | ||
| 3 | local username, host = jid.split(module:get_option_array("admins")[1]) | ||
| 4 | |||
| 5 | module:hook_global("server-started", function() | ||
| 6 | if not usermanager.user_exists(username, host) then | ||
| 7 | assert(usermanager.create_user(username, module:get_option_string("bootstrap_password"), host)) | ||
| 8 | end | ||
| 9 | end) | ||
service/xmpp/runtime/run.py created+103| ... | @@ -0,0 +1,103 @@ | ||
| 1 | #!/usr/bin/env python3 | ||
| 2 | import json | ||
| 3 | import os | ||
| 4 | from pathlib import Path | ||
| 5 | import signal | ||
| 6 | import socket | ||
| 7 | import sqlite3 | ||
| 8 | import subprocess | ||
| 9 | import sys | ||
| 10 | import time | ||
| 11 | |||
| 12 | |||
| 13 | os.umask(0o077) | ||
| 14 | network = json.loads(Path("/etc/xmpp-network.json").read_text()) | ||
| 15 | host = os.environ["XMPP_HOST"] | ||
| 16 | certificate = Path("/tls/live/server/fullchain.pem") | ||
| 17 | key = certificate.with_name("privkey.pem") | ||
| 18 | mode = sys.argv[1] | ||
| 19 | |||
| 20 | if mode == "certificates": | ||
| 21 | domain = os.environ["XMPP_DOMAIN"] | ||
| 22 | names = list(dict.fromkeys([domain, host, "groups." + domain])) | ||
| 23 | if host.endswith(".test"): | ||
| 24 | certificate.parent.mkdir(parents=True, exist_ok=True) | ||
| 25 | if not certificate.exists(): | ||
| 26 | subprocess.run([ | ||
| 27 | "openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "30", | ||
| 28 | "-subj", "/CN=" + domain, "-addext", "subjectAltName=" + ",".join("DNS:" + name for name in names), | ||
| 29 | "-keyout", str(key), "-out", str(certificate), | ||
| 30 | ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||
| 31 | while True: | ||
| 32 | time.sleep(3600) | ||
| 33 | credentials = Path("/tmp/cloudflare.ini") | ||
| 34 | credentials.write_text("dns_cloudflare_api_token = " + os.environ.pop("DNS_TOKEN") + "\n") | ||
| 35 | command = [ | ||
| 36 | "certbot", "certonly", "--non-interactive", "--agree-tos", "--email", os.environ["OWNER_EMAIL"], | ||
| 37 | "--dns-cloudflare", "--dns-cloudflare-credentials", str(credentials), | ||
| 38 | "--dns-cloudflare-propagation-seconds", "30", "--cert-name", "server", | ||
| 39 | "--config-dir", "/tls", "--work-dir", "/tmp/certbot-work", "--logs-dir", "/tmp/certbot-logs", | ||
| 40 | "--keep-until-expiring", *[arg for name in names for arg in ("-d", name)], | ||
| 41 | ] | ||
| 42 | while True: | ||
| 43 | subprocess.run(command, check=not certificate.exists()) | ||
| 44 | time.sleep(12 * 60 * 60) | ||
| 45 | |||
| 46 | while not certificate.exists() or not key.exists(): | ||
| 47 | time.sleep(1) | ||
| 48 | |||
| 49 | if mode == "prosody": | ||
| 50 | command = ["prosody", "-F", "--config", "/etc/prosody/prosody.cfg.lua"] | ||
| 51 | elif mode == "turn": | ||
| 52 | address = "1.1.1.1" if host.endswith(".test") else socket.gethostbyname(host) | ||
| 53 | with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as route: | ||
| 54 | route.connect((address, network["turn"])) | ||
| 55 | local_ip = route.getsockname()[0] | ||
| 56 | with sqlite3.connect("/tmp/turn.sqlite") as database: | ||
| 57 | database.executescript(Path("/usr/share/coturn/schema.sql").read_text()) | ||
| 58 | # A public/private external-ip mapping whitelists the private address as a relay peer. | ||
| 59 | config = Path("/tmp/turnserver.conf") | ||
| 60 | config.write_text("\n".join([ | ||
| 61 | "listening-port=" + str(network["turn"]), "tls-listening-port=" + str(network["turn_tls"]), | ||
| 62 | "min-port=" + str(network["relay_first"]), "max-port=" + str(network["relay_last"]), | ||
| 63 | "listening-ip=0.0.0.0", "relay-ip=" + local_ip, | ||
| 64 | "external-ip=" + (local_ip if host.endswith(".test") else address), | ||
| 65 | "realm=" + host, "server-name=" + host, "fingerprint", "use-auth-secret", | ||
| 66 | "static-auth-secret=" + os.environ.pop("TURN_SECRET"), | ||
| 67 | "cert=" + str(certificate), "pkey=" + str(key), | ||
| 68 | "no-cli", "no-multicast-peers", "no-tcp-relay", "no-rfc5780", "no-software-attribute", | ||
| 69 | "user-quota=12", "total-quota=128", "log-file=stdout", "simple-log", | ||
| 70 | "pidfile=/tmp/turnserver.pid", "userdb=/tmp/turn.sqlite", "relay-threads=2", | ||
| 71 | *["denied-peer-ip=" + cidr for cidr in ( | ||
| 72 | "0.0.0.0-0.255.255.255", "10.0.0.0-10.255.255.255", "100.64.0.0-100.127.255.255", | ||
| 73 | "127.0.0.0-127.255.255.255", "169.254.0.0-169.254.255.255", | ||
| 74 | "172.16.0.0-172.31.255.255", "192.168.0.0-192.168.255.255", "224.0.0.0-255.255.255.255", | ||
| 75 | "::", "::1", "fc00::-fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff", | ||
| 76 | )], | ||
| 77 | ]) + "\n") | ||
| 78 | command = ["turnserver", "-c", str(config)] | ||
| 79 | else: | ||
| 80 | raise ValueError("unknown XMPP task") | ||
| 81 | |||
| 82 | process = subprocess.Popen(command) | ||
| 83 | |||
| 84 | |||
| 85 | def stop(signum, frame): | ||
| 86 | process.send_signal(signum) | ||
| 87 | |||
| 88 | |||
| 89 | signal.signal(signal.SIGTERM, stop) | ||
| 90 | signal.signal(signal.SIGINT, stop) | ||
| 91 | previous = (certificate.read_bytes(), key.read_bytes()) | ||
| 92 | while process.poll() is None: | ||
| 93 | time.sleep(2) | ||
| 94 | current = (certificate.read_bytes(), key.read_bytes()) | ||
| 95 | if current != previous: | ||
| 96 | previous = current | ||
| 97 | if mode == "prosody": | ||
| 98 | process.send_signal(signal.SIGHUP) | ||
| 99 | else: | ||
| 100 | process.terminate() | ||
| 101 | process.wait(timeout=20) | ||
| 102 | process = subprocess.Popen(command) | ||
| 103 | sys.exit(process.returncode) | ||
service/xmpp/service.pkl created+88| ... | @@ -0,0 +1,88 @@ | ||
| 1 | amends "../../config/Service.pkl" | ||
| 2 | |||
| 3 | import "../../config/site.pkl" as site | ||
| 4 | import "../ddns-updater/service.pkl" as dns | ||
| 5 | import "pkl:json" | ||
| 6 | |||
| 7 | local network = new json.Parser {}.parse(read("network.json").text) | ||
| 8 | local host = read?("prop:hostname") ?? "xmpp.\(site.domain)" | ||
| 9 | local accountDomain = if (site.preview) host else site.domain | ||
| 10 | |||
| 11 | meta { name = "XMPP"; launcher = false } | ||
| 12 | stageIsolation = "fresh" | ||
| 13 | healthyDeadline = "10m" | ||
| 14 | setup = "setup.py" | ||
| 15 | |||
| 16 | requirements = if (site.domain.endsWith(".test")) new Listing {} else new Listing { | ||
| 17 | new dns.DnsCredentials { | ||
| 18 | srv = if (site.preview) new Listing {} else new Listing { | ||
| 19 | new dns.SrvRecord { name = "_xmpp-client._tcp.\(site.domain)"; target = host; port = network.c2s } | ||
| 20 | new dns.SrvRecord { name = "_xmpps-client._tcp.\(site.domain)"; target = host; port = network.c2s_tls } | ||
| 21 | new dns.SrvRecord { name = "_xmpp-server._tcp.\(site.domain)"; target = host; port = network.s2s } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | secrets { ["password"] {}; ["turn"] {} } | ||
| 26 | |||
| 27 | containers { | ||
| 28 | ["certificates"] { | ||
| 29 | build = "." | ||
| 30 | entrypoint = "/usr/bin/python3" | ||
| 31 | args { "/studio/run.py"; "certificates" } | ||
| 32 | hostNetwork = true | ||
| 33 | lifecycle = "prestartSidecar" | ||
| 34 | cpu = 100 | ||
| 35 | memory = 256 | ||
| 36 | volumes { ["/tls"] {}; ["/studio"] { config = "runtime" } } | ||
| 37 | env { | ||
| 38 | ["XMPP_DOMAIN"] = accountDomain | ||
| 39 | ["XMPP_HOST"] = host | ||
| 40 | ["OWNER_EMAIL"] = site.ownerEmail | ||
| 41 | ["DNS_TOKEN"] = if (site.domain.endsWith(".test")) "" else "${secret.dns.api_token}" | ||
| 42 | } | ||
| 43 | } | ||
| 44 | ["prosody"] { | ||
| 45 | build = "." | ||
| 46 | entrypoint = "/usr/bin/python3" | ||
| 47 | args { "/studio/run.py"; "prosody" } | ||
| 48 | hostNetwork = true | ||
| 49 | memory = 512 | ||
| 50 | http { containerPort = 5280; hostname = host; loopback = true; checkPath = "/health"; forwardRealIp = true } | ||
| 51 | tcp { | ||
| 52 | new { name = "c2s"; containerPort = network.c2s; hostPort = containerPort; loopback = false } | ||
| 53 | new { name = "c2stls"; containerPort = network.c2s_tls; hostPort = containerPort; loopback = false } | ||
| 54 | new { name = "s2s"; containerPort = network.s2s; hostPort = containerPort; loopback = false } | ||
| 55 | } | ||
| 56 | volumes { | ||
| 57 | ["/var/lib/prosody"] {} | ||
| 58 | ["/tls"] { readOnly = true } | ||
| 59 | ["/studio"] { config = "runtime" } | ||
| 60 | ["/etc/prosody/prosody.cfg.lua"] { config = "prosody.cfg.lua" } | ||
| 61 | } | ||
| 62 | env { | ||
| 63 | ["XMPP_DOMAIN"] = accountDomain | ||
| 64 | ["XMPP_HOST"] = host | ||
| 65 | ["XMPP_PASSWORD"] = "${secret.own.password}" | ||
| 66 | ["TURN_SECRET"] = "${secret.own.turn}" | ||
| 67 | } | ||
| 68 | envTemplate = """ | ||
| 69 | HTTP_PORT={{ env "NOMAD_PORT_prosody_http" }} | ||
| 70 | """ | ||
| 71 | } | ||
| 72 | ["turn"] { | ||
| 73 | build = "." | ||
| 74 | entrypoint = "/usr/bin/python3" | ||
| 75 | args { "/studio/run.py"; "turn" } | ||
| 76 | hostNetwork = true | ||
| 77 | memory = 256 | ||
| 78 | tcp { | ||
| 79 | new { name = "turn"; containerPort = network.turn; hostPort = containerPort; loopback = false } | ||
| 80 | new { name = "turntls"; containerPort = network.turn_tls; hostPort = containerPort; loopback = false } | ||
| 81 | for (port in IntSeq(network.relay_first, network.relay_last)) { | ||
| 82 | new { name = "relay-\(port)"; containerPort = port; hostPort = port; loopback = false } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | volumes { ["/tls"] { readOnly = true }; ["/studio"] { config = "runtime" } } | ||
| 86 | env { ["XMPP_HOST"] = host; ["TURN_SECRET"] = "${secret.own.turn}" } | ||
| 87 | } | ||
| 88 | } | ||
service/xmpp/setup.py created+53| ... | @@ -0,0 +1,53 @@ | ||
| 1 | #!/usr/bin/env python3 | ||
| 2 | import json | ||
| 3 | from pathlib import Path | ||
| 4 | import signal | ||
| 5 | import subprocess | ||
| 6 | import sys | ||
| 7 | import time | ||
| 8 | |||
| 9 | |||
| 10 | if len(sys.argv) == 1: | ||
| 11 | data = json.load(sys.stdin) | ||
| 12 | if data["preview"]: | ||
| 13 | unit = data["serviceId"] + "-network" | ||
| 14 | subprocess.run(["systemctl", "stop", unit], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||
| 15 | subprocess.run([ | ||
| 16 | "systemd-run", "--collect", "--unit=" + unit, | ||
| 17 | "--setenv=PATH=/run/current-system/sw/bin:/usr/bin:/bin", | ||
| 18 | sys.executable, str(Path(__file__).resolve()), data["serviceId"], | ||
| 19 | ], check=True) | ||
| 20 | time.sleep(1) | ||
| 21 | subprocess.run(["systemctl", "is-active", "--quiet", unit], check=True) | ||
| 22 | sys.exit() | ||
| 23 | |||
| 24 | stage = sys.argv[1] | ||
| 25 | metadata = Path("/var/lib/studio/stages") / (stage + ".json") | ||
| 26 | network = json.loads(Path(__file__).with_name("network.json").read_text()) | ||
| 27 | rules = [ | ||
| 28 | ["-p", "tcp", "-m", "multiport", "--dports", ",".join(str(network[key]) for key in ( | ||
| 29 | "c2s", "c2s_tls", "s2s", "turn", "turn_tls", | ||
| 30 | ))], | ||
| 31 | ["-p", "udp", "-m", "multiport", "--dports", f'{network["turn"]},{network["turn_tls"]}'], | ||
| 32 | ["-p", "udp", "--dport", f'{network["relay_first"]}:{network["relay_last"]}'], | ||
| 33 | ] | ||
| 34 | rules = [rule + ["-m", "comment", "--comment", stage, "-j", "nixos-fw-accept"] for rule in rules] | ||
| 35 | |||
| 36 | |||
| 37 | def stop(signum, frame): | ||
| 38 | raise SystemExit() | ||
| 39 | |||
| 40 | |||
| 41 | signal.signal(signal.SIGTERM, stop) | ||
| 42 | signal.signal(signal.SIGINT, stop) | ||
| 43 | try: | ||
| 44 | while metadata.exists(): | ||
| 45 | for command in ("iptables", "ip6tables"): | ||
| 46 | for rule in rules: | ||
| 47 | if subprocess.run([command, "-w", "-C", "nixos-fw", *rule], capture_output=True).returncode: | ||
| 48 | subprocess.run([command, "-w", "-I", "nixos-fw", "1", *rule], check=True) | ||
| 49 | time.sleep(10) | ||
| 50 | finally: | ||
| 51 | for command in ("iptables", "ip6tables"): | ||
| 52 | for rule in rules: | ||
| 53 | subprocess.run([command, "-w", "-D", "nixos-fw", *rule], capture_output=True) | ||
tools/studio.py+11-9| ... | @@ -258,7 +258,7 @@ def render(data, definitions): | ... | @@ -258,7 +258,7 @@ def render(data, definitions): |
| 258 | for task in data["containers"].values(): | 258 | for task in data["containers"].values(): |
| 259 | if task["hostNetwork"] or any( | 259 | if task["hostNetwork"] or any( |
| 260 | endpoint and endpoint.get("hostPort") is not None | 260 | endpoint and endpoint.get("hostPort") is not None |
| 261 | for endpoint in (task.get("http"), task.get("tcp")) | 261 | for endpoint in (task.get("http"), *task["tcp"]) |
| 262 | ) or any(not volume["readOnly"] for volume in task["volumes"].values()): | 262 | ) or any(not volume["readOnly"] for volume in task["volumes"].values()): |
| 263 | raise ValueError(f"overlapped rollout requires dynamic ports and read-only mounts: {name}") | 263 | raise ValueError(f"overlapped rollout requires dynamic ports and read-only mounts: {name}") |
| 264 | elif data["rollout"] != "simple": | 264 | elif data["rollout"] != "simple": |
| ... | @@ -278,11 +278,11 @@ def render(data, definitions): | ... | @@ -278,11 +278,11 @@ def render(data, definitions): |
| 278 | if host_network and not all(task["hostNetwork"] for task in data["containers"].values()): | 278 | if host_network and not all(task["hostNetwork"] for task in data["containers"].values()): |
| 279 | raise ValueError("mixed host and bridge networking in one group") | 279 | raise ValueError("mixed host and bridge networking in one group") |
| 280 | for task_name, task in data["containers"].items(): | 280 | for task_name, task in data["containers"].items(): |
| 281 | for kind in ("http", "tcp"): | 281 | for kind, endpoint in [("http", task.get("http")), *((port["name"], port) for port in task["tcp"])]: |
| 282 | if task.get(kind): | 282 | if endpoint: |
| 283 | endpoint = task[kind] | 283 | label = kind if len(data["containers"]) == 1 else f"{task_name}-{kind}" |
| 284 | port_name = endpoint["name"] if kind == "tcp" else kind | 284 | if label in ports.values(): |
| 285 | label = port_name if len(data["containers"]) == 1 else f"{task_name}-{port_name}" | 285 | raise ValueError(f"duplicate port label: {label}") |
| 286 | ports[(task_name, kind)] = label | 286 | ports[(task_name, kind)] = label |
| 287 | network.append(f" port {q(label)} {{") | 287 | network.append(f" port {q(label)} {{") |
| 288 | if endpoint.get("hostPort") is not None: | 288 | if endpoint.get("hostPort") is not None: |
| ... | @@ -326,8 +326,9 @@ def render(data, definitions): | ... | @@ -326,8 +326,9 @@ def render(data, definitions): |
| 326 | for task_name, task in data["containers"].items(): | 326 | for task_name, task in data["containers"].items(): |
| 327 | if task.get("http") or task.get("tcp"): | 327 | if task.get("http") or task.get("tcp"): |
| 328 | kind = "http" if task.get("http") else "tcp" | 328 | kind = "http" if task.get("http") else "tcp" |
| 329 | endpoint = task[kind] | 329 | endpoint = task["http"] if kind == "http" else task["tcp"][0] |
| 330 | lines += [" service {", f" name = {q(name if task_name == 'app' else name + '-' + task_name)}", ' provider = "nomad"', f" port = {q(ports[(task_name, kind)])}"] | 330 | label = ports[(task_name, "http" if kind == "http" else endpoint["name"])] |
| 331 | lines += [" service {", f" name = {q(name if task_name == 'app' else name + '-' + task_name)}", ' provider = "nomad"', f" port = {q(label)}"] | ||
| 331 | if len(data["containers"]) == 1: | 332 | if len(data["containers"]) == 1: |
| 332 | lines.append(f" task = {q(task_name)}") | 333 | lines.append(f" task = {q(task_name)}") |
| 333 | tags = [] | 334 | tags = [] |
| ... | @@ -1005,7 +1006,8 @@ def main(): | ... | @@ -1005,7 +1006,8 @@ def main(): |
| 1005 | raise ValueError("fresh stages cannot use writable external mounts") | 1006 | raise ValueError("fresh stages cannot use writable external mounts") |
| 1006 | for task in data["containers"].values(): | 1007 | for task in data["containers"].values(): |
| 1007 | if task.get("tcp") and not task["hostNetwork"]: | 1008 | if task.get("tcp") and not task["hostNetwork"]: |
| 1008 | task["tcp"]["hostPort"] = None | 1009 | for port in task["tcp"]: |
| 1010 | port["hostPort"] = None | ||
| 1009 | if hostname: | 1011 | if hostname: |
| 1010 | for task in data["containers"].values(): | 1012 | for task in data["containers"].values(): |
| 1011 | if task.get("http") and task["http"].get("hostname") == original_host: | 1013 | if task.get("http") and task["http"].get("hostname") == original_host: |
tools/xmpp.md created+63| ... | @@ -0,0 +1,63 @@ | ||
| 1 | # XMPP | ||
| 2 | |||
| 3 | Prosody and coturn run as Nomad/Podman tasks. The production account is | ||
| 4 | `clover@paperclover.net`; previews use their generated hostname as the account | ||
| 5 | domain. Each fresh preview has separate accounts, archives, uploads, certificates, | ||
| 6 | and generated secrets. The DNS provider supplies the existing Cloudflare token | ||
| 7 | through a protected Nomad input to the certificate task. | ||
| 8 | |||
| 9 | The configuration enables MAM history, message carbons, resumable streams, CSI, | ||
| 10 | PEP for OMEMO keys, modern bookmarks, private persistent groups with history, | ||
| 11 | 100 MiB uploads, mobile push extensions, SASL2/FAST, BOSH, WebSocket, and external | ||
| 12 | STUN/TURN discovery. Encryption of message contents is a client feature; enable | ||
| 13 | OMEMO in the client. Archives and uploads have no automatic expiry, with a 10 GiB | ||
| 14 | upload quota. Open account registration is disabled; the administrator can issue | ||
| 15 | invitations through XMPP ad-hoc commands. | ||
| 16 | |||
| 17 | Native listeners and the UDP relay range have one source in | ||
| 18 | [`network.json`](../service/xmpp/network.json). Only one XMPP instance can reserve | ||
| 19 | these host ports; stop its Nomad job before launching another on the same host. | ||
| 20 | `setup.py` installs a transient firewall service tagged with the preview ID. It | ||
| 21 | removes its rules within ten seconds of preview destruction, and on service stop. | ||
| 22 | After a host reboot, repeat the staging command to restore these runtime rules. | ||
| 23 | Production firewall rules are declared in `nixos/zenith.nix`. The DNS provider | ||
| 24 | provisions client, direct-TLS client, and federation SRV records pointing at | ||
| 25 | `xmpp.paperclover.net`; the website keeps its existing apex address. Router | ||
| 26 | forwarding must send the TCP listeners, UDP TURN listeners, and UDP relay range | ||
| 27 | to Zenith. Production activation uses the normal described-main deployment: | ||
| 28 | |||
| 29 | ```sh | ||
| 30 | STUDIO_DEPLOY_HOST=root@10.0.0.1 STUDIO_DEPLOY_PORT=22 python3 tools/deploy.py prod | ||
| 31 | ``` | ||
| 32 | |||
| 33 | ## Zenith preview, 2026-10-10 | ||
| 34 | |||
| 35 | Stage: `xmpp-preview-c035f9fb` | ||
| 36 | Hostname: `xmpp-preview-c035f9fb.paperclover.net` | ||
| 37 | JID: `clover@xmpp-preview-c035f9fb.paperclover.net` | ||
| 38 | Dataset: `globe/staging/xmpp-preview-c035f9fb` | ||
| 39 | |||
| 40 | The generated password is in Nomad variables. Clover's Mac also has an | ||
| 41 | owner-readable connection file at | ||
| 42 | `~/.local/share/snowglobe/xmpp-preview.json`; credentials are not tracked here. | ||
| 43 | Monal can use the preview JID and password with a manual server of `10.0.0.1`, | ||
| 44 | port `5223`, and direct TLS while on the home LAN. The public hostname serves | ||
| 45 | HTTPS media and WebSocket; public native connections require router forwarding | ||
| 46 | to Zenith for the listeners and UDP relay range in `network.json`. | ||
| 47 | |||
| 48 | Delete the preview, including its accounts and history: | ||
| 49 | |||
| 50 | ```sh | ||
| 51 | STUDIO_DEPLOY_HOST=root@10.0.0.1 STUDIO_DEPLOY_PORT=22 python3 tools/deploy.py destroy xmpp-preview-c035f9fb | ||
| 52 | ``` | ||
| 53 | |||
| 54 | Verified with synthetic data: certificate validation and TLS 1.3 login, two | ||
| 55 | simultaneous resources, carbons, stream resumption after transport loss, MAM | ||
| 56 | retrieval, PEP publication/deletion, private group creation/history/destruction, | ||
| 57 | PNG upload/download over public HTTPS, WebSocket stream opening, external-service | ||
| 58 | credentials, authenticated UDP TURN allocation/deallocation, advertised public | ||
| 59 | relay address/range, and rejection of a private-network peer. Push support was | ||
| 60 | discovered; delivery through an iOS client's push provider and calls across the | ||
| 61 | public router were not exercised. All 25 pre-existing service jobs rendered | ||
| 62 | byte-identically after the TCP schema migration. The active production release | ||
| 63 | was unchanged, and Caddy, Nomad, dashboard, and Shale checks passed. | ||