authoravatarclover caruso<git@paperclover.net>2026-11-22 11:31:43+00:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 11:39:56+00:00
logd559ec390582fe4bd7e029097434273bbe1ddd9b
tree897212ae55cce903824bc68a6d7ac3c30f1b48ef
parent1e32e2ab9fc916bbdefd5efe7a151e083345c19f

Expose native Snowcord voice media alongside Pion

Publish a separate native UDP port and route the Phoenix VM tailnet address to it. Share the fixed endpoint relay with the guest and preserve the existing voice hostname and Pion listener. Assisted-by: gpt-6.2-sol

6 files changed, 73 insertions(+), 4 deletions(-)

nixos/snowglobe.nix+13-1
......@@ -7,7 +7,7 @@ let
77 xmppUDPPorts = [ xmppNetwork.turn xmppNetwork.turn_tls ];
88 irohNetwork = builtins.fromJSON (builtins.readFile ../service/iroh-relay/network.json);
99 spacebarVoice = builtins.fromJSON (builtins.readFile ../service/spacebar/voice-network.json);
10 udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media ];
10 udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media spacebarVoice.nativeMedia ];
1111 encryptionRoots = {
1212 clover = "clover";
1313 media = "clover/Media";
......@@ -40,6 +40,18 @@ in
4040 reverse_proxy 127.0.0.1:${toString spacebarVoice.signaling}
4141 '';
4242
43 systemd.services.phoenix-voice-udp-relay = {
44 wantedBy = [ "multi-user.target" ];
45 wants = [ "network-online.target" ];
46 after = [ "network-online.target" "nomad.service" ];
47 serviceConfig = {
48 DynamicUser = true;
49 ExecStart = "${pkgs.nodejs_24}/bin/node ${../tools/phoenix-voice-udp-relay.mjs} ${spacebarVoice.phoenixRelayAddress} ${(builtins.head config.networking.interfaces.enp4s0.ipv4.addresses).address} ${toString spacebarVoice.nativeMedia}";
50 Restart = "always";
51 RestartSec = 5;
52 };
53 };
54
4355 systemd.services.public-ports = {
4456 wantedBy = [ "multi-user.target" ];
4557 wants = [ "network-online.target" ];
service/spacebar/build/supervisor.mjs+6-1
......@@ -9,7 +9,7 @@ import { createInterface } from "node:readline";
99import { setTimeout as sleep } from "node:timers/promises";
1010import { promisify } from "node:util";
1111
12const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT } = process.env;
12const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env;
1313const WORK = "/work";
1414const REPO = "/source.git";
1515const POLL_MS = 250;
......@@ -231,6 +231,11 @@ async function startVoice(release) {
231231 WRTC_PUBLIC_IP: ip,
232232 WRTC_PORT_MIN: VOICE_MEDIA_PORT,
233233 WRTC_PORT_MAX: VOICE_MEDIA_PORT,
234 ...(VOICE_NATIVE_MEDIA_PORT ? {
235 VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT,
236 VOICE_UDP_PUBLIC_IP: ip,
237 VOICE_UDP_LOOPBACK_RELAY: "true",
238 } : {}),
234239 });
235240 await waitForVoice(voice.signaling, () => fetch(`http://127.0.0.1:${VOICE_SIGNALING_PORT}/`, {
236241 signal: AbortSignal.timeout(1000),
service/spacebar/service.pkl+2
......@@ -41,6 +41,7 @@ container {
4141 when (!site.preview) {
4242 new { name = "voice"; containerPort = voice.signaling; hostPort = containerPort }
4343 new { name = "voice-media"; containerPort = voice.media; hostPort = containerPort; loopback = false }
44 new { name = "voice-native-media"; containerPort = voice.nativeMedia; hostPort = containerPort; loopback = false }
4445 }
4546 }
4647
......@@ -64,6 +65,7 @@ container {
6465 ["VOICE_PUBLIC_HOST"] = voiceHost
6566 ["VOICE_SIGNALING_PORT"] = voice.signaling.toString()
6667 ["VOICE_MEDIA_PORT"] = voice.media.toString()
68 ["VOICE_NATIVE_MEDIA_PORT"] = voice.nativeMedia.toString()
6769 }
6870 }
6971
service/spacebar/voice-network.json+3-1
......@@ -1,5 +1,7 @@
11{
22 "subdomain": "voice.phoenix-selfhosted",
33 "signaling": 17404,
4 "media": 6000
4 "media": 6000,
5 "nativeMedia": 6001,
6 "phoenixRelayAddress": "100.64.0.1"
57}
tools/phoenix-voice-udp-relay.mjs created+41
......@@ -0,0 +1,41 @@
1import { createSocket } from "node:dgram";
2import { isIPv4 } from "node:net";
3
4const [listenAddress, upstreamAddress, configuredPort] = process.argv.slice(2);
5const port = Number(configuredPort);
6if (!isIPv4(listenAddress ?? "") || !isIPv4(upstreamAddress ?? "") || listenAddress === upstreamAddress ||
7 listenAddress === "0.0.0.0" || upstreamAddress === "0.0.0.0" || !Number.isInteger(port) || port < 1 || port > 65535) {
8 throw new Error("Expected distinct local and upstream IPv4 addresses and a fixed shared UDP port");
9}
10
11const listener = createSocket("udp4");
12const clients = new Map();
13
14listener.on("message", (packet, remote) => {
15 if (!packet.length || packet.length > 4096) return;
16 const key = `${remote.address}:${remote.port}`;
17 let client = clients.get(key);
18 if (!client) {
19 if (clients.size >= 64) return;
20 const socket = createSocket("udp4");
21 const close = () => {
22 clients.delete(key);
23 clearTimeout(idle);
24 socket.close();
25 };
26 const idle = setTimeout(close, 300_000).unref();
27 client = { socket, idle };
28 clients.set(key, client);
29 // Keep the upstream tuple stable for this caller's discovery and media.
30 socket.on("message", (reply, source) => {
31 if (source.address !== upstreamAddress || source.port !== port || reply.length > 4096) return;
32 idle.refresh();
33 if (listener.getSendQueueCount() < 64) listener.send(reply, remote.port, remote.address, () => {});
34 });
35 socket.once("error", close);
36 }
37 client.idle.refresh();
38 if (client.socket.getSendQueueCount() < 64) client.socket.send(packet, port, upstreamAddress, () => {});
39});
40
41listener.bind(port, listenAddress);
tools/spacebar.md+8-1
......@@ -54,10 +54,17 @@ start that is still waiting. Rolling back is pushing the old commit again.
5454
5555The production service publishes the voice WebSocket at `voice.phoenix-selfhosted.paperclover.net:443`.
5656[`voice-network.json`](../service/spacebar/voice-network.json) configures the
57loopback WebSocket mapping and the UDP media port. The supervisor builds Pion
57WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion
5858from the Snowcord revision's `flake.lock`: git fetches the pinned commit, `go.sum`
5959pins the modules, and the build and SFU run with a minimal environment. The binary
6060is kept in `/work` for that pin, so a restart needs no network.
6161It starts one SFU and one standalone voice process after the API is healthy.
6262Each app release restarts voice, and an unavailable voice process retries
6363while the API continues serving. Previews leave voice disabled.
64
65Snow Globe's tailnet UDP relay forwards the native media port to the published
66container port. The Phoenix VM uses the same relay tool bound to loopback with
67Snow Globe's tailnet address as upstream. Its gateway relay sets
68`X-Snowcord-Voice-Relay: phoenix-loopback` so voice advertises the VM's loopback
69address. The guest profile lets Phoenix bind and receive UDP on its temporary
70ports and send UDP only to the local relay port.