python - 我如何与来自 DHT 网络的同行进行通信?

标签 python bittorrent dht

我最近正在学习 Bit-torrent 协议(protocol),并且我已经使我的脚本成功通过 DHT 协议(protocol)获取大量对等地址,具体取决于文档 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%的好同伴?

谢谢!

这是我从跟踪器获得的对等点的 TCP 响应

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

最佳答案

('127.103.184.23', 40592)

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

我建议您采用流行的、已知良好的 torrent,删除其公告 URL,并将其放入支持 DHT 的客户端中。然后观察它的行为并将其与您自己的实现进行比较。

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

关于python - 我如何与来自 DHT 网络的同行进行通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181847/

相关文章:

python - 我以前从未见过的语法错误

python - Z 标签未显示在 3d matplotlib 散点图中

python - 使用 asyncio 与其他对等方连接时如何处理 ConnectionRefusedError

protocols - 'distributed tracker' 概念在 Bittorrent DHT 中如何运作?

PHP DHT 信息哈希列表?

python - 在 python 中设置 blob 检测参数

python - 将一个字典的键替换为另一个字典的值以创建一个新字典

objective-c - 将 NSData 字节转换为 NSString?

debugging - Torrent 调试工具

blockchain - 以太坊协议(protocol)如何与 geth 协同工作