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