【问题标题】:Bittorrent Extension ProtocolBittorrent 扩展协议
【发布时间】:2017-04-20 19:51:33
【问题描述】:

我正在尝试用 java 编写自己的 bittorrent 客户端,这是一个让我刷新 java 技能的工作项目。我一直被困在某个开发点上,尽管有一些论坛可以进行 bittorrent 开发,但它们似乎并不活跃。

到目前为止,我可以做到以下几点:

  1. 从 Tracker 站点获取对等点列表
  2. 连接到对等体
  3. 发送“握手”消息
  4. 接收回“握手”消息
  5. 发送“有兴趣”消息。
  6. 收到回复“Unchoke”消息。

我遇到的问题是,如果我在发送“感兴趣”消息之前尝试向对等方发送扩展数据 (http://www.bittorrent.org/beps/bep_0010.html)。协议规定您必须在收到握手后立即发送扩展数据。 如果我向对等方发送扩展数据,对等方会立即关闭连接,即在 Wireshark 中我看到一个 FIN,ACK。 我已经使用 Wireshark 和 uTorrent 来查看正在发送哪些扩展数据,并且我试图复制该数据,但无济于事。我已经尝试了扩展数据参数的所有不同组合,但无论我做什么,我都无法超越这一点。

这是一个有效的扩展数据消息示例,从 Wireshark 复制。

d1:ei0e4:ipv44:czH12:complete_agoi-1e1:md11:upload_onlyi3e11:lt_donthavei7e12:ut_holepunchi4e11:ut_metadatai2e6:ut_pexi1e10:ut_commenti6ee1:pi43276e4:reqqi255e1:v13:Torrent 3.52 3.52

这是来自我的客户端的扩展数据,它不起作用。顺便说一句,我已检查以确保 ip 变量正确(它们以 4 个字节发送)。

d1:ei0e4:ipv44:czH12:complete_agoi-1e1:md11:upload_onlyi3e11:lt_donthavei7e12:ut_holepunchi4e11:ut_metadatai2e6:ut_pexi1e10:ut_commenti6ee1:pi51361e4:reqqi255e1:v15:MyTorrent:40 eTorrent v861:你的eqqi255e1:ip24:40 /strong>

此外,一旦我收到“Unchoke”消息,我就能成功检索碎片。

任何信息将不胜感激。

【问题讨论】:

  • 您可以将数据发布为 hex + ascii 视图。 bencoding 是二进制的,像字符串一样发布它可能会损坏它。您是否使用经过测试的编码实现来创建扩展握手?为什么要复制远程对等方的握手,而不是根据规范仅使用必填字段来创建它?
  • 我没有保存wireshark捕获,我可以做更多的测试并复制和粘贴十六进制和ASCII视图,但我可以向你保证我发送给你的数据中的所有信息都是有效的。至于扩展协议的必填字段,我认为没有,根据我在规范中阅读的内容,它们都是可选的。正如我所提到的,我尝试了不同的参数组合,例如,只发送我看到的其他应用程序(如 BitComet)发送的参数,但没有任何效果。我知道我正在发送错误数据(我不认为它已损坏),我只是不知道如何弄清楚它是什么。
  • wireshark 捕获仍然是最容易诊断的
  • 我看不到如何将文件附加到我的问题中,显示wireshark 捕获的最佳方式是什么?

标签: java bittorrent


【解决方案1】:

从 Ray_Write 发布的链接中捕获的 TCP 流:

00000000  13 42 69 74 54 6f 72 72  65 6e 74 20 70 72 6f 74 .BitTorr ent prot
00000010  6f 63 6f 6c 00 00 00 00  00 10 00 00 14 82 9a a1 ocol.... ........
00000020  b5 7d 5a d7 3c b5 c9 d6  60 83 c8 99 8f f6 aa cb .}Z.<... `.......
00000030  2d 4d 59 31 32 33 34 2d  31 32 33 34 35 36 37 38 -MY1234- 12345678
00000040  39 30 31 32                                      9012
    00000000  13 42 69 74 54 6f 72 72  65 6e 74 20 70 72 6f 74 .BitTorr ent prot
    00000010  6f 63 6f 6c 00 00 00 00  00 10 00 05 14 82 9a a1 ocol.... ........
    00000020  b5 7d 5a d7 3c b5 c9 d6  60 83 c8 99 8f f6 aa cb .}Z.<... `.......
    00000030  2d 55 54 33 33 30 42 2d  06 77 26 8b 01 b2 4d 0e -UT330B- .w&...M.
    00000040  69 ab d2 3b 00 00 00 eb  14 00 64 31 3a 65 69 30 i..;.... ..d1:ei0
    00000050  65 34 3a 69 70 76 34 34  3a b2 21 7b 3d 31 32 3a e4:ipv44 :.!{=12:
    00000060  63                                               c
00000044  64 31 3a 65 69 30 65 34  3a 69 70 76 34 34 3a 79 d1:ei0e4 :ipv44:y
00000054  98 b9 fa 31 32 3a 63 6f  6d 70 6c 65 74 65 5f 61 ...12:co mplete_a
00000064  67 6f 69 2d 31 65 31 3a  6d 64 31 31 3a 75 70 6c goi-1e1: md11:upl
00000074  6f 61 64 5f 6f 6e 6c 79  69 33 65 31 31 3a 6c 74 oad_only i3e11:lt
00000084  5f 64 6f 6e 74 68 61 76  65 69 37 65 31 32 3a 75 _donthav ei7e12:u
00000094  74 5f 68 6f 6c 65 70 75  6e 63 68 69 34 65 31 31 t_holepu nchi4e11
000000A4  3a 75 74 5f 6d 65 74 61  64 61 74 61 69 32 65 36 :ut_meta datai2e6
000000B4  3a 75 74 5f 70 65 78 69  31 65 31 30 3a 75 74 5f :ut_pexi 1e10:ut_
000000C4  63 6f 6d 6d 65 6e 74 69  36 65 65 31 3a 70 69 35 commenti 6ee1:pi5
000000D4  31 39 30 32 65 34 3a 72  65 71 71 69 32 35 35 65 1902e4:r eqqi255e
000000E4  31 3a 76 31 33 3a 76 31  35 3a 4d 79 54 6f 72 72 1:v13:v1 5:MyTorr
000000F4  65 6e 74 20 76 30 2e 31  35 32 3a 79 70 69 32 30 ent v0.1 52:ypi20
00000104  37 30 34 65 36 3a 79 6f  75 72 69 70 34 3a b2 21 704e6:yo urip4:.!
00000114  7b 3d 65                                         {=e
    00000061  6f 6d 70 6c 65 74 65 5f  61 67 6f 69 31 32 65 31 omplete_ agoi12e1
    00000071  3a 6d 64 31 31 3a 75 70  6c 6f 61 64 5f 6f 6e 6c :md11:up load_onl
    00000081  79 69 33 65 31 32 3a 75  74 5f 68 6f 6c 65 70 75 yi3e12:u t_holepu
    00000091  6e 63 68 69 34 65 31 31  3a 75 74 5f 6d 65 74 61 nchi4e11 :ut_meta
    000000A1  64 61 74 61 69 32 65 36  3a 75 74 5f 70 65 78 69 datai2e6 :ut_pexi
    000000B1  31 65 31 32 3a 75 74 5f  72 65 63 6f 6d 6d 65 6e 1e12:ut_ recommen
    000000C1  64 69 35 65 31 30 3a 75  74 5f 63 6f 6d 6d 65 6e di5e10:u t_commen
    000000D1  74 69 36 65 31 31 3a 6c  74 5f 64 6f 6e 74 68 61 ti6e11:l t_dontha
    000000E1  76 65 69 37 65 65 31 33  3a 6d 65 74 61 64 61 74 vei7ee13 :metadat
    000000F1  61 5f 73 69 7a 65 69 32  36 37 38 65 31 3a 70 69 a_sizei2 678e1:pi
    00000101  32 30 37 30 34 65 34 3a  72 65 71 71 69 32 35 35 20704e4: reqqi255
    00000111  65 31 3a 76 31 32 3a 75  54 6f 72 72 65 6e 74 20 e1:v12:u Torrent 
    00000121  33 2e 33 36 3a 79 6f 75  72 69 70 34 3a 79 98 b9 3.36:you rip4:y..
    00000131  fa 65 00 00 00 11 05 ff  ff fa ee f7 af ff ef bf .e...... ........
    00000141  de ee f7 ec 5e 4f a0 00  00 00 05 04 00 00 00 29 ....^O.. .......)
    00000151  00 00 00 05 04 00 00 00  53 00 00 00 05 04 00 00 ........ S.......
    00000161  00 6a 00 00 00 05 04 00  00 00 17 00 00 00 05 04 .j...... ........
    00000171  00 00 00 2b 00 00 00 05  04 00 00 00 1f 00 00 00 ...+.... ........
    00000181  05 04 00 00 00 73 00 00  00 05 04 00 00 00 24 00 .....s.. ......$.
    00000191  00 00 05 04 00 00 00 41  00 00 00 05 04 00 00 00 .......A ........
    000001A1  1b 00 00 00 05 04 00 00  00 4f 00 00 00 05 04 00 ........ .O......
    000001B1  00 00 66 00 00 00 05 04  00 00 00 5c 00 00 00 05 ..f..... ...\....
    000001C1  04 00 00 00 57 00 00 00  05 04 00 00 00 79 00 00 ....W... .....y..
    000001D1  00 05 04 00 00 00 4a 00  00 00 05 04 00 00 00 3b ......J. .......;
    000001E1  00 00 00 05 04 00 00 00  72 00 00 00 05 04 00 00 ........ r.......
    000001F1  00 68 00 00 00 05 04 00  00 00 15 00 00 00 05 04 .h...... ........
    00000201  00 00 00 63 00 00 00 05  04 00 00 00 6f 00 00 00 ...c.... ....o...
    00000211  05 04 00 00 00 70 00 00  00 05 04 00 00 00 67    .....p.. ......g
00000117  00 00 00 01 02  

主要问题在这一行:

...
00000044  64 31 3a 65 69 30 65 34  3a 69 70 76 34 34 3a 79 d1:ei0e4 :ipv44:y
...

扩展协议握手中缺少 4 字节长度前缀、1 字节 bittorrent 消息 ID 和 1 字节扩展消息 ID。应该是:

...
00000044  00 00 00 d5 14 00 64 31  3a 65 69 30 65 34 3a 69 ......d1:ei0e4 :i
...

这里还有一个问题:

...
000000E4  31 3a 76 31 33 3a 76 31  35 3a 4d 79 54 6f 72 72 1:v13:v1 5:MyTorr
000000F4  65 6e 74 20 76 30 2e 31  35 32 3a 79 70 69 32 30 ent v0.1 52:ypi20
...

“v”键的值有两个字符串长度索引。
它是:1:v13:v15:MyTorrent v0.15
应该是:1:v15:MyTorrent v0.15

还有一个小问题是,在两个客户端扩展协议握手中,根和“m”字典未排序。它们应该被排序,但大多数客户接受未排序的字典,因为这个问题很常见。

我还怀疑 µTorrent 客户端是假的,因为真正的客户端在版本字符串中使用“µ”,而不是“u”。

【讨论】:

  • 哇,感谢您发现这一点,我知道消息的格式,但由于某种原因,我被困在消息的内容上,再次感谢。
  • @Encombe,你好。我可以请你查一下吗。发送extended message 的顺序是否正确 -> (1) send standard bittorrent handshake (2) send extended msg handshake upon receiving bittorrent handshake response (3) send extended msg with payload to get the magnet link's metadata?我一直在尝试接收磁力链接的元数据,但无济于事。另外,extended msg payload 看起来像谁?我浏览了文档,但不完全理解..
【解决方案2】:

根据您的 wireshark 捕获,您只发送扩展握手负载,而不是必要的扩展协议或 bittorrent 消息头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多