From d559ec390582fe4bd7e029097434273bbe1ddd9b Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sun, 22 Nov 2026 11:31:43 +0000 Subject: [PATCH] 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 --- nixos/snowglobe.nix | 14 ++++++++- service/spacebar/build/supervisor.mjs | 7 ++++- service/spacebar/service.pkl | 2 ++ service/spacebar/voice-network.json | 4 ++- tools/phoenix-voice-udp-relay.mjs | 41 +++++++++++++++++++++++++++ tools/spacebar.md | 9 +++++- 6 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 tools/phoenix-voice-udp-relay.mjs diff --git a/nixos/snowglobe.nix b/nixos/snowglobe.nix index f15f37741df2c757707e3ef7f8f3258afeab1f2d..e66cf9d02e26aaff1465b512bb307c32c1565471 100644 --- a/nixos/snowglobe.nix +++ b/nixos/snowglobe.nix @@ -7,7 +7,7 @@ let xmppUDPPorts = [ xmppNetwork.turn xmppNetwork.turn_tls ]; irohNetwork = builtins.fromJSON (builtins.readFile ../service/iroh-relay/network.json); spacebarVoice = builtins.fromJSON (builtins.readFile ../service/spacebar/voice-network.json); - udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media ]; + udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media spacebarVoice.nativeMedia ]; encryptionRoots = { clover = "clover"; media = "clover/Media"; @@ -40,6 +40,18 @@ in reverse_proxy 127.0.0.1:${toString spacebarVoice.signaling} ''; + systemd.services.phoenix-voice-udp-relay = { + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ]; + after = [ "network-online.target" "nomad.service" ]; + serviceConfig = { + DynamicUser = true; + 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}"; + Restart = "always"; + RestartSec = 5; + }; + }; + systemd.services.public-ports = { wantedBy = [ "multi-user.target" ]; wants = [ "network-online.target" ]; diff --git a/service/spacebar/build/supervisor.mjs b/service/spacebar/build/supervisor.mjs index e013f7294188fd8c146494bb7e72ca02e8acf709..af55b6cb8f8a9c9442df50c5bdb13c3e144c6ecd 100644 --- a/service/spacebar/build/supervisor.mjs +++ b/service/spacebar/build/supervisor.mjs @@ -9,7 +9,7 @@ import { createInterface } from "node:readline"; import { setTimeout as sleep } from "node:timers/promises"; import { promisify } from "node:util"; -const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT } = process.env; +const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT, VOICE_NATIVE_MEDIA_PORT } = process.env; const WORK = "/work"; const REPO = "/source.git"; const POLL_MS = 250; @@ -231,6 +231,11 @@ async function startVoice(release) { WRTC_PUBLIC_IP: ip, WRTC_PORT_MIN: VOICE_MEDIA_PORT, WRTC_PORT_MAX: VOICE_MEDIA_PORT, + ...(VOICE_NATIVE_MEDIA_PORT ? { + VOICE_UDP_PORT: VOICE_NATIVE_MEDIA_PORT, + VOICE_UDP_PUBLIC_IP: ip, + VOICE_UDP_LOOPBACK_RELAY: "true", + } : {}), }); await waitForVoice(voice.signaling, () => fetch(`http://127.0.0.1:${VOICE_SIGNALING_PORT}/`, { signal: AbortSignal.timeout(1000), diff --git a/service/spacebar/service.pkl b/service/spacebar/service.pkl index 89f5d1fe05e302b8b6f2d0496aba40365a6de324..77c2c23b0045881a9445e659330c9188f987756d 100644 --- a/service/spacebar/service.pkl +++ b/service/spacebar/service.pkl @@ -41,6 +41,7 @@ container { when (!site.preview) { new { name = "voice"; containerPort = voice.signaling; hostPort = containerPort } new { name = "voice-media"; containerPort = voice.media; hostPort = containerPort; loopback = false } + new { name = "voice-native-media"; containerPort = voice.nativeMedia; hostPort = containerPort; loopback = false } } } @@ -64,6 +65,7 @@ container { ["VOICE_PUBLIC_HOST"] = voiceHost ["VOICE_SIGNALING_PORT"] = voice.signaling.toString() ["VOICE_MEDIA_PORT"] = voice.media.toString() + ["VOICE_NATIVE_MEDIA_PORT"] = voice.nativeMedia.toString() } } diff --git a/service/spacebar/voice-network.json b/service/spacebar/voice-network.json index 95ed0ae18e1af6e48f759a83bb371798719c7a46..948468a84237b1c2edef9f1c2bc37cc0ef6601a4 100644 --- a/service/spacebar/voice-network.json +++ b/service/spacebar/voice-network.json @@ -1,5 +1,7 @@ { "subdomain": "voice.phoenix-selfhosted", "signaling": 17404, - "media": 6000 + "media": 6000, + "nativeMedia": 6001, + "phoenixRelayAddress": "100.64.0.1" } diff --git a/tools/phoenix-voice-udp-relay.mjs b/tools/phoenix-voice-udp-relay.mjs new file mode 100644 index 0000000000000000000000000000000000000000..5fc8f0cc0964ad1beb9bc330abae9780de4b1deb --- /dev/null +++ b/tools/phoenix-voice-udp-relay.mjs @@ -0,0 +1,41 @@ +import { createSocket } from "node:dgram"; +import { isIPv4 } from "node:net"; + +const [listenAddress, upstreamAddress, configuredPort] = process.argv.slice(2); +const port = Number(configuredPort); +if (!isIPv4(listenAddress ?? "") || !isIPv4(upstreamAddress ?? "") || listenAddress === upstreamAddress || + listenAddress === "0.0.0.0" || upstreamAddress === "0.0.0.0" || !Number.isInteger(port) || port < 1 || port > 65535) { + throw new Error("Expected distinct local and upstream IPv4 addresses and a fixed shared UDP port"); +} + +const listener = createSocket("udp4"); +const clients = new Map(); + +listener.on("message", (packet, remote) => { + if (!packet.length || packet.length > 4096) return; + const key = `${remote.address}:${remote.port}`; + let client = clients.get(key); + if (!client) { + if (clients.size >= 64) return; + const socket = createSocket("udp4"); + const close = () => { + clients.delete(key); + clearTimeout(idle); + socket.close(); + }; + const idle = setTimeout(close, 300_000).unref(); + client = { socket, idle }; + clients.set(key, client); + // Keep the upstream tuple stable for this caller's discovery and media. + socket.on("message", (reply, source) => { + if (source.address !== upstreamAddress || source.port !== port || reply.length > 4096) return; + idle.refresh(); + if (listener.getSendQueueCount() < 64) listener.send(reply, remote.port, remote.address, () => {}); + }); + socket.once("error", close); + } + client.idle.refresh(); + if (client.socket.getSendQueueCount() < 64) client.socket.send(packet, port, upstreamAddress, () => {}); +}); + +listener.bind(port, listenAddress); diff --git a/tools/spacebar.md b/tools/spacebar.md index 4b77e89570d7af4d17cca466e2dc32a418a42966..905ab21052eeef021166bfadd3d964a275e6e305 100644 --- a/tools/spacebar.md +++ b/tools/spacebar.md @@ -54,10 +54,17 @@ start that is still waiting. Rolling back is pushing the old commit again. The production service publishes the voice WebSocket at `voice.phoenix-selfhosted.paperclover.net:443`. [`voice-network.json`](../service/spacebar/voice-network.json) configures the -loopback WebSocket mapping and the UDP media port. The supervisor builds Pion +WebSocket port and separate UDP ports for Pion and native Discord voice. The supervisor builds Pion from the Snowcord revision's `flake.lock`: git fetches the pinned commit, `go.sum` pins the modules, and the build and SFU run with a minimal environment. The binary is kept in `/work` for that pin, so a restart needs no network. It starts one SFU and one standalone voice process after the API is healthy. Each app release restarts voice, and an unavailable voice process retries while the API continues serving. Previews leave voice disabled. + +Snow Globe's tailnet UDP relay forwards the native media port to the published +container port. The Phoenix VM uses the same relay tool bound to loopback with +Snow Globe's tailnet address as upstream. Its gateway relay sets +`X-Snowcord-Voice-Relay: phoenix-loopback` so voice advertises the VM's loopback +address. The guest profile lets Phoenix bind and receive UDP on its temporary +ports and send UDP only to the local relay port. -- 2.54.1