diff --git a/examples/advanced/network_discover.py b/examples/advanced/network_discover.py index 87b44ddf..71edd209 100644 --- a/examples/advanced/network_discover.py +++ b/examples/advanced/network_discover.py @@ -18,7 +18,7 @@ try: except ImportError: # Fallbacks if utilities are missing def get_available_interfaces(port: int, protocol: str = "tcp"): - return [Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}")] + return [Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}")] def expand_wildcard_address(addr: Multiaddr, port: int | None = None): if port is None: @@ -27,7 +27,7 @@ except ImportError: return [Multiaddr(addr_str + f"/{port}")] def get_optimal_binding_address(port: int, protocol: str = "tcp"): - return Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}") + return Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}") def main() -> None: diff --git a/examples/bootstrap/bootstrap.py b/examples/bootstrap/bootstrap.py index af7d08cc..93a6913a 100644 --- a/examples/bootstrap/bootstrap.py +++ b/examples/bootstrap/bootstrap.py @@ -59,7 +59,7 @@ async def run(port: int, bootstrap_addrs: list[str]) -> None: key_pair = create_new_key_pair(secret) # Create listen address - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Register peer discovery handler peerDiscovery.register_peer_discovered_handler(on_peer_discovery) diff --git a/examples/chat/chat.py b/examples/chat/chat.py index 05a9b918..c06e20a7 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -40,7 +40,7 @@ async def write_data(stream: INetStream) -> None: async def run(port: int, destination: str) -> None: - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") host = new_host() async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery: # Start the peer-store cleanup task diff --git a/examples/doc-examples/example_encryption_insecure.py b/examples/doc-examples/example_encryption_insecure.py index c1536808..089fb72f 100644 --- a/examples/doc-examples/example_encryption_insecure.py +++ b/examples/doc-examples/example_encryption_insecure.py @@ -40,7 +40,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_encryption_noise.py b/examples/doc-examples/example_encryption_noise.py index a2a4318c..7d037610 100644 --- a/examples/doc-examples/example_encryption_noise.py +++ b/examples/doc-examples/example_encryption_noise.py @@ -41,7 +41,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_encryption_secio.py b/examples/doc-examples/example_encryption_secio.py index 603ad6ea..3b1cb405 100644 --- a/examples/doc-examples/example_encryption_secio.py +++ b/examples/doc-examples/example_encryption_secio.py @@ -34,7 +34,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_multiplexer.py b/examples/doc-examples/example_multiplexer.py index 0d6f2662..6963ace0 100644 --- a/examples/doc-examples/example_multiplexer.py +++ b/examples/doc-examples/example_multiplexer.py @@ -41,7 +41,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_net_stream.py b/examples/doc-examples/example_net_stream.py index d8842bea..a77a7509 100644 --- a/examples/doc-examples/example_net_stream.py +++ b/examples/doc-examples/example_net_stream.py @@ -173,7 +173,7 @@ async def run_enhanced_demo( """ Run enhanced echo demo with NetStream state management. """ - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Generate or use provided key if seed: diff --git a/examples/doc-examples/example_peer_discovery.py b/examples/doc-examples/example_peer_discovery.py index 7ceec375..eb3e1914 100644 --- a/examples/doc-examples/example_peer_discovery.py +++ b/examples/doc-examples/example_peer_discovery.py @@ -44,7 +44,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_running.py b/examples/doc-examples/example_running.py index a0169931..7f3ade32 100644 --- a/examples/doc-examples/example_running.py +++ b/examples/doc-examples/example_running.py @@ -41,7 +41,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/doc-examples/example_transport.py b/examples/doc-examples/example_transport.py index e981fa7d..8f4c9fa1 100644 --- a/examples/doc-examples/example_transport.py +++ b/examples/doc-examples/example_transport.py @@ -21,7 +21,7 @@ async def main(): # Configure the listening address port = 8000 - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Start the host async with host.run(listen_addrs=[listen_addr]): diff --git a/examples/identify/identify.py b/examples/identify/identify.py index 98980f99..445962c3 100644 --- a/examples/identify/identify.py +++ b/examples/identify/identify.py @@ -58,7 +58,7 @@ def print_identify_response(identify_response: Identify): async def run(port: int, destination: str, use_varint_format: bool = True) -> None: - localhost_ip = "0.0.0.0" + localhost_ip = "127.0.0.1" if not destination: # Create first host (listener) @@ -79,10 +79,9 @@ async def run(port: int, destination: str, use_varint_format: bool = True) -> No # Start the peer-store cleanup task nursery.start_soon(host_a.get_peerstore().start_cleanup_task, 60) - # Get the actual address and replace 0.0.0.0 with 127.0.0.1 for client - # connections + # Get the actual address server_addr = str(host_a.get_addrs()[0]) - client_addr = server_addr.replace("/ip4/0.0.0.0/", "/ip4/127.0.0.1/") + client_addr = server_addr format_name = "length-prefixed" if use_varint_format else "raw protobuf" format_flag = "--raw-format" if not use_varint_format else "" diff --git a/examples/identify_push/identify_push_listener_dialer.py b/examples/identify_push/identify_push_listener_dialer.py index c23e62bb..a9974b82 100644 --- a/examples/identify_push/identify_push_listener_dialer.py +++ b/examples/identify_push/identify_push_listener_dialer.py @@ -216,7 +216,7 @@ async def run_listener( ) # Start listening - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") try: async with host.run([listen_addr]): @@ -275,7 +275,7 @@ async def run_dialer( ) # Start listening on a different port - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") async with host.run([listen_addr]): logger.info("Dialer host ready!") diff --git a/examples/mDNS/mDNS.py b/examples/mDNS/mDNS.py index d3f11b56..499ca224 100644 --- a/examples/mDNS/mDNS.py +++ b/examples/mDNS/mDNS.py @@ -33,7 +33,7 @@ def onPeerDiscovery(peerinfo: PeerInfo): async def run(port: int) -> None: secret = secrets.token_bytes(32) key_pair = create_new_key_pair(secret) - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") peerDiscovery.register_peer_discovered_handler(onPeerDiscovery) diff --git a/examples/ping/ping.py b/examples/ping/ping.py index d1a5daae..bb47bd95 100644 --- a/examples/ping/ping.py +++ b/examples/ping/ping.py @@ -55,7 +55,7 @@ async def send_ping(stream: INetStream) -> None: async def run(port: int, destination: str) -> None: - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") host = new_host(listen_addrs=[listen_addr]) async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery: diff --git a/examples/pubsub/pubsub.py b/examples/pubsub/pubsub.py index 41545658..843a2829 100644 --- a/examples/pubsub/pubsub.py +++ b/examples/pubsub/pubsub.py @@ -109,7 +109,7 @@ async def run(topic: str, destination: str | None, port: int | None) -> None: port = find_free_port() logger.info(f"Using random available port: {port}") - listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") # Create a new libp2p host host = new_host( diff --git a/examples/random_walk/random_walk.py b/examples/random_walk/random_walk.py index 845ccd57..b90d6304 100644 --- a/examples/random_walk/random_walk.py +++ b/examples/random_walk/random_walk.py @@ -130,7 +130,7 @@ async def run_node(port: int, mode: str, demo_interval: int = 30) -> None: # Create host and DHT key_pair = create_new_key_pair(secrets.token_bytes(32)) host = new_host(key_pair=key_pair, bootstrap=DEFAULT_BOOTSTRAP_NODES) - listen_addr = Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") + listen_addr = Multiaddr(f"/ip4/127.0.0.1/tcp/{port}") async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery: # Start maintenance tasks @@ -139,7 +139,7 @@ async def run_node(port: int, mode: str, demo_interval: int = 30) -> None: peer_id = host.get_id().pretty() logger.info(f"Node peer ID: {peer_id}") - logger.info(f"Node address: /ip4/0.0.0.0/tcp/{port}/p2p/{peer_id}") + logger.info(f"Node address: /ip4/127.0.0.1/tcp/{port}/p2p/{peer_id}") # Create and start DHT with Random Walk enabled dht = KadDHT(host, dht_mode, enable_random_walk=True) diff --git a/libp2p/utils/address_validation.py b/libp2p/utils/address_validation.py index 77b797a1..10677241 100644 --- a/libp2p/utils/address_validation.py +++ b/libp2p/utils/address_validation.py @@ -99,7 +99,7 @@ def get_available_interfaces(port: int, protocol: str = "tcp") -> list[Multiaddr # Fallback if nothing discovered if not addrs: - addrs.append(Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}")) + addrs.append(Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}")) return addrs @@ -148,8 +148,8 @@ def get_optimal_binding_address(port: int, protocol: str = "tcp") -> Multiaddr: if "/ip4/127." in str(c) or "/ip6/::1" in str(c): return c - # As a final fallback, produce a wildcard - return Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}") + # As a final fallback, produce a loopback address + return Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}") __all__ = [