bmuller / kademlia

A DHT in Python using asyncio
http://kademlia.readthedocs.org
MIT License
830 stars 210 forks source link

Connect 2 peers behind the NAT #44

Closed personstays closed 6 years ago

personstays commented 6 years ago

Hello,

Thanks for this good project. I have been trying to use this project to test the peer to peer discovery. I have the following setup. (2 nodes over NAT and one Non-NATed bootstrap node)

import logging
import asyncio

from kademlia.network import Server

handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log = logging.getLogger('kademlia')
log.addHandler(handler)
log.setLevel(logging.DEBUG)

server = Server()
server.listen(8468)

loop = asyncio.get_event_loop()
loop.set_debug(True)

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    server.stop()
    loop.close()

from kademlia.network import Server

handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) log = logging.getLogger('kademlia') log.addHandler(handler) log.setLevel(logging.DEBUG)

server = Server() server.listen(8468)

loop = asyncio.get_event_loop() loop.set_debug(True) loop.run_until_complete(server.bootstrap([("", 8468)]))

try: loop.run_forever() except KeyboardInterrupt: pass finally: server.stop() loop.close()



- Computer B behind NAT (at home) with the same source code as Computer A.

Those 2 nodes could connect the bootstrap node. My problem is that **I can't make those 2 nodes to connect to each other through bootstrap node**. 

In Computer B - The response was **kademlia.protocol - WARNING - no response from <Computer A's NAT's external IP>:46357, removing from router**

In Computer A - Vice versa

Since I'm new to "P2P Network" (Kademlia), perhaps I missed or did something wrong. I've tried to search for the answers on the internet for many days, but no luck. There aren't many documents about this problem either.

Could you help guide me how to connect those 2 nodes over NAT? Or **source code** would be highly appreciated.

Thanks!
bmuller commented 6 years ago

If both machines are behind a NAT, then UDP hole punching won't work and the two machines won't be able to talk to each other. Eventually, we could come up with a way of coordinating mutual hole punching using a coordination node, but that would require a lot of work.