mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
feat: Enhance echo example to dynamically find free ports and improve address handling
- Added a function to find a free port on localhost. - Updated the run function to use the new port finding logic when a non-positive port is provided. - Modified address printing to handle multiple listen addresses correctly. - Improved the get_available_interfaces function to ensure the IPv4 loopback address is included.
This commit is contained in:
@ -1,4 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
import random
|
||||||
|
import secrets
|
||||||
|
import socket
|
||||||
|
|
||||||
import multiaddr
|
import multiaddr
|
||||||
import trio
|
import trio
|
||||||
@ -12,23 +15,30 @@ from libp2p.crypto.secp256k1 import (
|
|||||||
from libp2p.custom_types import (
|
from libp2p.custom_types import (
|
||||||
TProtocol,
|
TProtocol,
|
||||||
)
|
)
|
||||||
from libp2p.network.stream.net_stream import (
|
|
||||||
INetStream,
|
|
||||||
)
|
|
||||||
from libp2p.network.stream.exceptions import (
|
from libp2p.network.stream.exceptions import (
|
||||||
StreamEOF,
|
StreamEOF,
|
||||||
)
|
)
|
||||||
|
from libp2p.network.stream.net_stream import (
|
||||||
|
INetStream,
|
||||||
|
)
|
||||||
from libp2p.peer.peerinfo import (
|
from libp2p.peer.peerinfo import (
|
||||||
info_from_p2p_addr,
|
info_from_p2p_addr,
|
||||||
)
|
)
|
||||||
from libp2p.utils.address_validation import (
|
from libp2p.utils.address_validation import (
|
||||||
get_optimal_binding_address,
|
get_available_interfaces,
|
||||||
)
|
)
|
||||||
|
|
||||||
PROTOCOL_ID = TProtocol("/echo/1.0.0")
|
PROTOCOL_ID = TProtocol("/echo/1.0.0")
|
||||||
MAX_READ_LEN = 2**32 - 1
|
MAX_READ_LEN = 2**32 - 1
|
||||||
|
|
||||||
|
|
||||||
|
def find_free_port():
|
||||||
|
"""Find a free port on localhost."""
|
||||||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||||
|
s.bind(("", 0)) # Bind to a free port provided by the OS
|
||||||
|
return s.getsockname()[1]
|
||||||
|
|
||||||
|
|
||||||
async def _echo_stream_handler(stream: INetStream) -> None:
|
async def _echo_stream_handler(stream: INetStream) -> None:
|
||||||
try:
|
try:
|
||||||
peer_id = stream.muxed_conn.peer_id
|
peer_id = stream.muxed_conn.peer_id
|
||||||
@ -47,19 +57,19 @@ async def _echo_stream_handler(stream: INetStream) -> None:
|
|||||||
|
|
||||||
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
async def run(port: int, destination: str, seed: int | None = None) -> None:
|
||||||
# CHANGED: previously hardcoded 0.0.0.0
|
# CHANGED: previously hardcoded 0.0.0.0
|
||||||
listen_addr = get_optimal_binding_address(port)
|
if port <= 0:
|
||||||
|
port = find_free_port()
|
||||||
|
listen_addr = get_available_interfaces(port)
|
||||||
|
|
||||||
if seed:
|
if seed:
|
||||||
import random
|
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
secret_number = random.getrandbits(32 * 8)
|
secret_number = random.getrandbits(32 * 8)
|
||||||
secret = secret_number.to_bytes(length=32, byteorder="big")
|
secret = secret_number.to_bytes(length=32, byteorder="big")
|
||||||
else:
|
else:
|
||||||
import secrets
|
|
||||||
secret = secrets.token_bytes(32)
|
secret = secrets.token_bytes(32)
|
||||||
|
|
||||||
host = new_host(key_pair=create_new_key_pair(secret))
|
host = new_host(key_pair=create_new_key_pair(secret))
|
||||||
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
|
async with host.run(listen_addrs=listen_addr), trio.open_nursery() as nursery:
|
||||||
# Start the peer-store cleanup task
|
# Start the peer-store cleanup task
|
||||||
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
|
nursery.start_soon(host.get_peerstore().start_cleanup_task, 60)
|
||||||
|
|
||||||
@ -69,9 +79,10 @@ async def run(port: int, destination: str, seed: int | None = None) -> None:
|
|||||||
host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler)
|
host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler)
|
||||||
|
|
||||||
# Print all listen addresses with peer ID (JS parity)
|
# Print all listen addresses with peer ID (JS parity)
|
||||||
print("Listener ready, listening on:")
|
print("Listener ready, listening on:\n")
|
||||||
peer_id = host.get_id().to_string()
|
peer_id = host.get_id().to_string()
|
||||||
print(f"{listen_addr}/p2p/{peer_id}")
|
for addr in listen_addr:
|
||||||
|
print(f"{addr}/p2p/{peer_id}")
|
||||||
|
|
||||||
print(
|
print(
|
||||||
"\nRun this from the same folder in another console:\n\n"
|
"\nRun this from the same folder in another console:\n\n"
|
||||||
|
|||||||
@ -67,6 +67,10 @@ def get_available_interfaces(port: int, protocol: str = "tcp") -> list[Multiaddr
|
|||||||
seen_v4.add(ip)
|
seen_v4.add(ip)
|
||||||
addrs.append(Multiaddr(f"/ip4/{ip}/{protocol}/{port}"))
|
addrs.append(Multiaddr(f"/ip4/{ip}/{protocol}/{port}"))
|
||||||
|
|
||||||
|
# Ensure IPv4 loopback is always included when IPv4 interfaces are discovered
|
||||||
|
if seen_v4 and "127.0.0.1" not in seen_v4:
|
||||||
|
addrs.append(Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}"))
|
||||||
|
|
||||||
seen_v6: set[str] = set()
|
seen_v6: set[str] = set()
|
||||||
for ip in _safe_get_network_addrs(6):
|
for ip in _safe_get_network_addrs(6):
|
||||||
seen_v6.add(ip)
|
seen_v6.add(ip)
|
||||||
|
|||||||
Reference in New Issue
Block a user