mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
chore: Update network address config to use loopback (127.0.0.1) instead of wildcard (0.0.0.0) across multiple examples and utilities.
This commit is contained in:
@ -18,7 +18,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
# Fallbacks if utilities are missing
|
# Fallbacks if utilities are missing
|
||||||
def get_available_interfaces(port: int, protocol: str = "tcp"):
|
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):
|
def expand_wildcard_address(addr: Multiaddr, port: int | None = None):
|
||||||
if port is None:
|
if port is None:
|
||||||
@ -27,7 +27,7 @@ except ImportError:
|
|||||||
return [Multiaddr(addr_str + f"/{port}")]
|
return [Multiaddr(addr_str + f"/{port}")]
|
||||||
|
|
||||||
def get_optimal_binding_address(port: int, protocol: str = "tcp"):
|
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:
|
def main() -> None:
|
||||||
|
|||||||
@ -59,7 +59,7 @@ async def run(port: int, bootstrap_addrs: list[str]) -> None:
|
|||||||
key_pair = create_new_key_pair(secret)
|
key_pair = create_new_key_pair(secret)
|
||||||
|
|
||||||
# Create listen address
|
# 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
|
# Register peer discovery handler
|
||||||
peerDiscovery.register_peer_discovered_handler(on_peer_discovery)
|
peerDiscovery.register_peer_discovered_handler(on_peer_discovery)
|
||||||
|
|||||||
@ -40,7 +40,7 @@ async def write_data(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str) -> 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()
|
host = new_host()
|
||||||
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
|
||||||
|
|||||||
@ -40,7 +40,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -41,7 +41,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -34,7 +34,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -41,7 +41,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -173,7 +173,7 @@ async def run_enhanced_demo(
|
|||||||
"""
|
"""
|
||||||
Run enhanced echo demo with NetStream state management.
|
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
|
# Generate or use provided key
|
||||||
if seed:
|
if seed:
|
||||||
|
|||||||
@ -44,7 +44,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -41,7 +41,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -21,7 +21,7 @@ async def main():
|
|||||||
|
|
||||||
# Configure the listening address
|
# Configure the listening address
|
||||||
port = 8000
|
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
|
# Start the host
|
||||||
async with host.run(listen_addrs=[listen_addr]):
|
async with host.run(listen_addrs=[listen_addr]):
|
||||||
|
|||||||
@ -58,7 +58,7 @@ def print_identify_response(identify_response: Identify):
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str, use_varint_format: bool = True) -> None:
|
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:
|
if not destination:
|
||||||
# Create first host (listener)
|
# 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
|
# Start the peer-store cleanup task
|
||||||
nursery.start_soon(host_a.get_peerstore().start_cleanup_task, 60)
|
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
|
# Get the actual address
|
||||||
# connections
|
|
||||||
server_addr = str(host_a.get_addrs()[0])
|
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_name = "length-prefixed" if use_varint_format else "raw protobuf"
|
||||||
format_flag = "--raw-format" if not use_varint_format else ""
|
format_flag = "--raw-format" if not use_varint_format else ""
|
||||||
|
|||||||
@ -216,7 +216,7 @@ async def run_listener(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Start listening
|
# 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:
|
try:
|
||||||
async with host.run([listen_addr]):
|
async with host.run([listen_addr]):
|
||||||
@ -275,7 +275,7 @@ async def run_dialer(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Start listening on a different port
|
# 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]):
|
async with host.run([listen_addr]):
|
||||||
logger.info("Dialer host ready!")
|
logger.info("Dialer host ready!")
|
||||||
|
|||||||
@ -33,7 +33,7 @@ def onPeerDiscovery(peerinfo: PeerInfo):
|
|||||||
async def run(port: int) -> None:
|
async def run(port: int) -> None:
|
||||||
secret = secrets.token_bytes(32)
|
secret = secrets.token_bytes(32)
|
||||||
key_pair = create_new_key_pair(secret)
|
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)
|
peerDiscovery.register_peer_discovered_handler(onPeerDiscovery)
|
||||||
|
|
||||||
|
|||||||
@ -55,7 +55,7 @@ async def send_ping(stream: INetStream) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def run(port: int, destination: str) -> 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])
|
host = new_host(listen_addrs=[listen_addr])
|
||||||
|
|
||||||
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:
|
||||||
|
|||||||
@ -109,7 +109,7 @@ async def run(topic: str, destination: str | None, port: int | None) -> None:
|
|||||||
port = find_free_port()
|
port = find_free_port()
|
||||||
logger.info(f"Using random available port: {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
|
# Create a new libp2p host
|
||||||
host = new_host(
|
host = new_host(
|
||||||
|
|||||||
@ -130,7 +130,7 @@ async def run_node(port: int, mode: str, demo_interval: int = 30) -> None:
|
|||||||
# Create host and DHT
|
# Create host and DHT
|
||||||
key_pair = create_new_key_pair(secrets.token_bytes(32))
|
key_pair = create_new_key_pair(secrets.token_bytes(32))
|
||||||
host = new_host(key_pair=key_pair, bootstrap=DEFAULT_BOOTSTRAP_NODES)
|
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:
|
async with host.run(listen_addrs=[listen_addr]), trio.open_nursery() as nursery:
|
||||||
# Start maintenance tasks
|
# 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()
|
peer_id = host.get_id().pretty()
|
||||||
logger.info(f"Node peer ID: {peer_id}")
|
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
|
# Create and start DHT with Random Walk enabled
|
||||||
dht = KadDHT(host, dht_mode, enable_random_walk=True)
|
dht = KadDHT(host, dht_mode, enable_random_walk=True)
|
||||||
|
|||||||
@ -99,7 +99,7 @@ def get_available_interfaces(port: int, protocol: str = "tcp") -> list[Multiaddr
|
|||||||
|
|
||||||
# Fallback if nothing discovered
|
# Fallback if nothing discovered
|
||||||
if not addrs:
|
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
|
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):
|
if "/ip4/127." in str(c) or "/ip6/::1" in str(c):
|
||||||
return c
|
return c
|
||||||
|
|
||||||
# As a final fallback, produce a wildcard
|
# As a final fallback, produce a loopback address
|
||||||
return Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}")
|
return Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}")
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
|||||||
Reference in New Issue
Block a user