Fya.Lee
Fya.Lee

Reputation: 11

How can I communicate with peers get from DHT network?

I am learning Bit-torrent protocol recently, and I have made my script successfully get ton of peers address through DHT protocol depend on document bep_0005:

http://www.bittorrent.org/beps/bep_0005.html

If the queried node has peers for the infohash, they are returned in a key "values" as a list of strings. Each string containing "compact" format peer information for a single peer.

So, I decode those "values" to ip/port.

I tried connect with them through TCP socket, timeout, network is unreachable, connection refused errors raised.

I tried send handshake message depend on bep_0029 through UDP socket, but none of them replied.

Anyone can tell me, Is there anything wrong? What is the correct way to connect those peers? or Is that normal 99% of them are unable to connect, I just haven't get those 1% good peers?

Thank you!

This is TCP response of peers that I got from trackers

But no lucky from DHT.

# This is function I decode ip/port
def decode_peers(peers):
    ret = []
    for ipport in peers:
        try:
            ret.append((socket.inet_ntoa(ipport[:4]), unpack('>H', ipport[-2:])[0]))
        except Exception as err:
            logging.critical(err)
    return ret

# This is an bdecoded example what I received from nodes, and I passed "value" part to the upon function.
[03:53:05](DEBUG): OrderedDict([(b'ip', b'n\xbc;,\xf1\x87'), (b'r', OrderedDict([(b'id', b'\xcf\x1c@\xfb\x86?\x17\xb0\x95\xd1\xb9:\x9a\xf1\x9c\xf1v\x03\xa82'), (b'nodes', b'\x85\xd7\x80\x1b\x8a"V\xd9>\xf8<\xa2\x0e2~\x84j\x94wIA\xb2\x19&\x8b\x0c\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde0\x05\xb2\xce\xe2\x0b8j\xd1\xd3.\x85\x8cH\x8c\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde0\x06\x8d\x81\x1f\xe4\xc5\x88\xec\xad\xfc\x1a\xcf\x9e\xa6\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde0\x07 \xbcR\x1d*u\x0e.\xa8E\x12G\xdb\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde1\x80r\xfdn\x8d!\r\x17\xbfU\xb5\x87\x81t\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde1\x81\x90\xc0\x13Pw\x91\x87\xc25\x9d1\x881\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde1\x82m"\x12-\x96\xc7\x1fY}\xe4_\x9f\xff\xcf\x1c@\xfb\x86?\x18\x93$\xdf\xde1\x83\x82\xdb\xf0\x10\xeb&I\xc5}\xece\x13\xc9'), (b'token', b'\na\xb8\x15'), (b'values', [b'9Bq\xf1X\xea', b'9\x07r7\x9a\xee', b'\x7fg\xb8\x17\x9e\x90', b'\xc1d\xfa\xa5\x01\x94', b'\x04r=!C\x9a', b'Fv\x7f\xa7\x86\x9e', b'\x89s\x7f\xf3\xc8\xa4'])])), (b't', b'\xc3\x8b\xc2\x9d'), (b'v', b'LT\x00\x11'), (b'y', b'r')])

# This is result I got from decode function, there is no difference when I compare to what Wireshark decode under DHT protocol.
[04:03:56](DEBUG): [('57.66.113.241', 22762), ('57.7.114.55', 39662), ('127.103.184.23', 40592), ('193.100.250.165', 404), ('4.114.61.33', 17306), ('70.118.127.167', 34462), ('137.115.127.243', 51364)]

Upvotes: 1

Views: 1262

Answers (1)

the8472
the8472

Reputation: 43125

('127.103.184.23', 40592)

This is clearly junk. The 127.0.0.0/8 block is not routed over the internet so no node should have any data containing an address in it.

I suggest you take a popular, known-good torrent, remove its announce URLs and put it into a client with DHT support. Then observe its behavior and compare it to your own implementation.

Generally speaking some fraction of junk is expected since there are buggy or malicious implementations out there. But generally that should only be a small fraction (smaller than 99%) and finding a single good address should be enough to connect to the swarm and then perform PEX to get more peers. If you have a malicious ISP that manipulates traffic to insert bogus data then that fraction may be much higher than normal.

Upvotes: 1

Related Questions