1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Refactor torrent

This commit is contained in:
Jesse Li
2019-12-22 13:22:03 -05:00
parent 11314c13dc
commit 4841bcda29
2 changed files with 60 additions and 16 deletions

View File

@@ -12,18 +12,26 @@ import (
const port = 6881
// Info info
type Info struct {
// Torrent encodes the metadata from a .torrent file
type Torrent struct {
Announce string
InfoHash []byte
PieceHashes [][]byte
PieceLength int
Length int
Name string
}
type bencodeInfo struct {
Pieces string `bencode:"pieces"`
PieceLength int `bencode:"piece length"`
Length int `bencode:"length"`
Name string `bencode:"name"`
}
// Torrent torrent
type Torrent struct {
Announce string `bencode:"announce"`
Info Info `bencode:"info"`
type bencodeTorrent struct {
Announce string `bencode:"announce"`
Info bencodeInfo `bencode:"info"`
}
// Download downloads a torrent
@@ -46,15 +54,19 @@ func (to *Torrent) Download() error {
// Open parses a torrent file
func Open(r io.Reader) (*Torrent, error) {
to := Torrent{}
err := bencode.Unmarshal(r, &to)
bto := bencodeTorrent{}
err := bencode.Unmarshal(r, &bto)
if err != nil {
return nil, err
}
return &to, nil
to, err := bto.toTorrent()
if err != nil {
return nil, err
}
return to, nil
}
func (i *Info) hash() ([]byte, error) {
func (i *bencodeInfo) hash() ([]byte, error) {
var buf bytes.Buffer
err := bencode.Marshal(&buf, *i)
if err != nil {
@@ -63,3 +75,39 @@ func (i *Info) hash() ([]byte, error) {
h := sha1.Sum(buf.Bytes())
return h[:], nil
}
func (i *bencodeInfo) splitPieceHashes() ([][]byte, error) {
hashLen := 20 // Length of SHA-1 hash
buf := []byte(i.Pieces)
if len(buf)%hashLen != 0 {
err := fmt.Errorf("Received malformed pieces of length %d", len(buf))
return nil, err
}
numHashes := len(buf) / hashLen
hashes := make([][]byte, numHashes)
for i := 0; i < numHashes; i++ {
hashes[i] = buf[i*hashLen : (i+1)*hashLen]
}
return hashes, nil
}
func (bto *bencodeTorrent) toTorrent() (*Torrent, error) {
infoHash, err := bto.Info.hash()
if err != nil {
return nil, err
}
pieceHashes, err := bto.Info.splitPieceHashes()
if err != nil {
return nil, err
}
to := Torrent{
Announce: bto.Announce,
InfoHash: infoHash,
PieceHashes: pieceHashes,
PieceLength: bto.Info.PieceLength,
Length: bto.Info.Length,
Name: bto.Info.Name,
}
return &to, nil
}

View File

@@ -51,18 +51,14 @@ func (tr *Tracker) buildTrackerURL() (string, error) {
if err != nil {
return "", err
}
infoHash, err := tr.Torrent.Info.hash()
if err != nil {
return "", err
}
params := url.Values{
"info_hash": []string{string(infoHash)},
"info_hash": []string{string(tr.Torrent.InfoHash)},
"peer_id": []string{string(tr.PeerID)},
"port": []string{strconv.Itoa(int(tr.Port))},
"uploaded": []string{"0"},
"downloaded": []string{"0"},
"compact": []string{"1"},
"left": []string{strconv.Itoa(tr.Torrent.Info.Length)},
"left": []string{strconv.Itoa(tr.Torrent.Length)},
}
base.RawQuery = params.Encode()
return base.String(), nil