A libp2p hole-punching example using Docker Compose, where 2 nodes behind NAT try to establish direct connection with the help of a relay server. It runs in a single machine, simulating NAT environments using container networks.
The server node provides the following services:
- DHT bootstrap node for listener and dialer.
- NAT service for listener and dialer to learn their own public addresses.
- Relay service
Simulating routers with iptables rules.
A listening node behind NAT, it will advertise itself to the DHT so that the dialer can discover its relayed address.
A dialing node behind NAT, it will first connect to listener through the relay server. Hole punching begins automatically after the connection is established, so that subsequent streams will be opened with a direct link.
docker compose build
docker compose up