examples.nat package
Submodules
examples.nat.dialer module
NAT Traversal Dialer Node Example.
This module implements a NAT’d peer that: - Connects to a destination through a Circuit Relay v2 relay - Attempts DCUtR (Direct Connection Upgrade through Relay) hole punching - Uses AutoNAT to detect and report reachability status - Logs the connection path (direct or relayed)
- Usage:
python dialer.py –relay-addr /ip4/127.0.0.1/tcp/8000/p2p/RELAY_PEER_ID –listener-id LISTENER_PEER_ID
- examples.nat.dialer.generate_fixed_private_key(seed: int | None) bytes
Generate a fixed private key from a seed for reproducible peer IDs.
- examples.nat.dialer.get_autonat_status_string(status: int) str
Convert AutoNAT status to human-readable string.
examples.nat.listener module
NAT Traversal Listener Node Example.
This module implements a NAT’d peer that: - Advertises via a Circuit Relay v2 relay - Supports DCUtR (Direct Connection Upgrade through Relay) for hole punching - Uses AutoNAT to detect and report reachability status - Accepts incoming connections via relay and supports direct upgrades
- Usage:
- python listener.py –port 8001 \
–relay-addr /ip4/127.0.0.1/tcp/8000/p2p/RELAY_PEER_ID
- examples.nat.listener.generate_fixed_private_key(seed: int | None) bytes
Generate a fixed private key from a seed for reproducible peer IDs.
- examples.nat.listener.get_autonat_status_string(status: int) str
Convert AutoNAT status to human-readable string.
- async examples.nat.listener.handle_example_protocol(stream: INetStream) None
Handle incoming messages on our example protocol.
- async examples.nat.listener.log_autonat_status(autonat_service: AutoNATService, interval: float = 5.0) None
Periodically log AutoNAT status.
examples.nat.relay module
Circuit Relay v2 Relay Node for NAT Traversal Example.
This module implements a publicly reachable relay node that facilitates connections between NATed peers using Circuit Relay v2.
- Usage:
python relay.py –port 8000
- examples.nat.relay.generate_fixed_private_key(seed: int | None) bytes
Generate a fixed private key from a seed for reproducible peer IDs.
- async examples.nat.relay.handle_example_protocol(stream: INetStream) None
Handle incoming messages on our example protocol.
Module contents
NAT Traversal Examples for py-libp2p
This package contains examples demonstrating NAT traversal using: - Circuit Relay v2: Relay connections through publicly reachable nodes - DCUtR: Direct Connection Upgrade through Relay (hole punching) - AutoNAT: Automatic NAT detection and reachability assessment
Examples: - relay.py: Publicly reachable relay node - listener.py: NAT’d node that advertises via relay - dialer.py: NAT’d node that connects via relay and attempts hole punching