Skip to content

Move a relayed computer to its direct path on the phone, and back - #48

Merged
TheAnimatrix merged 3 commits into
mainfrom
direct-path-upgrade
Dec 1, 2026
Merged

TheAnimatrix merged 3 commits into
mainfrom
direct-path-upgrade

Conversation

@TheAnimatrix

@TheAnimatrix TheAnimatrix commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

A phone paired with "Your own relay" on the same Wi-Fi as its computer stayed on the relay for good. It never learned the computer's LAN address, and once on the relay it never tried direct again until the network changed. Now the phone does what the desktop helper already does for a relayed host (mousse-helper app/paths.rs upgrade, app/lan.rs), in the shared Rust core:

  • Finding the address. The phone browses the Wi-Fi for _mousse._tcp every time it connects, after a network change, and on its direct probes. It uses NSD on Android (multicast lock held only during the 3 s browse) and Bonjour on iOS (NWBrowser). The core drives it through a new LocalNetworkBrowser callback. Only LAN IP literals count. A browse is reused for 60 s, forgotten on a network change, and skipped off Wi-Fi.
  • Moving to direct. While a computer is reached through the relay, the phone probes its direct routes 1 s after connecting. The waits then double up to 5 min (±20%), and start over on return to the foreground or when the person taps "Look for it on this network". A path must pass the same checks as the relay path: TLS pinned to the paired identity (an address that answers as another computer is skipped), the computer's authorization, and the app hello.
    • A same-network path replaces the relay at once.
    • Any other direct path must not be slower than the relay by more than 20 ms (best of 3 pings).
    • The move uses the engine's path switch (try_path_turn, candidate, then reopened). New calls go direct at once. Calls already on the relay finish there (up to 20 s) before it closes with PATH_REPLACED. Readers reopen their streams on the new path from their cursors, woken by a status nudge.
    • No write is cut or replayed. A write that is cut anyway reports MayHaveHappened (now also for Connecting).
  • Falling back. When a direct path ends, the relay is dialled at once, with no 300 ms head start and no 500 ms quiet pause.
  • No flapping. A direct path that ends by itself within 60 s holds probes off for 1 min, doubling up to 30 min. A network change doesn't count as a flap.
  • Remembering. The route that worked is saved with the engine's note_route (last_route), so the next cold start at home tries it first. same_record no longer compares last_route, so saving it doesn't restart the worker.
  • Battery and data. Nothing new runs in the background: probes pause until the app returns. Off Wi-Fi, saved LAN addresses aren't dialled.
  • Connection and speed now shows four things:
    • Which path is in use: Direct (same Wi-Fi / Tailscale / internet / tunnel) or Relay.
    • When and why it last changed.
    • On the relay, why direct isn't used: not found (turn on Same Wi-Fi or network at the computer), not on Wi-Fi, Local Network access needed or off (iOS), no answer (firewall or Wi-Fi isolation), relay faster, or kept dropping.
    • A button to look again. On iOS the first browse waits until the person taps "Allow Local Network access" on this screen, or has paired over Same Wi-Fi, so the system prompt never appears unexplained at launch.
  • The path line now sits in the sheet's content. The sheet's subtitle kept its first, empty value, so the old "Direct / Relay" line never showed.

Checked

Rust.

  • direct::tests cover the probe backoff, the flap hold-off, the RTT margin, the problem mapping, LAN-only browse results, and no LAN dials off Wi-Fi.
  • tests/direct_path.rs uses a real engine host, a real mousse-relay and a phone paired relay-only, and covers:
    • relay → direct in about 80 ms once the address answers, while a 1.5 s write runs on the relay. That write lands once, through the relay, and the next write goes direct. The relayed path then closes.
    • direct → relay in about 110 ms after the direct path ends.
    • after that, the direct path offered again is not taken for 3 s (held).
    • a browse result that answers as another computer is never used. Control: adding the right address in the same test moves.
  • Must-fail controls, run locally:
    • with the in-flight wait removed, the write fails with MayHaveHappened.
    • with the flap hold-off disabled, the problem shows Looking instead of Unstable.
    • with the engine built with --cfg mousse_control="skip-pin-check", a session forms with the stranger.

Android emulator (API 36, perf-link serve-upgrade: real host and real relay). The emulator's NAT hides multicast, so debug builds take --es test-lan as the browse result. The NSD browse itself only runs on a real phone.

  • Cold start on the relay: 951 ms.
  • A direct listener on a port the phone didn't know is offered → Direct (same Wi-Fi) about 65 ms later.
  • Listener stopped → Relay again 387 ms later, with the reason shown.
  • Dropped within a minute and offered again → held for exactly 60 s, then direct.

Direct (same Wi-Fi) after moving off the relay
Back on the relay, holding off a direct path that kept dropping

iOS. scripts/build-ios.sh builds the simulator app on the Mac.

Not changed, and gaps

  • Desktop: with only "Your own relay" on, the desktop doesn't listen on the LAN or advertise mDNS. Settings › Reach › Same Wi-Fi or network is off by default (routes.json lan). Mousse adds no firewall rule. It relies on Windows' first-run prompt for Mousse.exe, so the network should be Private and Mousse.exe allowed.
  • Protocol: no authenticated message carries a host's current direct addresses to a caller. The signed manifest has relay routes only, and net.direct.endpoints answers engine callers with nothing. So the phone learns addresses only by browsing (and from saved routes). Off the LAN (a tailnet or port forward the phone was never given), it can't learn them.

Not tested

The owner's phone and home network, a real NSD/Bonjour browse on a phone, the iOS Local Network prompt, and leaving Wi-Fi on a real radio.

The phone now finds its computer on the local network (NSD on Android,
Bonjour on iOS) every time it connects, after a network change and on
background probes while it is reached through the relay. A path that
passes the pinned TLS handshake, the computer's authorization and the app
hello replaces the relay: same network at once, any other direct path
unless the relay is faster by more than 20 ms. Calls on the relay finish
there before it closes, so no write is cut or sent twice. A direct path
that ends falls back to the relay at once; one that keeps dropping holds
probes off for 1 minute, doubling to 30. The Connection and speed screen
says which path is in use, when and why it last changed, and why direct
is not available.
…tor harness

The Connection and speed sheet shows the path in its content: the sheet's
subtitle kept its first, empty value. perf-link serve-upgrade runs a desktop
on a real relay with a direct listener that can be shut and opened on a new
port, for the relay-to-direct check on an Android emulator.
…found address, cancel the hand-over with the direct path
@TheAnimatrix
TheAnimatrix merged commit 9573513 into main Dec 1, 2026
3 checks passed
@TheAnimatrix
TheAnimatrix deleted the direct-path-upgrade branch December 1, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant