authoravatarclover caruso<git@paperclover.net>2026-10-17 04:26:48-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-21 02:52:29-07:00
log129b355b89c28ac75a69758310ca0e2f0a6b7d7c
tree5e828820c0a3efa0334d20691eff8172de2567ec
parentdb659de20444cfc5e23f5c0ec262e46a98d79768
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Discover the router UPnP endpoint for XMPP mappings


4 files changed, 205 insertions(+), 4 deletions(-)

nixos/snowglobe.nix+2-1
......@@ -40,7 +40,8 @@ in
4040 startAt = "*:0/5";
4141 serviceConfig = { Type = "oneshot"; DynamicUser = true; };
4242 script = ''
43 ${pkgs.miniupnpc}/bin/upnpc -u http://${config.networking.defaultGateway.address}:1900/whaoc/rootDesc.xml -e "Snow Globe XMPP" -r \
43 description="$(${pkgs.python3}/bin/python3 ${./upnp-discover.py} ${lib.escapeShellArg config.networking.defaultGateway.address})"
44 exec ${pkgs.miniupnpc}/bin/upnpc -u "$description" -e "Snow Globe XMPP" -r \
4445 ${lib.concatMapStringsSep " " (port: "${toString port} TCP") xmppTCPPorts} \
4546 ${lib.concatMapStringsSep " " (port: "${toString port} UDP") (xmppUDPPorts ++ lib.range xmppNetwork.relay_first xmppNetwork.relay_last)}
4647 '';
nixos/upnp-discover.py created+71
......@@ -0,0 +1,71 @@
1import ipaddress
2import socket
3import sys
4import time
5from urllib.parse import urlsplit
6
7
8def description_location(packet, sender, gateway):
9 if sender != (gateway, 1900) or len(packet) > 8192:
10 return None
11 try:
12 header, _ = packet.decode("ascii").split("\r\n\r\n", 1)
13 status, *lines = header.split("\r\n")
14 if status != "HTTP/1.1 200 OK":
15 return None
16 headers = {}
17 for line in lines:
18 name, value = line.split(":", 1)
19 if not name or any(not (c.isalnum() or c in "!#$%&'*+-.^_`|~") for c in name):
20 return None
21 name = name.lower()
22 if name in headers:
23 return None
24 headers[name] = value.strip(" \t")
25 if headers.get("st") != "upnp:rootdevice":
26 return None
27 location = headers["location"]
28 if any(ord(c) <= 32 or ord(c) >= 127 for c in location):
29 return None
30 url = urlsplit(location)
31 if (url.scheme != "http" or url.hostname != gateway or url.port == 0
32 or url.username is not None or url.password is not None
33 or url.fragment or not url.path.startswith("/")):
34 return None
35 return location
36 except (ValueError, KeyError):
37 return None
38
39
40def discover(gateway, timeout=3.0):
41 gateway = str(ipaddress.IPv4Address(gateway))
42 request = (
43 'M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\n'
44 'MAN: "ssdp:discover"\r\nMX: 1\r\nST: upnp:rootdevice\r\n\r\n'
45 ).encode("ascii")
46 deadline = time.monotonic() + timeout
47 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp:
48 udp.settimeout(timeout)
49 udp.sendto(request, (gateway, 1900))
50 for _ in range(64):
51 remaining = deadline - time.monotonic()
52 if remaining <= 0:
53 break
54 udp.settimeout(remaining)
55 try:
56 packet, sender = udp.recvfrom(8193)
57 except socket.timeout:
58 break
59 location = description_location(packet, sender, gateway)
60 if location is not None:
61 return location
62 raise TimeoutError(f"No valid UPnP description from {gateway}:1900")
63
64
65if __name__ == "__main__":
66 if len(sys.argv) != 2:
67 sys.exit("Usage: upnp-discover.py GATEWAY_IPV4")
68 try:
69 print(discover(sys.argv[1]))
70 except (OSError, ValueError) as error:
71 sys.exit(str(error))
tools/upnp-discover-test.py created+127
......@@ -0,0 +1,127 @@
1import importlib.util
2from pathlib import Path
3import socket
4import threading
5import time
6import unittest
7
8
9spec = importlib.util.spec_from_file_location(
10 "upnp_discover", Path(__file__).resolve().parents[1] / "nixos/upnp-discover.py"
11)
12upnp = importlib.util.module_from_spec(spec)
13spec.loader.exec_module(upnp)
14GATEWAY = "127.0.0.1"
15LOCATION = "http://127.0.0.1:1900/random-after-reboot/rootDesc.xml"
16RESPONSE = (
17 f"HTTP/1.1 200 OK\r\nST: upnp:rootdevice\r\nLOCATION: {LOCATION}\r\n\r\n"
18).encode("ascii")
19
20
21class DiscoveryTests(unittest.TestCase):
22 def test_gateway_extension_headers(self):
23 packet = (
24 "HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=120\r\nST: upnp:rootdevice\r\n"
25 "USN: uuid:00000000-0000-0000-0000-000000000001::upnp:rootdevice\r\nEXT:\r\n"
26 "SERVER: TP-LINK/TP-LINK UPnP/1.1 MiniUPnPd/1.8\r\n"
27 f"LOCATION: {LOCATION}\r\n"
28 'OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01\r\n01-NLS: 1\r\n'
29 "BOOTID.UPNP.ORG: 1\r\nCONFIGID.UPNP.ORG: 1337\r\n\r\n"
30 ).encode("ascii")
31 self.assertEqual(upnp.description_location(packet, (GATEWAY, 1900), GATEWAY), LOCATION)
32
33 def test_response_validation(self):
34 self.assertEqual(upnp.description_location(RESPONSE, (GATEWAY, 1900), GATEWAY), LOCATION)
35 alternate_port = RESPONSE.replace(b":1900/random", b":8080/random")
36 self.assertEqual(
37 upnp.description_location(alternate_port, (GATEWAY, 1900), GATEWAY),
38 LOCATION.replace(":1900/", ":8080/"),
39 )
40 invalid = [
41 RESPONSE.replace(b"127.0.0.1:1900", b"example.net:1900"),
42 RESPONSE.replace(b"127.0.0.1:1900", b"127.0.0.2:1900"),
43 RESPONSE.replace(b":1900/", b":0/"),
44 RESPONSE.replace(b":1900/", b":65536/"),
45 RESPONSE.replace(b":1900/", b":not-a-port/"),
46 RESPONSE.replace(b"http://", b"https://"),
47 RESPONSE.replace(b"http://", b"http://user@"),
48 RESPONSE.replace(b"rootDesc.xml", b"rootDesc.xml#fragment"),
49 RESPONSE.replace(b"rootDesc.xml", b"root\tDesc.xml"),
50 RESPONSE.replace(b"200 OK", b"404 Not Found"),
51 RESPONSE.replace(b"upnp:rootdevice", b"ssdp:all"),
52 RESPONSE.replace(b"LOCATION:", b"MISSING:"),
53 RESPONSE.replace(b"LOCATION:", b"BAD NAME:"),
54 RESPONSE.replace(b"LOCATION:", b"BAD/NAME:"),
55 RESPONSE.replace(b"\r\n\r\n", b"\r\nLOCATION: http://evil/\r\n\r\n"),
56 RESPONSE.replace(b"\r\n\r\n", b"\r\n folded\r\n\r\n"),
57 RESPONSE.replace(b"\r\n\r\n", b"\r\n"),
58 b"<html>404</html>",
59 b"\xff" + RESPONSE,
60 RESPONSE + b"x" * 8192,
61 ]
62 for packet in invalid:
63 with self.subTest(packet=packet[:100]):
64 self.assertIsNone(upnp.description_location(packet, (GATEWAY, 1900), GATEWAY))
65 self.assertIsNone(upnp.description_location(RESPONSE, ("127.0.0.2", 1900), GATEWAY))
66 self.assertIsNone(upnp.description_location(RESPONSE, (GATEWAY, 1901), GATEWAY))
67
68 def test_real_udp_ignores_invalid_then_accepts_gateway(self):
69 errors = []
70 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
71 gateway.bind((GATEWAY, 1900))
72 gateway.settimeout(1)
73
74 def respond():
75 try:
76 request, sender = gateway.recvfrom(8192)
77 self.assertIn(b"M-SEARCH * HTTP/1.1\r\n", request)
78 self.assertIn(b"ST: upnp:rootdevice\r\n\r\n", request)
79 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as rogue:
80 rogue.sendto(RESPONSE, sender)
81 gateway.sendto(RESPONSE.replace(b"127.0.0.1:1900", b"evil:1900"), sender)
82 gateway.sendto(b"HTTP/1.1 200 OK\r\n\r\n", sender)
83 gateway.sendto(RESPONSE, sender)
84 except BaseException as error:
85 errors.append(error)
86
87 thread = threading.Thread(target=respond)
88 thread.start()
89 try:
90 self.assertEqual(upnp.discover(GATEWAY, timeout=0.5), LOCATION)
91 finally:
92 thread.join(timeout=2)
93 self.assertFalse(thread.is_alive())
94 self.assertEqual(errors, [])
95
96 def test_timeout_is_bounded(self):
97 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
98 gateway.bind((GATEWAY, 1900))
99 started = time.monotonic()
100 with self.assertRaises(TimeoutError):
101 upnp.discover(GATEWAY, timeout=0.05)
102 self.assertLess(time.monotonic() - started, 0.5)
103
104 def test_invalid_response_flood_is_bounded(self):
105 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
106 gateway.bind((GATEWAY, 1900))
107 gateway.settimeout(1)
108
109 def flood():
110 _, sender = gateway.recvfrom(8192)
111 for _ in range(128):
112 gateway.sendto(b"invalid", sender)
113
114 thread = threading.Thread(target=flood)
115 thread.start()
116 started = time.monotonic()
117 try:
118 with self.assertRaises(TimeoutError):
119 upnp.discover(GATEWAY, timeout=0.5)
120 finally:
121 thread.join(timeout=2)
122 self.assertFalse(thread.is_alive())
123 self.assertLess(time.monotonic() - started, 1)
124
125
126if __name__ == "__main__":
127 unittest.main()
tools/xmpp.md+5-3
......@@ -25,9 +25,11 @@ provisions client, direct-TLS client, and federation SRV records pointing at
2525`xmpp.paperclover.net`; the website keeps its existing apex address. Router
2626forwarding must send the TCP listeners, UDP TURN listeners, and UDP relay range
2727to Snow Globe. `xmpp-nat.service` refreshes UPnP mappings on startup and every five
28minutes, using the Archer AX3000's description URL at the configured gateway.
29The description is fetched directly; multicast SSDP discovery failed on this
30host. Production activation uses the normal described-main deployment:
28minutes, discovering the current description URL with a bounded unicast SSDP
29request to the configured gateway on port 1900. Replies and HTTP description
30hosts must match that gateway; discovery failure fails the refresh. Multicast
31SSDP discovery does not work on this host. Production activation uses the normal
32described-main deployment:
3133
3234```sh
3335SNOWGLOBE_DEPLOY_HOST=root@10.0.0.1 SNOWGLOBE_DEPLOY_PORT=22 python3 tools/deploy.py prod