原文連接:http://luoguochun.cn/2014/09/17/bt-file-structure/
原文做者:buf1024node
估計 80% 以上接觸互聯網的人都知道 bt 是什麼東西,任何一個用 bt 下載的人都知道這樣一個概念,種子。bt 種子就是記錄了 p2p 對等網絡中 tracker, nodes, files 等信息,也就是說,這個種子告訴你,你要下載什麼,到哪裏下載。bt 種子文件有本身的文件格式,下面簡單看看 bt 種子文件的結構。python
在開始以前,咱們先介紹 bencode 這種編碼格式,由於 bt 種子文件,包括之後的 DHT 網絡中,都是用這種編碼的。網上有不少介紹,這裏簡單再重複一遍。bencode 有 4 種數據類型: string, integer, list 和 dictionary。服務器
string網絡
字符是以這種方式編碼的: <字符串長度>:<字符串>。
如 hell: 4:hell
編碼
integerurl
整數是一這種方式編碼的: i<整數>e。
如 1999: i1999e
code
list排序
列表是一這種方式編碼的: l[數據1][數據2][數據3][…]e。
如列表 [hello, world, 101]:l5:hello5:worldi101ee
utf-8
dictionaryci
字典是一這種方式編碼的: d[key1][value1][key2][value2][…]e
,其中 key 必須是 string 並且按照字母順序排序。
如字典 {aa:100, bb:bb, cc:200}: d2:aai100e2:bb2:bb2:cci200ee
不少語言都有 bencode 的實現,Python 版本可在 pypi 裏面找到: bencode。
bt 種子文件是使用 bencode 編碼的,整個文件就 dictionary,包含如下鍵。
info, dictinary, 必選, 表示該bt種子文件的文件信息。
文件信息包括文件的公共部分
piece length, integer, 必選, 每一數據塊的長度
pieces, string, 必選, 全部數據塊的 SHA1 校驗值
publisher, string, 可選, 發佈者
publisher.utf-8, string, 可選, 發佈者的 UTF-8 編碼
publisher-url, string, 可選, 發佈者的 URL
publisher-url.utf-8, string, 可選, 發佈者的 URL 的 UTF-8 編碼
若是 bt 種子包含的是單個文件,包含如下內容
name, string, 必選, 推薦的文件名稱
name.utf-8, string, 可選, 推薦的文件名稱的 UTF-8 編碼
length, int, 必選, 文件的長度單位是字節
若是是多文件,則包含如下部分:
name, string, 必選, 推薦的文件夾名稱
name.utf-8, string, 可選, 推薦的文件名稱的 UTF-8 編碼
files, list, 必選, 文件列表,每一個文件列表下面是包括每個文件的信息,文件信息是個字典。
文件字典
length, int, 必選, 文件的長度單位是字節
path, string, 必選, 文件名稱,包含文件夾在內
path.utf-8, string, 必選, 文件名稱 UTF-8 表示,包含文件夾在內
filehash,string, 可選, 文件 hash。
ed2k, string, 可選, ed2k 信息。
announce, string, 必選, tracker 服務器的地址
announce-list, list, 可選, 可選的 tracker 服務器地址
creation date, interger, 必選, 文件建立時間
comment, string, 可選, bt 文件註釋
created by, string, 可選, 文件建立者。
上面列舉的可能不是很完整的,可是大致上主要的字段沒有重大的錯誤。