async def run(port: int, destination: str, localhost: bool, seed: int = None) -> None: if localhost: ip = "127.0.0.1" else: ip = urllib.request.urlopen("https://v4.ident.me/").read().decode( "utf8") transport_opt = f"/ip4/{ip}/tcp/{port}" if seed: import random random.seed(seed) secret_number = random.getrandbits(32 * 8) secret = secret_number.to_bytes(length=32, byteorder="big") else: import secrets secret = secrets.token_bytes(32) host = await new_node(key_pair=create_new_key_pair(secret), transport_opt=[transport_opt]) print(f"I am {host.get_id().to_string()}") await host.get_network().listen(multiaddr.Multiaddr(transport_opt)) if not destination: # its the server host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler) localhost_opt = " --localhost" if localhost else "" print( f"Run 'python ./examples/echo/echo.py" + localhost_opt + f" -p {int(port) + 1} -d /ip4/{ip}/tcp/{port}/p2p/{host.get_id().pretty()}'" + " on another console.") print("Waiting for incoming connections...") else: # its the client maddr = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(maddr) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) msg = b"hi, there!\n" await stream.write(msg) # Notify the other side about EOF await stream.close() response = await stream.read() print(f"Sent: {msg}") print(f"Got: {response}")
async def run(port: int, destination: str) -> None: localhost_ip = "127.0.0.1" listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") host = new_host() async with host.run( listen_addrs=[listen_addr]), trio.open_nursery() as nursery: if not destination: # its the server async def stream_handler(stream: INetStream) -> None: nursery.start_soon(read_data, stream) nursery.start_soon(write_data, stream) host.set_stream_handler(PROTOCOL_ID, stream_handler) print( f"Run 'python ./examples/chat/chat.py " f"-p {int(port) + 1} " f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}' " "on another console.") print("Waiting for incoming connection...") else: # its the client maddr = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(maddr) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) nursery.start_soon(read_data, stream) nursery.start_soon(write_data, stream) print(f"Connected to peer {info.addrs[0]}") await trio.sleep_forever()
async def connect(node1, node2): """ Connect node1 to node2 """ addr = node2.get_addrs()[0] info = info_from_p2p_addr(addr) await node1.connect(info)
async def test_chat(test, security_protocol): print("!@# ", security_protocol) async with HostFactory.create_batch_and_listen( 2, security_protocol=security_protocol) as hosts: addr = hosts[0].get_addrs()[0] info = info_from_p2p_addr(addr) await hosts[1].connect(info) await test(hosts[0], hosts[1])
def test_info_from_p2p_addr_valid(): m_addr = multiaddr.Multiaddr(VALID_MULTI_ADDR_STR) info = info_from_p2p_addr(m_addr) assert ( info.peer_id.pretty() == "3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ" ) assert len(info.addrs) == 1 assert str(info.addrs[0]) == "/ip4/127.0.0.1/tcp/8000"
async def test_chat(test): transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] (host_a, host_b) = await set_up_nodes_by_transport_opt(transport_opt_list) addr = host_a.get_addrs()[0] info = info_from_p2p_addr(addr) await host_b.connect(info) await test(host_a, host_b)
def test_info_from_p2p_addr(): # pylint: disable=line-too-long m_addr = multiaddr.Multiaddr( '/ip4/127.0.0.1/tcp/8000/p2p/3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ' ) info = info_from_p2p_addr(m_addr) assert info.peer_id.pretty( ) == '3YgLAeMKSAPcGqZkAt8mREqhQXmJT8SN8VCMN4T6ih4GNX9wvK8mWJnWZ1qA2mLdCQ' assert len(info.addrs) == 1 assert str(info.addrs[0]) == '/ip4/127.0.0.1/tcp/8000'
async def test_chat(test): host_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) host_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) addr = host_a.get_addrs()[0] info = info_from_p2p_addr(addr) await host_b.connect(info) await test(host_a, host_b) await cleanup()
async def run(port: int, destination: str, seed: int = None) -> None: localhost_ip = "127.0.0.1" listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") if seed: import random random.seed(seed) secret_number = random.getrandbits(32 * 8) secret = secret_number.to_bytes(length=32, byteorder="big") else: import secrets secret = secrets.token_bytes(32) host = new_host(key_pair=create_new_key_pair(secret)) async with host.run(listen_addrs=[listen_addr]): print(f"I am {host.get_id().to_string()}") if not destination: # its the server host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler) print( f"Run 'python ./examples/echo/echo.py " f"-p {int(port) + 1} " f"-d /ip4/{localhost_ip}/tcp/{port}/p2p/{host.get_id().pretty()}' " "on another console.") print("Waiting for incoming connections...") await trio.sleep_forever() else: # its the client maddr = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(maddr) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) msg = b"hi, there!\n" await stream.write(msg) # Notify the other side about EOF await stream.close() response = await stream.read() print(f"Sent: {msg}") print(f"Got: {response}")
async def run(port, destination): host = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/%s" % port]) m = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(m) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) asyncio.ensure_future(read_data(stream)) asyncio.ensure_future(write_data(stream)) print("Connected to peer %s" % info.addrs[0])
def peer_info(self) -> None: if self._peer_info is not None: return self._peer_info if not self.event_ready.is_set(): raise Exception("process is not ready yet. failed to parse the peer info") # Example: # b"I am /ip4/127.0.0.1/tcp/56171/ipfs/QmU41TRPs34WWqa1brJEojBLYZKrrBcJq9nyNfVvSrbZUJ\n" m = re.search(br"I am ([\w\./]+)", self.bytes_read) if m is None: raise Exception("failed to find the pattern for the listening multiaddr") maddr_bytes_str_ipfs = m.group(1) maddr_str = maddr_bytes_str_ipfs.decode().replace("ipfs", "p2p") maddr = Multiaddr(maddr_str) self._peer_info = info_from_p2p_addr(maddr) return self._peer_info
async def make_echo_proc( proc_factory, port: int, is_secure: bool, destination: Multiaddr = None ): args = [f"-l={port}"] if not is_secure: args.append("-insecure") if destination is not None: args.append(f"-d={str(destination)}") echo_proc = proc_factory(str(ECHO_PATH), args) await echo_proc.expect(r"I am ([\w\./]+)" + PEXPECT_NEW_LINE, async_=True) maddr_str_ipfs = echo_proc.match.group(1) maddr_str = maddr_str_ipfs.replace("ipfs", "p2p") maddr = Multiaddr(maddr_str) go_pinfo = info_from_p2p_addr(maddr) if destination is None: await echo_proc.expect("listening for connections", async_=True) return echo_proc, go_pinfo
async def connect_peer(config, peer): info = info_from_p2p_addr(multiaddr.Multiaddr(peer)) if str(info.peer_id) == str(singleton.host.get_id()): # LOGGER.debug("Can't connect to myself.") return if 'streamer' in config.p2p.clients.value: if not await singleton.streamer.has_active_streams(info.peer_id): # network = singleton.host.get_network() # if info.peer_id in network.connections: # await network.close_peer(info.peer_id) # del network[info.peer_id] await singleton.host.connect(info) await singleton.streamer.create_connections(info.peer_id) else: await singleton.host.connect(info)
async def run(port: int, destination: str, localhost: bool) -> None: if localhost: ip = "127.0.0.1" else: ip = urllib.request.urlopen("https://v4.ident.me/").read().decode("utf8") transport_opt = f"/ip4/{ip}/tcp/{port}" host = await new_node(transport_opt=[transport_opt]) await host.get_network().listen(multiaddr.Multiaddr(transport_opt)) if not destination: # its the server async def stream_handler(stream: INetStream) -> None: asyncio.ensure_future(read_data(stream)) asyncio.ensure_future(write_data(stream)) host.set_stream_handler(PROTOCOL_ID, stream_handler) localhost_opt = " --localhost" if localhost else "" print( f"Run 'python ./examples/chat/chat.py" + localhost_opt + f" -p {int(port) + 1} -d /ip4/{ip}/tcp/{port}/p2p/{host.get_id().pretty()}'" + " on another console." ) print("Waiting for incoming connection...") else: # its the client maddr = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(maddr) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) asyncio.ensure_future(read_data(stream)) asyncio.ensure_future(write_data(stream)) print("Connected to peer %s" % info.addrs[0])
async def run(port, destination): external_ip = urllib.request.urlopen('https://v4.ident.me/').read().decode( 'utf8') host = await new_node( transport_opt=["/ip4/%s/tcp/%s" % (external_ip, port)]) if not destination: # its the server async def stream_handler(stream): asyncio.ensure_future(read_data(stream)) asyncio.ensure_future(write_data(stream)) host.set_stream_handler(PROTOCOL_ID, stream_handler) port = None ip = None for listener in host.network.listeners.values(): for addr in listener.get_addrs(): ip = addr.value_for_protocol('ip4') port = int(addr.value_for_protocol('tcp')) if not port: raise RuntimeError("was not able to find the actual local port") print( "Run './examples/chat/chat.py -p %s -d /ip4/%s/tcp/%s/p2p/%s' on another console.\n" % (int(port) + 1, ip, port, host.get_id().pretty())) print("\nWaiting for incoming connection\n\n") else: # its the client m = multiaddr.Multiaddr(destination) info = info_from_p2p_addr(m) # Associate the peer with local ip address await host.connect(info) # Start a stream with the destination. # Multiaddress of the destination peer is fetched from the peerstore using 'peerId'. stream = await host.new_stream(info.peer_id, [PROTOCOL_ID]) asyncio.ensure_future(read_data(stream)) asyncio.ensure_future(write_data(stream)) print("Connected to peer %s" % info.addrs[0])
async def test_host_connect(): transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]] (node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list) assert not node_a.get_peerstore().peer_ids() addr = node_b.get_addrs()[0] info = info_from_p2p_addr(addr) await node_a.connect(info) assert len(node_a.get_peerstore().peer_ids()) == 1 await node_a.connect(info) # make sure we don't do double connection assert len(node_a.get_peerstore().peer_ids()) == 1 assert node_b.get_id() in node_a.get_peerstore().peer_ids() ma_node_b = multiaddr.Multiaddr("/p2p/%s" % node_b.get_id().pretty()) for addr in node_a.get_peerstore().addrs(node_b.get_id()): assert addr.encapsulate(ma_node_b) in node_b.get_addrs()
async def make_p2pd( daemon_control_port: int, client_callback_port: int, security_protocol: TProtocol, is_pubsub_enabled: bool = True, is_gossipsub: bool = True, is_pubsub_signing: bool = False, is_pubsub_signing_strict: bool = False, ) -> AsyncIterator[Daemon]: control_maddr = Multiaddr(f"/ip4/{LOCALHOST_IP}/tcp/{daemon_control_port}") p2pd_proc = P2PDProcess( control_maddr, security_protocol, is_pubsub_enabled, is_gossipsub, is_pubsub_signing, is_pubsub_signing_strict, ) await p2pd_proc.start() client_callback_maddr = Multiaddr( f"/ip4/{LOCALHOST_IP}/tcp/{client_callback_port}") p2pc = Client(control_maddr, client_callback_maddr) async with p2pc.listen(): peer_id, maddrs = await p2pc.identify() listen_maddr: Multiaddr = None for maddr in maddrs: try: ip = maddr.value_for_protocol(multiaddr.protocols.P_IP4) # NOTE: Check if this `maddr` uses `tcp`. maddr.value_for_protocol(multiaddr.protocols.P_TCP) except multiaddr.exceptions.ProtocolLookupError: continue if ip == LOCALHOST_IP: listen_maddr = maddr break assert listen_maddr is not None, "no loopback maddr is found" peer_info = info_from_p2p_addr( listen_maddr.encapsulate(Multiaddr(f"/p2p/{peer_id.to_string()}"))) yield Daemon(p2pd_proc, p2pc, peer_info)
async def test_host_connect(): node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"]) assert not node_a.get_peerstore().peers() addr = node_b.get_addrs()[0] info = info_from_p2p_addr(addr) await node_a.connect(info) assert len(node_a.get_peerstore().peers()) == 1 await node_a.connect(info) # make sure we don't do double connection assert len(node_a.get_peerstore().peers()) == 1 assert node_b.get_id() in node_a.get_peerstore().peers() ma_node_b = multiaddr.Multiaddr('/p2p/%s' % node_b.get_id().pretty()) for addr in node_a.get_peerstore().addrs(node_b.get_id()): assert addr.encapsulate(ma_node_b) in node_b.get_addrs() # Success, terminate pending tasks. await cleanup()
async def connect(node1, node2): # node1 connects to node2 addr = node2.get_addrs()[0] info = info_from_p2p_addr(addr) await node1.connect(info)
async def connect(node1: IHost, node2: IHost) -> None: """Connect node1 to node2.""" addr = node2.get_addrs()[0] info = info_from_p2p_addr(addr) await node1.connect(info)
def test_invalid_addr_1(): with pytest.raises(InvalidAddrError): info_from_p2p_addr(None)
def test_invalid_addr_2(monkeypatch): random_addrs = [random.randint(0, 255) for r in range(4)] monkeypatch.setattr("multiaddr.util.split", lambda x: None) with pytest.raises(InvalidAddrError): info_from_p2p_addr(random_addrs)
def test_info_from_p2p_addr_invalid(addr): with pytest.raises(InvalidAddrError): info_from_p2p_addr(addr)
def peer_id_for_node(node): addr = node.get_addrs()[0] info = info_from_p2p_addr(addr) return info.peer_id