Repository navigation
feat(torrent): punch through NATs with BEP 55 so peers behind them reach each other - #541
Conversation
…ach each other Peers behind routers or carrier-grade NAT, however many layers, cannot dial each other, so a device that no port mapping can open only ever talks to peers it reaches itself. v1 swarms of public torrents now speak `ut_holepunch`: they relay a rendezvous between two connected peers with a CONNECT to each, ask the peer that introduced an endpoint they cannot dial to introduce them, and dial a peer they are introduced to over uTP at once, while it dials them.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df7711e590
ℹ️ 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".
A device behind more than one NAT can't be reached by any peer: port mapping only opens the nearest router. On the desktop this was found on, the path is computer → home router (e.g.
192.168.1.1) → a second NAT (e.g.10.0.0.1) → internet. The outer NAT answers neither NAT-PMP nor PCP, so every swarm reportedincoming [none]. Hole punching gets peers through anyway.What changes for users
ut_holepunch), as qBittorrent, Deluge and other libtorrent-based clients speak it.How
peer/PeerHolepunch.kt:HolepunchMessagecodec in BEP 55's layout (type, address type, IPv4 or IPv6 address, port,err_code).err_code.nulland are ignored rather than closing the peer.PeerExtensions.handshake(holepunch = …)advertisesut_holepunch;pexFlagssets BEP 11 flag0x08for peers that offer it.v1/SwarmHolepunch.kt, one per swarm run, knows the connected peers that take part and which peer introduced each endpoint by PEX.relay: sends a CONNECT to both peers, each naming the other: a peer reached over uTP by its uTP source endpoint, else where it listens. Otherwise it answers withNOT_CONNECTED,NO_SUPPORTorNO_SELF, and ignores peers that never offered the extension (BEP 55's MUST).rendezvous: asks the last introducer of an endpoint, at most once per endpoint in 5 minutes.TorrentSwarmwiring:utpEnabled, the engine's live uTP setting passed throughKotlinTorrentSession, gates everything (holepunchOn()).rendezvous.MAX_CONNECTS_PER_MINUTE) goes through apuncheschannel: withinMAX_CANDIDATES, the endpoint jumps the queue and is dialed uTP first (onUtpPeers), downloading or seeding.holepunch peers= relayed= asked= connects=.KetchDispatchersTest(library:core) that compared a pool thread's name while coroutine debug mode, on under Gradle's assertions, could still carry@coroutine#Non it.Tests
:library:torrent:jvmTestpasses in full.PeerHolepunchTestcovers the IPv4/IPv6 layout, a missingerr_code, ignored garbage and the handshake entry.SwarmHolepunchTestcovers a relay picking the uTP source or listen endpoint, each error code, ignoring non-participants, and introducer retry and leave.TorrentSwarmHolepunchTestdrives a real swarm with scripted peers on loopback:holepunchcounters are there to check it.Known limits