登陆注册
18100

bt种子怎么下载(聚bt官方网站)

大财经2023-03-25 13:38:382

后面我们将从tracker获取到peers的信息,敬请关注。

Name

bto.Info.Length,

Announce

Name:

infoHash, err := bto.Info.hash()

Name

&34;github.com/origin/jackpal/bencode-go&34;

实现一个bencode也许是很有趣的,但不是我们现在要关注的,我们将使用一个现成的库github.com/origin/jackpal/bencode-go来完成。

type TorrentFile struct {

PieceHashes: pieceHashes,

Pieces

InfoHash:

bto := bencodeTorrent{}

Info

bto.Announce,

}

}为了保持结构的扁平化,我们把一个结构分成了几个,并使用一个工具函数来合并他们。

}

PieceHashes [][20]byte

&34;crypto/sha1&34;

}

Length

Announce:

)

t := TorrentFile{

InfoHash

func (bto *bencodeTorrent) toTorrentFile() (TorrentFile, error) {

}

file, err := os.Open(path)

bencodeInfo `bencode:&34;info&34;`

`bencode:&34;piece length&34;`

PieceLength: bto.Info.PieceLength,

if err != nil {

type bencodeInfo struct {

string

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。

import (

infoHash,

&34;bytes&34;

string `bencode:&34;name.utf-8&34;`

err = bencode.Unmarshal(file, &bto)

bto.Info.Name,

在上篇中,我们介绍了BT下载的原理,并通过示例搭建了一个p2p网络,了解了BT下载的原理和流程,从这篇文章将开始我们将用golang从零开始手写一个bt下载客户端。在开始动手之前,我们需要得到一个.torrent文件并解析它。

PieceLength int

return TorrentFile{}, err

Length:

return bto.toTorrentFile()

int

type bencodeTorrent struct {

}

return TorrentFile{}, err

string `bencode:&34;pieces&34;`

}

func Open(path string) (TorrentFile, error) {

Announce string

if err != nil {

defer file.Close()

package torrentfile

if err != nil {

在这个文件里,我们可以找出来tracker服务器地址,创建时间(用unix时间戳表示),文件名、大小以及文件的每个分片。

return TorrentFile{}, err

`bencode:&34;announce&34;`

}

PieceLength int

if err != nil {

`bencode:&34;length&34;`

bt种子怎么下载 聚bt官方网站
bencode editor

&34;fmt&34;

return t, nil

这是一个文本文件,包含了要我们开始下载的全部信息:要分享的文件信息和连接到tracker服务器的信息。它使用bencode编码,就像这样:

}

Length

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文件

NameUtf8

int

}

return TorrentFile{}, err

&34;os&34;

pieceHashes, err := bto.Info.splitPieceHashes()

string

string `bencode:&34;name&34;`

[20]byte

0002
评论列表
共(0)条
热点
关注
推荐