bt种子怎么下载(聚bt官方网站)
InfoHash
&34;fmt&34;
string
if err != nil {
PieceLength int
InfoHash:
}
Length
}
这是一个文本文件,包含了要我们开始下载的全部信息:要分享的文件信息和连接到tracker服务器的信息。它使用bencode编码,就像这样:
PieceHashes [][20]byte
type bencodeInfo struct {
`bencode:&34;length&34;`
func (bto *bencodeTorrent) toTorrentFile() (TorrentFile, error) {
`bencode:&34;announce&34;`
Name
package torrentfile

return bto.toTorrentFile()
defer file.Close()
if err != nil {
int
}为了保持结构的扁平化,我们把一个结构分成了几个,并使用一个工具函数来合并他们。
PieceHashes: pieceHashes,
}
return TorrentFile{}, err
func Open(path string) (TorrentFile, error) {
string `bencode:&34;name&34;`
Info
t := TorrentFile{
}
Pieces
type bencodeTorrent struct {
string `bencode:&34;name.utf-8&34;`
Announce string
string:用一个数字前缀加冒号表示长度如字符串spam表示为4:spaminteger:用i表示开始e表示结束,如数字7表示为i7elist:用l表示开始e表示结束,如[&39;spam&39;,7]表示为l4:spami7eedictionary:用d表示开始e表示结束,如{spam:7}表示为d4:spami7ee我们可以使用现成的工具bencode editor来读写.torrent文件
d8:announce41:http://bttracker.debian.org:6969/announce7:comment35:&34;Debian CD from cdimage.debian.org&34;13:creation datei1573903810e9:httpeedsl145:http://cdimage.debian.org/cdimage/release/10.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-10.2.0-amd64-netinst.iso145:http://cdimage.debian.org/cdimage/archive/10.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-10.2.0-amd64-netinst.isoe4:infod6:lengthi351272960e4:name31:debian-10.2.0-amd64-netinst.iso12:piece lengthi262144e6:pieces26800:?????PS?^?? (binary blob of the hashes of each piece)ee上面那一堆“乱码”是用一种叫做bencode(发音:bee-encode)的编码方式生成的信息,它不是人类直接可读的,我们需要把它解码才能使用,但它能够高效的编码二进制数据,并且很容易解析。它使用的数据结构和JSON大致相同,共包含4种数据结构:string,integer,list,dictionary。
Name
&34;github.com/origin/jackpal/bencode-go&34;
if err != nil {
bencodeInfo `bencode:&34;info&34;`
Announce:
infoHash,
&34;bytes&34;
Name:
`bencode:&34;piece length&34;`
PieceLength: bto.Info.PieceLength,
return TorrentFile{}, err
import (
file, err := os.Open(path)
在上篇中,我们介绍了BT下载的原理,并通过示例搭建了一个p2p网络,了解了BT下载的原理和流程,从这篇文章将开始我们将用golang从零开始手写一个bt下载客户端。在开始动手之前,我们需要得到一个.torrent文件并解析它。
if err != nil {
pieceHashes, err := bto.Info.splitPieceHashes()
}
Length:
bto.Announce,
}
return t, nil
实现一个bencode也许是很有趣的,但不是我们现在要关注的,我们将使用一个现成的库github.com/origin/jackpal/bencode-go来完成。
bto.Info.Name,
PieceLength int
Length
在这个文件里,我们可以找出来tracker服务器地址,创建时间(用unix时间戳表示),文件名、大小以及文件的每个分片。
return TorrentFile{}, err
&34;os&34;
}
string
err = bencode.Unmarshal(file, &bto)
string `bencode:&34;pieces&34;`
后面我们将从tracker获取到peers的信息,敬请关注。
}
)
bto.Info.Length,
}
int
[20]byte
NameUtf8
return TorrentFile{}, err
&34;crypto/sha1&34;
infoHash, err := bto.Info.hash()
bto := bencodeTorrent{}
type TorrentFile struct {
Announce
- 0002
- 0004
- 0000
- 00011
- 0001