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.dialer.is_connection_direct(host, peer_id: ID) bool

Check if we have a direct connection to a peer.

examples.nat.dialer.main() None

Parse arguments and run the dialer node.

async examples.nat.dialer.setup_dialer_node(relay_addr: str, listener_id: str, seed: int | None = None) None

Set up and run a dialer node that connects through relay and attempts hole punching.

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.listener.main() None

Parse arguments and run the listener node.

async examples.nat.listener.setup_listener_node(port: int, relay_addr: str, seed: int | None = None) None

Set up and run a listener node behind NAT.

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.

examples.nat.relay.main() None

Parse arguments and run the relay node.

async examples.nat.relay.setup_relay_node(port: int, seed: int | None = None) None

Set up and run a relay node.

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