Skip to content

feat(torrent): forward the port through every NAT layer and show whether peers can connect - #543

Merged
linroid merged 3 commits into
mainfrom
feat/torrent-reachability
Oct 14, 2026
Merged

linroid merged 3 commits into
mainfrom
feat/torrent-reachability

Conversation

@linroid

@linroid linroid commented Oct 14, 2026

Copy link
Copy Markdown
Owner

What

Port mapping through every NAT layer. After mapping the listen port on the home router, if that router's own external address is still private (a second router, or a provider's carrier-grade NAT), Ketch asks the gateway outside it to forward the same port on to the router, up to three layers, until it reaches a public address.

  • Each layer tries NAT-PMP, then PCP (new PcpClient, RFC 6887 MAP), then UPnP. Outer layers' UPnP is a unicast SSDP search sent straight to the guessed gateway (UpnpClient.discoverAt), since multicast discovery can't cross a NAT.
  • Outer gateways are guessed from the inner layer's external address (outerGatewayGuesses): the first and last host of its /24 and the first host of its range (10.0.0.1, 100.64.0.1, 172.x.0.1).
  • Peers, trackers and DHT are told the outermost forwarded port. Mappings renew as before and are removed outermost first on stop.

Reachability status, on every device.

  • TorrentController.reachability() → TorrentReachability: status reachable (a peer connected in over TCP), forwarded (every layer up to a public address forwards the port and the DHT agrees on that address), blocked (a NAT outside the mapped routers won't forward), not-forwarded, or unknown (engine not running); plus the port, forwarded layers, incoming TCP peers and whether hole punching is on.
  • New reachability capability and GET /api/torrents/reachability; RemoteTorrentController fails closed against older servers without sending a request. Unknown wire statuses decode as unknown.
  • Settings → BitTorrent shows it as a notice for the local device and remote ones, refreshed every 10 s, and says whether hole punching (feat(torrent): punch through NATs with BEP 55 so peers behind them reach each other #541) still reaches peers when the port is blocked.

Why

Hole punching (#541) gets peers behind NATs talking, but a forwarded port is still the best path, and routers behind a second NAT never got one. Users also had no way to tell whether their device was reachable at all.

Notes for reviewers

  • Punched uTP connections don't count toward reachable, as they prove nothing about incoming reachability.
  • Tests use 20.0.113.x for "public" addresses, since publicTorrentAddress treats the documentation ranges as non-public.
  • New strings are English only; translations follow.
  • Not tried against a real multi-layer NAT yet: the outer NAT on my test network answers neither NAT-PMP nor PCP, so it should report blocked there.

Tests

  • NatLayersTest: a router behind a second NAT forwarding through both (and removal order), nothing answering outside the router (not public), a PCP-only gateway, and the gateway guesses.
  • TorrentReachabilityTest (engine before and after start), KetchTorrentControllerTest (capability gating), TorrentRoutesTest, RemoteTorrentControllerTest, TorrentContractsTest (wire names, unknown statuses) and TorrentReachabilityNoticeTest (text and tone per status).
  • :library:torrent:jvmTest, :app:shared:jvmTest, :library:mcp:test, :app:desktop:test, plus the torrent-api, remote and server suites pass.

…her peers can connect

- Port mapping goes on through the NAT layers in front of the device, up to three: while a
  router's own external address is still private, Ketch asks the gateway outside it (guessed from
  that address) to forward the port the router got, by NAT-PMP, PCP (new, RFC 6887) or UPnP sent
  to it directly. Peers are told the outermost forwarded port.
- The engine reports whether peers can connect (`TorrentController.reachability`, REST
  `GET /api/torrents/reachability`, the `reachability` capability): reachable once a peer
  connected over TCP, forwarded through every layer to a public address, blocked by a NAT outside
  the mapped routers, or not forwarded. RemoteKetch fails closed against older servers.
- Settings → BitTorrent shows it for this device and remote ones, and whether hole punching
  still reaches peers behind routers.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T00:43:11.353884Z ea63a8f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ea63a8f71a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

- Only TCP peers from public addresses count as reachable; LAN peers found by local discovery
  no longer turn a blocked device reachable.
- Forwarded waits for the DHT to see our address while DHT runs (unknown until then).
- The Settings notice follows the device's current entry and features, so polling moves to the
  client a reconnect creates.
…download finds it

RecordingKetchApi copied the engine's task list on Dispatchers.Default, so a DELETE sent right
after POST could miss the new task and answer 404, which failed DownloadRoutesTest now and then.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 900 files  +  485   1 927 suites  +485   24m 57s ⏱️ + 8m 6s
12 815 tests +4 276  12 570 ✅ +4 276  245 💤 ±0  0 ❌ ±0 
15 352 runs  +4 290  15 107 ✅ +4 290  245 💤 ±0  0 ❌ ±0 

Results for commit a768c3e. ± Comparison against base commit bafc86e.

@linroid
linroid merged commit e700474 into main Oct 14, 2026
14 checks passed
@linroid
linroid deleted the feat/torrent-reachability branch October 14, 2026 01:37
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