Play over the internet #
Rubylight docs › Play anywhere · Configuration · Privacy
Devices reach your PC from outside your network without forwarding ports by hand, and without any server run by the Rubylight project: no relay carries your stream and no rendezvous server knows your devices. Two settings are involved:
- Automatic port forwarding (
upnp, off by default) asks your router to forward the streaming ports: through UPnP IGD, else PCP, else NAT-PMP, for IPv4, and opens IPv6 firewall pinholes through UPnP IGDv2 or PCP. Stock Moonlight benefits as well: it reaches a forwarded port at the PC'sExternalIP. - Play over the internet (
internet_play, off by default) adds a direct path for when no port can be forwarded, such as behind carrier-grade NAT: Nightjar and the host find each other through a public peer-to-peer directory, the Mainline DHT, where each leaves an encrypted note only the other can read, and punch a UDP path through both routers. Nothing is downloaded or shared through it.
The order a device tries #
- The local network. At home nothing else is tried and nothing is added to the stream.
- The PC's IPv6 address. No router mapping is needed where the router lets it in (or the host opened a pinhole).
- A forwarded IPv4 port, at the PC's public address, when every streaming port is forwarded under its own number.
- A punched path. Only Nightjar, only with a host that offers it (
RustHostInternetPlayVersion1 in a paired serverinfo), and only after the device was on the PC's network once with the option on, or opened an invite.
The PC's line in Nightjar names the path in use (Internet (IPv6), Internet (forwarded port) or Internet (direct path)). When none works, it lists every path and why it failed, such as "IPv6: no answer; a direct internet path: both routers change ports per destination".
What the host does #
- Port mapping. With
upnpon, every streaming port (HTTP, HTTPS, RTSP, video, audio, control, microphone) is asked for. Leases are renewed every minute and removed when the host stops. A UPnP router that misses one renewal keeps its mappings; after two missed in a row (the lease's two minutes), or when this PC no longer has the address they were made for, the host looks for the router again and maps the ports for the address it has now. A host listening on every address looks from its Ethernet and Wi-Fi connections in the order Windows prefers them (the lowest interface metric first; of two on the same router with equal metrics, the one Windows routes to that router through, and otherwise the lower address; Ethernet on a Hyper-V external switch counts as Ethernet), leaving out Hyper-V's Default Switch, WSL's adapter and VPN adapters that call themselves Ethernet while such a connection has an address; without one it looks from every Ethernet and Wi-Fi address, those included. These searches run at once, and when none of them finds a router, the search earlier versions made counts too, unless it would leave from an address already searched from: it goes wherever Windows sends it and with a VPN up may leave through the VPN. A router found that way, as in earlier versions, gets the ports mapped, the console shows its address as the public IPv4 address, and PCP and NAT-PMP are not asked on the home router while it answers. A host bound to one address looks from that address. PCP and NAT-PMP ask the default gateway of the first connection in that order, or of the address the host is bound to. The console's Settings → Network → Internet status shows the method that worked, each port mapped or refused and why, the public address, the IPv6 pinholes, and notes for a router whose own address is not public: carrier-grade NAT, or another router or modem in front of it. - Public address. With
internet_playon, the host asks public STUN servers for its public address every five minutes (internet_play_stun_servers, defaultstun.moonlight-stream.org:3478andstun.cloudflare.com:3478) and classifies the router: cone (one mapping for every destination) or symmetric. Paired serverinfo reportsExternalIPwhen either setting is on and the address is known. - Keys for each device. Each paired device gets a random 32-byte secret, stored with the device on the host and fetched by that device over the paired HTTPS connection (
GET /internet-play/v1). Unpairing removes it. - Invites. An invite to a friend carries a secret of its own, from which both sides derive the rendezvous keys, so a device that never was on the PC's network finds it as above. Once paired, the device fetches its own secret and changes to it; the invite's record stays published for ten minutes after it is used. With
internet_playoff, the host starts the rendezvous (STUN and the DHT) only while an unused invite or a device paired from one needs it, publishes only those, and stops it after. - Finding each other. For each device, the host publishes an encrypted record in the directory every 30 minutes and on change, and looks for the device's record every ten seconds while the device is punching or was heard from in the last ten minutes, else every 15 seconds, and not while its path is connected and heard from (a device that starts again punches through that path itself) (the previous hour's record only in the first two minutes of an hour, when it can still be fresh).
- Tunnels. A punch authenticated with a device's keys opens a path. The host connects tunnelled HTTP, HTTPS and RTSP to its own ports on loopback from a source address of the device's own (127.77.x.y), so sessions of two devices stay apart, and forwards media datagrams to its media ports.
Finding each other on the Mainline DHT #
The Mainline DHT is a public peer-to-peer directory, the same one BitTorrent apps use to find each other. Rubylight and Nightjar use it only as a directory for their encrypted records: nothing is downloaded, shared or seeded through it. The records are mutable items (BEP 44), stored by public DHT nodes for about two hours. The host and the device each publish one and read the other's:
- Keys. HKDF-SHA256 over the device secret (salt
rubylight internet play v1) derives an Ed25519 signing key per side, a record key, and a salt per side and hour. The item's target is SHA-1 of the public key and the salt, so the target changes every hour and nobody without the secret can link two hours, a device and its PC, or find either. - Contents. Sealed with ChaCha20-Poly1305 under the record key: the side's role, time, session ID (random per run), the peer session it answers, its NAT type, up to eight public endpoints and, from the host, its port numbers. A node storing it sees only its size, the signature and the sequence number (the publication time).
- Lookups. Iterative
get_peers-style lookups (find_node,get,putwith a write token) withro=1(BEP 43): neither side answers DHT queries or stores anyone else's items. The bootstrap nodes arerouter.bittorrent.com:6881,dht.transmissionbt.com:6881,router.utorrent.com:6881anddht.libtorrent.org:25401.
Punching and the tunnel #
Both sides send authenticated punches to every endpoint the other published, every 100 ms for 3 s and then every 500 ms, for up to 30 s. The device starts with the endpoints the PC gave it on its network while it looks up the PC's record, and punches on until 51 s after its own record is stored, the time the host needs to find it. A side behind a symmetric NAT is also tried at the next eight ports. The first authenticated answer fixes the path, including a peer address neither side published (peer-reflexive).
- Wire format. Each datagram: magic
A7 52 4C, version 1, kind (punch, punch ack, stream, media, close, keepalive), channel, an 8-byte path tag, an 8-byte sequence number, and a ChaCha20-Poly1305 payload whose nonce is the sequence number. Keys are per direction and per pair of sessions. A 512-packet window refuses replays and still takes a packet up to 512 behind the newest one seen, as the packets of a large frame can be when audio or control packets overtake them. Keepalives every 10 s; a path silent for 45 s is closed. A device's punch carries its clock (unix seconds) as its payload: a device that started again punches through a path of the PC that still looks connected, at once, when its stamp is within two minutes and later than the old session's; an unstamped, stale or older punch (a replay on the way, or an older device) replaces nothing, and the PC then finds the new session through the device's record, once the old path has been silent for 15 s. - TCP channels. HTTP, HTTPS and RTSP run over a small reliable stream layer (selective acknowledgements, RFC 6298 retransmission timer, fast retransmit, up to 256 segments of 1100 bytes in flight, 512 KiB send buffer per stream). HTTPS stays end-to-end TLS with the certificate pinned at pairing.
- Media. Video, audio, control and microphone datagrams go straight over the punched path, one sealed datagram each, without the stream layer. Streams over it use Moonlight's remote packet size (1024 bytes) so the sealed datagrams fit.
- The device's side. The tunnel binds the PC's port numbers on a loopback address of the device (127.78.x.y from the PC's ID; 127.0.0.1 on macOS, which binds only that). Nightjar then talks to that address as to the PC, so pairing, the library and both streaming engines are unchanged. The device closes the path once it has carried no connection and no media for two minutes (the host list's polls and streams keep it up); the next connection punches again.
Punching works for IPv4 when at least one side has a cone NAT, or both are port-restricted cones. Two symmetric NATs, or a symmetric NAT opposite a port-restricted one, cannot be punched; then only a forwarded port or IPv6 reaches the PC.
Away wake relay #
A sleeping PC publishes nothing and answers no punch. With Away wake relay on (away_wake_relay, besides internet_play), an always-on paired device on the PC's network, a relay, wakes it for a paired device that is away. The same records, punches and sealed datagrams carry it, and the relay only ever sends Wake-on-LAN on its own network.
- Enrolling. A device with Host control on one of the PC's own private subnets, never through a tunnel, enrolls over its paired HTTPS connection (
POST /api/native/v1/wake/relay/enroll, Native API) and gets a 32-byte secret, the PC's physical MACs and its subnets' broadcast addresses, and the devices that may wake it: those paired, turned on and allowed to launch apps. It fetches them again every five minutes while the PC is awake, so an unpaired device, a revoked relay or new keys reach it then. A revoked relay is not enrolled again by its refresh. - Keys. A device's key for a relay is HKDF-SHA256 over the relay's secret, the PC's ID, the device's ID (the SHA-256 of its certificate) and the key epoch. The PC hands each device that may launch apps its keys (
GET /wake/relay); the relay derives the same ones and never learns more. Rotate keys in the console raises the epoch: each relay drops the old keys at its next refresh (within five minutes while the PC is awake), and each device gets new ones the next time it reads them. - Meeting. For every device it may wake for, the relay publishes a record under that device's key (role wake relay) every 30 minutes and polls the device's record (role wake requester) every 15 seconds, as the PC does for internet play. The device looks the relay's record up, publishes its own and punches; only a punch stamped within two minutes opens a path.
- The request. One sealed datagram of 148 bytes: version, epoch, the device's clock, a random 16-byte nonce, the device's ID and a hash of the PC's ID, signed with an Ed25519 key derived from the device's key. The relay checks, in this order: a device it may wake for, the signature, the PC, the epoch, a clock within two minutes, a nonce it has not seen (512 kept; when full it refuses rather than forgets), and its limits: one burst every ten seconds per PC and per device, at most ten per device and twenty per PC in ten minutes. Anything that fails before the signature gets no answer at all.
- The burst. At most three magic packets (102 bytes, UDP port 9) per MAC and broadcast, only to the PC's registered MACs and to the broadcast addresses of the relay's own private subnets that the PC registered: at most 60 small packets on the local network for one request, none anywhere else. A repeated request gets its answer again, never a second burst.
- The answer. A sealed status: sent, rate limited (with seconds to wait), refused, then awake when the PC answers on its HTTP port within 45 seconds, or timed out. The device then connects as usual.
The relay forwards no port: like the PC for internet play it uses one UDP socket for the DHT, STUN and punches, and sends the magic packets from its own interface addresses.
Running a relay. Any always-on box on the PC's network with Nightjar's desktop client can be the relay, such as a Linux mini PC or server. Pair it with the PC once in Nightjar's window (add the PC and type the PIN, or open the console's pairing link with nightjar 'rubylight://pair?…', which asks in the window before it pairs; a box without a screen needs a desktop session for that once). In the Rubylight console turn on Host control for it, and Away wake relay with Play over the internet. Then keep nightjar wake-relay running (--name "Living room NUC" is how the PC lists it when it enrolls). It enrolls with every paired PC that offers a relay, prints a line for each change (the PCs it serves and their devices, its public address and NAT, STUN or DHT trouble, each wake), and stops on Ctrl-C or SIGTERM with its enrollments kept. It saves the assignments, which hold its secrets, in wake-relay.json in Nightjar's config directory (~/.config/nightjar on Linux, readable by its owner only), so a relay started again while the PC sleeps still wakes it. A PC that cannot be reached keeps its enrollment. A PC with the setting off, or that sees the relay off its network, is not served until a refresh succeeds again; a PC that removed the relay, took Host control away or unpaired it is dropped at once and not enrolled again until nightjar wake-relay --enroll. nightjar wake-relay --leave leaves every PC and deletes the file. As a systemd user service, with loginctl enable-linger so it runs without a login:
# ~/.config/systemd/user/nightjar-wake-relay.service, then: systemctl --user enable --now nightjar-wake-relay
[Unit]
Description=Nightjar wake relay
[Service]
ExecStart=/usr/bin/nightjar wake-relay
Restart=on-failure
RestartSec=30
[Install]
WantedBy=default.target
Limits #
- A tunnelled device reaches the host's ports from this PC, yet the host treats it as a device on the internet: every connection and media socket the tunnel opens is marked as tunnelled before it sends, and 127.77.0.0/16 and 127.78.0.0/16 always count as the internet. It gets
wan_encryption_mode, neverlan_encryption_mode, none of what serverinfo shows only programs on this PC (session counts, the host's profile), no browser-on-this-PC trust and no LAN-only guest play. The path is sealed with the device's keys. - The host keys sessions by the device's source address (127.77.x.y). A host that listens on one address (
bind_address), not every address, connects every tunnel from that address, so it serves one device over the internet at a time: an invited friend and you both away from home need the default, every address. - The host polls each device's record every 15 seconds while the option is on (every ten seconds for a device it heard from lately), and not at all for a device whose path is connected and heard from within 15 s: a few small DHT packets per device. The DHT routers' names are resolved once and kept for 30 minutes (again after a lookup that reached no node).
- Away from home, a PC that is off is tried over the DHT at most once a minute.