【问题标题】:Parsing Peers IP Address in Annouce Response From Tracker Server (bitTorrent)在来自 Tracker Server (bitTorrent) 的 Annouce 响应中解析 Peers IP 地址
【发布时间】:2015-07-20 00:44:51
【问题描述】:

所以下面的请求:

torrent.ubuntu.com:6969/announce?info_hash=%02%21%CA%F9j%A3%CB%94%F0%F5%8DE%8Ex%B0%FC4J%D8%BF&peer_id=ABCDEFGHIJKLMNOPQRST&port=6881&uploaded=0&downloaded=0&left=3353370624&compact=0

导致提供一个 Announce 文件。在该文件被弯曲编码后,您会得到:

{'peers': '\xb9\x15\xd9\x08\xd8\x05[\xbd_\x15\x1b!', 'interval': 1800, 'complete': 5, 'incomplete': 1}

我很纠结

'\xb9\x15\xd9\x08\xd8\x05[\xbd_\x15\x1b!'

使用 compact=1 你会得到:

'\xbd_\x15\x1b\n\xb9\x15\xd9\x08\xd8\x05'

如果这是网络顺序(小端)?

来自here 我读到:

请注意,如果您在二进制模型中获得对等体,最后两个字节一起编码端口号(即'\x1a\xe1' = 26 * 256 + 225 = 6881)。

所以也许 '\xd8\x05' 构成端口:216 * 256 + 5 = 55301 或者不是。

有人可以向我解释如何将这些十六进制数字解析为 ip:port 地址吗?

在谷歌上搜索了一段时间,没有发现太多,所以任何帮助都将不胜感激。

【问题讨论】:

    标签: bittorrent tracker


    【解决方案1】:

    所以根据specification

    peers:(二进制模型)不使用上述字典模型,peers 值可以是由 6 个字节的倍数组成的字符串。前 4 个字节是 IP 地址,后 2 个字节是端口号。全部采用网络(大端)表示法。

    这是紧凑标志设置为 1(真)的时候,我只关心这个 atm,因为它看起来很标准。

    解析完编码后的announce文件后,取出key 'peers'会得到6字节字符串的倍数。

    这个字符串是二进制数据并且是大端的,所以我们可以解析第一个地址(在 Python 中):

    decoded = bdecode(announce) # decode the bencoded announce
    binary_ip = decoded['peers']
    print len(binary_ip) # this will be a multiple of 6 (ie, 12 = 2 ip:port)
    offset = 0
    ip1 = struct.unpack_from("!i", binary_ip, offset)[0] # ! = network order(big endian); i = int
    first_ip = socket.inet_ntoa(struct.pack("!i", ip1)
    offset +=4 # save where the first ip ends and the port begins
    port1 = struct.unpack_from("!H", binary_ip, offset)[0] # H = unsigned short
    offset += 2
    

    如果有更多的对等 ips 要读取,显然你可以遍历它。

    【讨论】:

      【解决方案2】:

      您应该阅读bittorrent specificationcompact announce extension

      网络顺序(小端)?

      “网络订单”无需进一步限定generally is big endian

      有人可以向我解释如何将这些十六进制数字解析为 ip:port 地址吗?

      它们不是十六进制数字。经编码的数据是没有任何特定字符集的原始二进制文件。无论您使用什么来显示它都会创建十六进制输出。

      【讨论】:

      • 感谢您为我指明正确的方向。想通了几乎要在 Python 中写下我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2010-09-27
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多