-
Notifications
You must be signed in to change notification settings - Fork 7
/
metainfo.go
192 lines (158 loc) · 4.07 KB
/
metainfo.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"bytes"
"crypto/sha1"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/nictuku/dht"
"github.com/zeebo/bencode"
)
type FileDict struct {
Length int64 `bencode:"length"`
Path []string `bencode:"path"`
Md5sum string `bencode:"md5sum,omitempty"`
}
type InfoDict struct {
PieceLength int64 `bencode:"piece length"`
Pieces string `bencode:"pieces"`
Private int64 `bencode:"private"`
Name string `bencode:"name"`
// Single File Mode
Length int64 `bencode:"length,omitempty"`
Md5sum string `bencode:"md5sum,omitempty"`
// Multiple File mode
Files []*FileDict `bencode:"files,omitempty"`
}
type MetaInfo struct {
Info *InfoDict `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"`
Encoding string `bencode:"encoding,omitempty"`
// These are not used for bencoding, only for helping
InfoHash string `bencode:"-"`
rawInfo []byte `bencode:"-"`
}
func NewMetaInfo(torrent string) (m *MetaInfo, err error) {
if strings.HasPrefix(torrent, "http:") {
return NewMetaInfoFromHTTP(torrent)
} else if strings.HasPrefix(torrent, "magnet:") {
return NewMetaInfoFromMagnet(torrent)
} else {
m, err := NewMetaInfoFromContent([]byte(torrent))
if err == nil {
return m, nil
}
return NewMetaInfoFromFile(torrent)
}
}
func NewMetaInfoFromHTTP(torrent string) (m *MetaInfo, err error) {
r, err := proxyHttpGet(torrent)
if err != nil {
return nil, err
}
defer r.Body.Close()
content, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
return NewMetaInfoFromContent(content)
}
func NewMetaInfoFromFile(torrent string) (m *MetaInfo, err error) {
content, err := ioutil.ReadFile(torrent)
if err != nil {
return
}
return NewMetaInfoFromContent(content)
}
func NewMetaInfoFromContent(content []byte) (m *MetaInfo, err error) {
var m1 MetaInfo
err1 := bencode.DecodeString(string(content), &m1)
if err1 != nil {
err = errors.New("Couldn't parse torrent file: " + err1.Error())
return
}
hash := sha1.New()
err1 = bencode.NewEncoder(hash).Encode(m1.Info)
if err1 != nil {
return
}
m1.InfoHash = string(hash.Sum(nil))
return &m1, nil
}
func NewMetaInfoFromMagnet(torrent string) (m *MetaInfo, err error) {
magnet, err := parseMagnet(torrent)
if err != nil {
log.Println("Couldn't parse magnet: ", err)
return
}
ih, err := dht.DecodeInfoHash(magnet.InfoHashes[0])
if err != nil {
return
}
m = &MetaInfo{InfoHash: string(ih)}
return
}
func (m *MetaInfo) saveToDisk(dir string) (err error) {
ihhex := fmt.Sprintf("%x", m.InfoHash)
f, err := os.Create(filepath.Join(dir, ihhex))
if err != nil {
log.Println("Error when opening file for creation: ", err)
return
}
defer f.Close()
return bencode.NewEncoder(f).Encode(m)
}
// Returns the size of the representation of this metainfo as a bencoded
// dictionary
func (m *MetaInfo) Size() (sz int) {
var buf bytes.Buffer
err := bencode.NewEncoder(&buf).Encode(m)
if err != nil {
log.Fatal("Couldn't bencode this metainfo: ", err)
}
return buf.Len()
}
// Returns the representation of this metainfo's info dict as a
// bencoded dictionary
func (m *MetaInfo) RawInfo() (b []byte) {
if m.rawInfo != nil {
return m.rawInfo
}
if m.Info == nil {
return []byte{}
}
var buf bytes.Buffer
err := bencode.NewEncoder(&buf).Encode(m.Info)
if err != nil {
log.Fatal("Couldn't bencode this metainfo's dict: ", err)
}
m.rawInfo = buf.Bytes()
return m.rawInfo
}
func getMetaInfo(torrent string) (metaInfo *MetaInfo, err error) {
return NewMetaInfo(torrent)
}
type SessionInfo struct {
PeerId string
Port int
Uploaded int64
Downloaded int64
Left int64
//UseDHT bool
FromMagnet bool
HaveTorrent bool
OurExtensions map[int]string
ME *MetaDataExchange
}
type MetaDataExchange struct {
Transferring bool
Pieces [][]byte
}