diff --git a/docs/examples.circuit_relay.rst b/docs/examples.circuit_relay.rst index 85326b00..055aafdf 100644 --- a/docs/examples.circuit_relay.rst +++ b/docs/examples.circuit_relay.rst @@ -36,12 +36,14 @@ Create a file named ``relay_node.py`` with the following content: from libp2p.relay.circuit_v2.transport import CircuitV2Transport from libp2p.relay.circuit_v2.config import RelayConfig from libp2p.tools.async_service import background_trio_service + from libp2p.utils import get_wildcard_address logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("relay_node") async def run_relay(): - listen_addr = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/9000") + # Use wildcard address to listen on all interfaces + listen_addr = get_wildcard_address(9000) host = new_host() config = RelayConfig( @@ -107,6 +109,7 @@ Create a file named ``destination_node.py`` with the following content: from libp2p.relay.circuit_v2.config import RelayConfig from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.tools.async_service import background_trio_service + from libp2p.utils import get_wildcard_address logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("destination_node") @@ -139,7 +142,8 @@ Create a file named ``destination_node.py`` with the following content: Run a simple destination node that accepts connections. This is a simplified version that doesn't use the relay functionality. """ - listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/9001") + # Create a libp2p host - use wildcard address to listen on all interfaces + listen_addr = get_wildcard_address(9001) host = new_host() # Configure as a relay receiver (stop) @@ -252,14 +256,15 @@ Create a file named ``source_node.py`` with the following content: from libp2p.peer.peerinfo import info_from_p2p_addr from libp2p.tools.async_service import background_trio_service from libp2p.relay.circuit_v2.discovery import RelayInfo + from libp2p.utils import get_wildcard_address # Configure logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("source_node") async def run_source(relay_peer_id=None, destination_peer_id=None): - # Create a libp2p host - listen_addr = multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/9002") + # Create a libp2p host - use wildcard address to listen on all interfaces + listen_addr = get_wildcard_address(9002) host = new_host() # Configure as a relay client diff --git a/examples/doc-examples/multiple_connections_example.py b/examples/doc-examples/multiple_connections_example.py index ebc8119f..20a7fd86 100644 --- a/examples/doc-examples/multiple_connections_example.py +++ b/examples/doc-examples/multiple_connections_example.py @@ -156,7 +156,7 @@ async def example_production_ready_config() -> None: # Production-ready connection configuration connection_config = ConnectionConfig( max_connections_per_peer=3, # Balance between performance and resource usage - connection_timeout=30.0, # Reasonable timeouta + connection_timeout=30.0, # Reasonable timeout load_balancing_strategy="round_robin", # Simple, predictable strategy ) diff --git a/examples/identify/identify.py b/examples/identify/identify.py index e62e5ff7..327ea4d6 100644 --- a/examples/identify/identify.py +++ b/examples/identify/identify.py @@ -104,7 +104,9 @@ async def run(port: int, destination: str, use_varint_format: bool = True) -> No # Use optimal address for the client command optimal_addr = get_optimal_binding_address(port) - optimal_addr_with_peer = f"{optimal_addr}/p2p/{host_a.get_id().to_string()}" + optimal_addr_with_peer = ( + f"{optimal_addr}/p2p/{host_a.get_id().to_string()}" + ) print( f"\nRun this from the same folder in another console:\n\n" f"identify-demo -d {optimal_addr_with_peer}\n" @@ -119,7 +121,9 @@ async def run(port: int, destination: str, use_varint_format: bool = True) -> No # Use optimal address for the client command optimal_addr = get_optimal_binding_address(port) - optimal_addr_with_peer = f"{optimal_addr}/p2p/{host_a.get_id().to_string()}" + optimal_addr_with_peer = ( + f"{optimal_addr}/p2p/{host_a.get_id().to_string()}" + ) print( f"\nRun this from the same folder in another console:\n\n" f"identify-demo -d {optimal_addr_with_peer}\n" diff --git a/newsfragments/885.feature.rst b/newsfragments/885.feature.rst index e0566b6a..4b2074eb 100644 --- a/newsfragments/885.feature.rst +++ b/newsfragments/885.feature.rst @@ -1,2 +1,2 @@ -Enhanced security by defaulting to loopback address (127.0.0.1) instead of wildcard binding. -All examples and core modules now use secure default addresses to prevent unintended public exposure. +Updated all example scripts and core modules to use secure loopback addresses instead of wildcard addresses for network binding. +The `get_wildcard_address` function and related logic now utilize all available interfaces safely, improving security and consistency across the codebase. \ No newline at end of file diff --git a/tests/core/pubsub/test_gossipsub_px_and_backoff.py b/tests/core/pubsub/test_gossipsub_px_and_backoff.py index 72ad5f9d..26119557 100644 --- a/tests/core/pubsub/test_gossipsub_px_and_backoff.py +++ b/tests/core/pubsub/test_gossipsub_px_and_backoff.py @@ -65,7 +65,7 @@ async def test_prune_backoff(): @pytest.mark.trio async def test_unsubscribe_backoff(): async with PubsubFactory.create_batch_with_gossipsub( - 2, heartbeat_interval=1, prune_back_off=1, unsubscribe_back_off=2 + 2, heartbeat_interval=0.5, prune_back_off=2, unsubscribe_back_off=4 ) as pubsubs: gsub0 = pubsubs[0].router gsub1 = pubsubs[1].router