-
Notifications
You must be signed in to change notification settings - Fork 179
/
torrentfile.go
138 lines (122 loc) · 2.79 KB
/
torrentfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package torrentfile
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"fmt"
"os"
"github.com/jackpal/bencode-go"
"github.com/veggiedefender/torrent-client/p2p"
)
// Port to listen on
const Port uint16 = 6881
// TorrentFile encodes the metadata from a .torrent file
type TorrentFile struct {
Announce string
InfoHash [20]byte
PieceHashes [][20]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"`
}
type bencodeTorrent struct {
Announce string `bencode:"announce"`
Info bencodeInfo `bencode:"info"`
}
// DownloadToFile downloads a torrent and writes it to a file
func (t *TorrentFile) DownloadToFile(path string) error {
var peerID [20]byte
_, err := rand.Read(peerID[:])
if err != nil {
return err
}
peers, err := t.requestPeers(peerID, Port)
if err != nil {
return err
}
torrent := p2p.Torrent{
Peers: peers,
PeerID: peerID,
InfoHash: t.InfoHash,
PieceHashes: t.PieceHashes,
PieceLength: t.PieceLength,
Length: t.Length,
Name: t.Name,
}
buf, err := torrent.Download()
if err != nil {
return err
}
outFile, err := os.Create(path)
if err != nil {
return err
}
defer outFile.Close()
_, err = outFile.Write(buf)
if err != nil {
return err
}
return nil
}
// Open parses a torrent file
func Open(path string) (TorrentFile, error) {
file, err := os.Open(path)
if err != nil {
return TorrentFile{}, err
}
defer file.Close()
bto := bencodeTorrent{}
err = bencode.Unmarshal(file, &bto)
if err != nil {
return TorrentFile{}, err
}
return bto.toTorrentFile()
}
func (i *bencodeInfo) hash() ([20]byte, error) {
var buf bytes.Buffer
err := bencode.Marshal(&buf, *i)
if err != nil {
return [20]byte{}, err
}
h := sha1.Sum(buf.Bytes())
return h, nil
}
func (i *bencodeInfo) splitPieceHashes() ([][20]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([][20]byte, numHashes)
for i := 0; i < numHashes; i++ {
copy(hashes[i][:], buf[i*hashLen:(i+1)*hashLen])
}
return hashes, nil
}
func (bto *bencodeTorrent) toTorrentFile() (TorrentFile, error) {
infoHash, err := bto.Info.hash()
if err != nil {
return TorrentFile{}, err
}
pieceHashes, err := bto.Info.splitPieceHashes()
if err != nil {
return TorrentFile{}, err
}
t := TorrentFile{
Announce: bto.Announce,
InfoHash: infoHash,
PieceHashes: pieceHashes,
PieceLength: bto.Info.PieceLength,
Length: bto.Info.Length,
Name: bto.Info.Name,
}
return t, nil
}