解析来自 Tracker 服务器的 Annouce 响应中的 Peers IP 地址(bitTorrent)

Parsing Peers IP Address in Annouce Response From Tracker Server (bitTorrent)

所以请求如下:

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

导致提供通告文件。对该文件进行编码后,您将获得:

{'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'

如果这是网络顺序(little endian)?

来自here我读到:

Note if you get the peers in the binary model that the last two bytes together encode the port number (i.e. ‘\x1a\xe1′ = 26 * 256 + 225 = 6881).

所以可能'\xd8\x05'组成端口:216 * 256 + 5 = 55301 或者可能不是。

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

用谷歌搜索了一段时间,没有找到太多,所以任何帮助将不胜感激。

你应该阅读 bittorrent specification and compact announce extension

network order (little endian)?

"Network order" 无进一步限定 generally is big endian.

Can someone explain to me how to parse these hex numbers into ip:port addresses?

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

所以根据 specification

peers: (binary model) Instead of using the dictionary model described above, the peers value may be a string consisting of multiples of 6 bytes. First 4 bytes are the IP address and last 2 bytes are the port number. All in network (big endian) notation.

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

解析bencoded announce文件后,提取密钥'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

显然,如果有更多对等 ip 需要读取,您可以遍历它。