如何从DHT网络与对等方通信?

2024-10-16 20:42:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近正在学习Bit torrent协议,我已经让我的脚本通过DHT协议成功地获取了大量的对等地址,这依赖于文档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.

所以,我将这些“值”解码为ip/端口。在

我尝试通过TCP套接字与他们连接,超时,网络无法连接,连接被拒绝引发错误。在

我尝试通过UDP套接字发送bep_0029的握手消息,但是没有一个回复。在

有谁能告诉我,有什么不对劲吗?如何正确地连接这些同龄人?或者是正常情况下99%的人无法连接,我只是没有得到那1%的好同伴?在

谢谢你!在

这是我从trackers得到的对等点的TCP响应

{1美元^

但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)]

Tags: theforisx86xdfdecodexfbdht
1条回答
网友
1楼 · 发布于 2024-10-16 20:42:03

('127.103.184.23', 40592)

这显然是垃圾。127.0.0.0/8块不是通过internet路由的,因此任何节点都不应该有任何包含地址的数据。

我建议您使用一个流行的,已知的好的torrent,删除它的公布网址,并把它放在一个客户端与DHT支持。然后观察它的行为并将其与您自己的实现进行比较。

一般来说,由于存在缺陷或恶意的实现,因此预计会出现一些垃圾。但一般来说,这应该只是一小部分(小于99%),找到一个好的地址就足以连接到群,然后执行PEX来获得更多的对等点。 如果你有一个恶意的ISP操纵流量来插入伪造的数据,那么这个比例可能会比正常情况高很多。

相关问题 更多 >