forked from amy/Bittorrent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtorrent.go
77 lines (62 loc) · 1.87 KB
/
torrent.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
package main
import (
"crypto/sha1"
"io"
"log"
"os"
"github.com/zeebo/bencode"
)
// Torrent is the struct containing the decoded torrent meta file
type Torrent struct {
Info bencode.RawMessage `bencode:"info"`
Announce string `bencode:"announce,omitempty"`
AnnounceList [][]string `bencode:"announce-list,omitempty"`
CreationDate int64 `bencode:"creation date,omitempty"`
Comment string `bencode:"comment,omitempty"`
CreatedBy string `bencode:"created by,omitempty"`
URLList string `bencode:"url-list,omitempty"`
}
// InfoDict is the info dictionary
type InfoDict struct {
Name string `bencode:"name"`
Length int `bencode:"length"`
PieceLength int `bencode:"piece length"`
Pieces string `bencode:"pieces"`
}
//InfoFile is currently unused
type InfoFile struct {
Name string `bencode:"name"`
Length int `bencode:"length"`
Md5Sum string `bencode:"md5sum"`
Path []string `bencode:"path"`
}
// NewTorrent creates a new torrent struct from the file at torrentPath
func NewTorrent(torrentPath string) (torrent *Torrent, err error) {
var file *os.File
file, err = os.Open(torrentPath)
if err != nil {
return
}
err = bencode.NewDecoder(file).Decode(&torrent)
if err != nil {
panic(err)
}
return
}
//InfoHash returns the hash of the bencoded info dictionary
func (t *Torrent) InfoHash() []byte {
// peer_id I need & self generate
// left = length of file downloading (dict)
//
hash := sha1.New()
io.WriteString(hash, string(t.Info))
infoHash := hash.Sum(nil)
return infoHash
}
//InfoDict returns the decoded info dictionary in the torrent info
func (t *Torrent) InfoDict() (id InfoDict) {
if err := bencode.DecodeBytes(t.Info, &id); err != nil {
log.Fatal("Unable to parse the Info Dictionary in the torrent file", err)
}
return id
}