[PR #192] [MERGED] Play with friends (1/3): invite codes that need no server, port mapping, STUN, a relay anyone can run #2391

Closed
opened 2026-11-02 15:55:36 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/192
Author: @ludovic111
Created: 10/14/2026
Status: ✅ Merged
Merged: 10/15/2026
Merged by: @ludovic111

Base: main ← Head: feat/friends-relay


📝 Commits (6)

  • 60ddf3d Play with friends: invite codes, the relay and the encrypted tunnel
  • 74c1a90 Relay: hand out IPv4 clients' addresses as IPv4
  • de464a8 Play with friends without a server: endpoints and key in the code
  • 5189a73 Invite codes: one address per LAN, no unique-local IPv6, the macOS firewall prompt in the docs
  • 61b0aaf Relay: no hosting platform in its comments
  • fe7df46 Play with friends: bound handshakes and the relay's first line in time, cap relay connections

📊 Changes

24 files changed (+6284 additions, -1 deletions)

View changed files

➕ .dockerignore (+8 -0)
📝 Cargo.lock (+198 -0)
📝 Cargo.toml (+1 -1)
➕ crates/horizon-friends/Cargo.toml (+24 -0)
➕ crates/horizon-friends/src/code.rs (+159 -0)
➕ crates/horizon-friends/src/crypto.rs (+392 -0)
➕ crates/horizon-friends/src/direct.rs (+335 -0)
➕ crates/horizon-friends/src/guest.rs (+309 -0)
➕ crates/horizon-friends/src/host.rs (+571 -0)
➕ crates/horizon-friends/src/invite.rs (+403 -0)
➕ crates/horizon-friends/src/lib.rs (+185 -0)
➕ crates/horizon-friends/src/portmap.rs (+980 -0)
➕ crates/horizon-friends/src/reach.rs (+361 -0)
➕ crates/horizon-friends/src/session.rs (+195 -0)
➕ crates/horizon-friends/src/stun.rs (+207 -0)
➕ crates/horizon-friends/src/wire.rs (+206 -0)
➕ crates/horizon-relay/Cargo.toml (+19 -0)
➕ crates/horizon-relay/Dockerfile (+15 -0)
➕ crates/horizon-relay/horizon-relay.service (+24 -0)
➕ crates/horizon-relay/src/lib.rs (+698 -0)

...and 4 more files

📄 Description

What

Ludovic can't pay for a hosted relay, so nothing here depends on a Horizon server. Read docs/play-with-friends.md first (rewritten: how it works, privacy, running your own relay, troubleshooting; no Railway).

  • crates/horizon-friends (used by the game and the launcher):
    • invite: the full invite code carries the host's endpoints (router mapping, public address on the direct port, LAN / VPN / one global IPv6 address) and an 80-bit secret, in Crockford base32 groups of five with a 16-bit SHA-256 checksum (every single-symbol typo caught). Typical sizes: 52 symbols (mapping + LAN + tailnet), 79 with IPv6. When the host also uses a relay, the code names it and carries the short code, so guests fall back to it without any setting.
    • portmap: the host opens its direct port (TCP 25580 by default, stable so a manual forward keeps working) on the router for one hour, renewed at half-life, deleted on stop: PCP → NAT-PMP (gateway from the OS routing table, Linux/macOS/Windows) → UPnP IGD (SSDP, description, SOAP AddPortMapping; permanent-only routers (725) get a mapping deleted on stop; taken ports (718) try others). Small own implementation (no new heavy deps) so it is testable against mocks.
    • stun: the public IPv4 address from stun.cloudflare.com:3478 and stun.l.google.com:19302 (two independent operators of free global STUN, so one outage doesn't break discovery); HORIZON_STUN_SERVERS list or none.
    • reach: interface addresses (skips container/VM bridges, link-local, unique-local; one per LAN), the endpoints for the code, and the verdict with the host's advice: reachable via the router mapping / a public address; otherwise "turn on UPnP, or forward TCP port P to , or use a relay, or a VPN such as Tailscale/ZeroTier (the code already holds your VPN address)"; CGNAT / double NAT detected (router WAN side private or 100.64/10, or STUN disagrees) with its own advice.
    • host / guest: guests dial every endpoint at once; the first that answers as a Horizon host gets the SPAKE2 + ChaCha20-Poly1305 handshake (unchanged crypto), the rest are dropped before it. Relay sessions as before (short code, direct race, relay fallback). Concurrent code handshakes capped.
  • crates/horizon-relay: unchanged protocol, now framed as something anyone can run on a PC/VPS/Raspberry Pi/free tier: binary, Dockerfile, horizon-relay.service (systemd, sandboxed). railway.toml removed. No relay by default.

How tested

  • Unit tests (33): code format (round trips, typos, versions, relay ride-along), STUN against a mock server, PCP and NAT-PMP against a mock router (map/renew/delete, NAT-PMP when PCP is unsupported), UPnP against a mock IGD (chunked replies, 725 permanent-only, 718 conflict, delete), verdicts/CGNAT/double NAT/endpoint order, gateway parsing (Linux/macOS/Windows), crypto, the direct race.
  • crates/horizon-relay/tests/tunnel.rs (8): the code alone with no server (1 MiB, two guests at once), a self-hosted relay with the short code (relayed, then its own direct race), dead endpoints falling back to the relay named in the code, wrong secrets (full code; short code retired after five, full code follows with the same secret), unknown/ended codes, version mismatch, rate limits, bandwidth cap, /health.
  • macOS (Mac mini) and Linux (ThinkPad); clippy -D warnings and fmt clean.

Security review (2026-10-15, before merge)

Reviewed as a network feature: crypto, what a code exposes, listening sockets, router mappings, relay abuse.

  • Holds: SPAKE2 (symmetric, identity bound to nameplate + session) then per-direction ChaCha20-Poly1305 keys salted with both ends' fresh randoms; key confirmation before any game byte; counters as nonces (replay, reorder and tamper end the stream). The relay never sees the secret and can't test guesses offline; 20-bit short secrets get 5 online tries, then retire. The direct port only listens after Invite friends; Stop, closing the world and quitting (macOS Cmd+Q included) run Host::stop, which closes it and deletes the router mapping. The guest's direct-race listener lives 3 s; its loopback port takes loopback peers only. The code's secret stays out of crash reports and status screens.
  • Fixed here (last commit): a peer trickling one byte at a time restarted the per-read timeout on every byte, so it could hold the host's 8 handshake slots, or relay threads, for hours. Host openings and the relay's first line now end after 10 s in all (horizon_friends::deadline); the relay closes connections past RELAY_MAX_CONNECTIONS (4096). Tests prove the relay case and failed without the fix. The docs also say what a crash leaves on the router: a timed mapping expires within the hour; a permanent-only UPnP mapping stays until removed by hand.
  • Known, accepted: the code holds the host's LAN, VPN and public addresses (documented); the launcher passes the code on the game's command line, as vanilla's launcher does with its access token.

Needs Ludovic

Nothing. No cloud resource exists or is needed. skip-release (no player-facing change in this PR).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/192 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/14/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `feat/friends-relay` --- ### 📝 Commits (6) - [`60ddf3d`](https://github.com/horizon-rust/horizon/commit/60ddf3daf8906f0750c2d1a68248b40b9430e90d) Play with friends: invite codes, the relay and the encrypted tunnel - [`74c1a90`](https://github.com/horizon-rust/horizon/commit/74c1a9098025b0baac2e63225a9100de0de91771) Relay: hand out IPv4 clients' addresses as IPv4 - [`de464a8`](https://github.com/horizon-rust/horizon/commit/de464a8fc0b147a8dd281d19c6f18e5532a07af2) Play with friends without a server: endpoints and key in the code - [`5189a73`](https://github.com/horizon-rust/horizon/commit/5189a735c71c2c6e461172530cce8fac2a4e5e8e) Invite codes: one address per LAN, no unique-local IPv6, the macOS firewall prompt in the docs - [`61b0aaf`](https://github.com/horizon-rust/horizon/commit/61b0aafbf8c3ace061daad2901573301f29a9542) Relay: no hosting platform in its comments - [`fe7df46`](https://github.com/horizon-rust/horizon/commit/fe7df463fa1de5b7d352c7d5034faaa7918f5b86) Play with friends: bound handshakes and the relay's first line in time, cap relay connections ### 📊 Changes **24 files changed** (+6284 additions, -1 deletions) <details> <summary>View changed files</summary> ➕ `.dockerignore` (+8 -0) 📝 `Cargo.lock` (+198 -0) 📝 `Cargo.toml` (+1 -1) ➕ `crates/horizon-friends/Cargo.toml` (+24 -0) ➕ `crates/horizon-friends/src/code.rs` (+159 -0) ➕ `crates/horizon-friends/src/crypto.rs` (+392 -0) ➕ `crates/horizon-friends/src/direct.rs` (+335 -0) ➕ `crates/horizon-friends/src/guest.rs` (+309 -0) ➕ `crates/horizon-friends/src/host.rs` (+571 -0) ➕ `crates/horizon-friends/src/invite.rs` (+403 -0) ➕ `crates/horizon-friends/src/lib.rs` (+185 -0) ➕ `crates/horizon-friends/src/portmap.rs` (+980 -0) ➕ `crates/horizon-friends/src/reach.rs` (+361 -0) ➕ `crates/horizon-friends/src/session.rs` (+195 -0) ➕ `crates/horizon-friends/src/stun.rs` (+207 -0) ➕ `crates/horizon-friends/src/wire.rs` (+206 -0) ➕ `crates/horizon-relay/Cargo.toml` (+19 -0) ➕ `crates/horizon-relay/Dockerfile` (+15 -0) ➕ `crates/horizon-relay/horizon-relay.service` (+24 -0) ➕ `crates/horizon-relay/src/lib.rs` (+698 -0) _...and 4 more files_ </details> ### 📄 Description ## What Ludovic can't pay for a hosted relay, so **nothing here depends on a Horizon server**. Read `docs/play-with-friends.md` first (rewritten: how it works, privacy, running your own relay, troubleshooting; no Railway). - **`crates/horizon-friends`** (used by the game and the launcher): - `invite`: the **full invite code** carries the host's endpoints (router mapping, public address on the direct port, LAN / VPN / one global IPv6 address) and an **80-bit secret**, in Crockford base32 groups of five with a 16-bit SHA-256 checksum (every single-symbol typo caught). Typical sizes: 52 symbols (mapping + LAN + tailnet), 79 with IPv6. When the host also uses a relay, the code names it and carries the short code, so guests fall back to it without any setting. - `portmap`: the host opens its direct port (TCP 25580 by default, stable so a manual forward keeps working) on the router for **one hour, renewed at half-life, deleted on stop**: **PCP** → **NAT-PMP** (gateway from the OS routing table, Linux/macOS/Windows) → **UPnP IGD** (SSDP, description, SOAP `AddPortMapping`; permanent-only routers (725) get a mapping deleted on stop; taken ports (718) try others). Small own implementation (no new heavy deps) so it is testable against mocks. - `stun`: the public IPv4 address from **`stun.cloudflare.com:3478` and `stun.l.google.com:19302`** (two independent operators of free global STUN, so one outage doesn't break discovery); `HORIZON_STUN_SERVERS` list or `none`. - `reach`: interface addresses (skips container/VM bridges, link-local, unique-local; one per LAN), the endpoints for the code, and the **verdict** with the host's advice: reachable via the router mapping / a public address; otherwise "turn on UPnP, or forward TCP port P to <LAN IP>, or use a relay, or a VPN such as Tailscale/ZeroTier (the code already holds your VPN address)"; **CGNAT / double NAT** detected (router WAN side private or 100.64/10, or STUN disagrees) with its own advice. - `host` / `guest`: guests dial every endpoint at once; the first that answers as a Horizon host gets the SPAKE2 + ChaCha20-Poly1305 handshake (unchanged crypto), the rest are dropped before it. Relay sessions as before (short code, direct race, relay fallback). Concurrent code handshakes capped. - **`crates/horizon-relay`**: unchanged protocol, now framed as something **anyone can run** on a PC/VPS/Raspberry Pi/free tier: binary, `Dockerfile`, `horizon-relay.service` (systemd, sandboxed). `railway.toml` removed. **No relay by default.** ## How tested - Unit tests (33): code format (round trips, typos, versions, relay ride-along), STUN against a mock server, **PCP and NAT-PMP against a mock router** (map/renew/delete, NAT-PMP when PCP is unsupported), **UPnP against a mock IGD** (chunked replies, 725 permanent-only, 718 conflict, delete), verdicts/CGNAT/double NAT/endpoint order, gateway parsing (Linux/macOS/Windows), crypto, the direct race. - `crates/horizon-relay/tests/tunnel.rs` (8): **the code alone with no server** (1 MiB, two guests at once), **a self-hosted relay** with the short code (relayed, then its own direct race), **dead endpoints falling back to the relay named in the code**, wrong secrets (full code; short code retired after five, full code follows with the same secret), unknown/ended codes, version mismatch, rate limits, bandwidth cap, `/health`. - macOS (Mac mini) and Linux (ThinkPad); clippy `-D warnings` and fmt clean. ## Security review (2026-10-15, before merge) Reviewed as a network feature: crypto, what a code exposes, listening sockets, router mappings, relay abuse. - **Holds:** SPAKE2 (symmetric, identity bound to nameplate + session) then per-direction ChaCha20-Poly1305 keys salted with both ends' fresh randoms; key confirmation before any game byte; counters as nonces (replay, reorder and tamper end the stream). The relay never sees the secret and can't test guesses offline; 20-bit short secrets get 5 online tries, then retire. The direct port only listens after **Invite friends**; Stop, closing the world and quitting (macOS Cmd+Q included) run `Host::stop`, which closes it and deletes the router mapping. The guest's direct-race listener lives 3 s; its loopback port takes loopback peers only. The code's secret stays out of crash reports and status screens. - **Fixed here** (last commit): a peer trickling one byte at a time restarted the per-read timeout on every byte, so it could hold the host's 8 handshake slots, or relay threads, for hours. Host openings and the relay's first line now end after **10 s in all** (`horizon_friends::deadline`); the relay closes connections past `RELAY_MAX_CONNECTIONS` (4096). Tests prove the relay case and failed without the fix. The docs also say what a crash leaves on the router: a timed mapping expires within the hour; a permanent-only UPnP mapping stays until removed by hand. - **Known, accepted:** the code holds the host's LAN, VPN and public addresses (documented); the launcher passes the code on the game's command line, as vanilla's launcher does with its access token. ## Needs Ludovic Nothing. No cloud resource exists or is needed. `skip-release` (no player-facing change in this PR). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:55:36 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2391
No description provided.