This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
magnet.go
297 lines (274 loc) · 7 KB
/
magnet.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2013 Vedran Vuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package magnet is a utility package for parsing magnet links.
package magnet
import (
"encoding/base32"
"encoding/hex"
"errors"
"net/url"
"strconv"
"strings"
)
// Magnet hash type.
type HashType int
// Hash types.
const (
HashTTH HashType = iota // Tiger Tree hash.
HashSHA1 // Secure Hash Algorithm 1.
HashBitPrint // BitPrint.
HashED2K // eDonkey2000 hash.
HashAICH // Advanced Intelligent Corruption Handler.
HashKazaa // Kazaa hash.
HashBTIH // BitTorrent Info Hash.
HashMD5 // Message Digest 5.
)
var HashTypeMap = map[string]HashType{
"tree:tiger": HashTTH,
"sha1": HashSHA1,
"bitprint": HashBitPrint,
"ed2k": HashED2K,
"aich": HashAICH,
"kzhash": HashKazaa,
"btih": HashBTIH,
"md5": HashMD5,
}
// Magnet key type
type KeyType int
// Key types.
const (
KeyAcceptableSource KeyType = iota // Acceptable sources.
KeyDisplayName // Display name.
KeyKeywordTopic // Search keywords.
KeyManifestTopic // Link to metafile containing list of MAGNETO manifests.
KeyTrackerAddress // Tracker address.
KeyExactLength // Exact length in bytes.
KeyExactSource // p2p link.
KeyExactTopic // URN containing file hash.
KeySuplement // Suplemental keys (extensions).
)
// Maps Magnet string keys to KeyType type.
var KeyTypeMap = map[string]KeyType{
"as": KeyAcceptableSource,
"dn": KeyDisplayName,
"kt": KeyKeywordTopic,
"mt": KeyManifestTopic,
"tr": KeyTrackerAddress,
"xl": KeyExactLength,
"xs": KeyExactSource,
"xt": KeyExactTopic,
"x.": KeySuplement,
}
var (
ErrInvalidMagnet = errors.New("invalid magnet")
)
// Defines a Magnet hash.
type Hash struct {
Type HashType // Hash type.
Data []byte // Hash data.
}
// Defines a Magnet URN.
type URN struct {
Hashes []Hash // Hashes, some URNs have multiple.
}
// Create a new *URN structure from an urn string or an error.
func newURN(v string) (*URN, error) {
r := &URN{}
a := strings.Split(v, "urn:")
if len(a) != 2 {
return nil, ErrInvalidMagnet
}
urn := a[1]
ht := HashType(-1)
hd := ""
for d := range HashTypeMap {
if strings.HasPrefix(strings.ToLower(urn), d) {
ht = HashTypeMap[d]
hd = urn[len(d)+1:]
break
}
}
switch ht {
case HashTTH, HashSHA1, HashAICH:
data, err := base32.StdEncoding.DecodeString(hd)
if err != nil {
return nil, err
}
r.Hashes = append(r.Hashes, Hash{ht, data})
case HashED2K, HashKazaa, HashBTIH:
data, err := hex.DecodeString(hd)
if err != nil {
return nil, err
}
r.Hashes = append(r.Hashes, Hash{ht, data})
case HashBitPrint:
b := strings.Split(hd, ".")
if len(b) != 2 {
return nil, ErrInvalidMagnet
}
data, err := base32.StdEncoding.DecodeString(b[0])
if err != nil {
return nil, err
}
r.Hashes = append(r.Hashes, Hash{HashSHA1, data})
data, err = base32.StdEncoding.DecodeString(b[1])
if err != nil {
return nil, err
}
r.Hashes = append(r.Hashes, Hash{HashTTH, data})
}
return r, nil
}
type Suplement struct {
Key string
Val string
}
type Magnet struct {
AcceptableSources []url.URL // Fall-back sources, direct download from a web server.
DisplayNames []string // Filename/display name.
KeywordTopics []string // Search keywords.
ManifestTopics []interface{} // Link to a list of links. URL or URN.
TrackerAddresses []string // Tracker addresses.
ExactLength int64 // Filesize. By logic only one key of thsi type should exist.
ExactSources []string
ExactTopics []URN
Suplements map[string][]Suplement
}
// Defines a magnet key.
type magnetKey struct {
Type KeyType // One of supported key types.
Indx int // Optional index if it's a multiple key:value type pair.
Supl string // If it's a suplemental key, this is its' value.
}
// Creates a new *magnetKey structure from a magnet key string "k" or an error.
func newMagnetKey(k string) (*magnetKey, error) {
r := magnetKey{}
// Fail on unsupported key.
if len(k) < 2 {
return nil, ErrInvalidMagnet
}
var ok bool
if r.Type, ok = KeyTypeMap[k[0:2]]; !ok {
return nil, ErrInvalidMagnet
}
// KeySuplement special case.
if r.Type == KeySuplement {
b := strings.SplitAfterN(k, ".", 1)
if len(b) < 2 {
return nil, ErrInvalidMagnet
}
r.Supl = b[1]
}
// Get index.
c := strings.Split(k, ".")
if len(c) > 2 {
return nil, ErrInvalidMagnet
}
if len(c) == 2 {
v, err := strconv.Atoi(c[1])
if err != nil {
return nil, ErrInvalidMagnet
}
r.Indx = v
}
return &r, nil
}
// Parses key:value pairs, converts to go types and adds to self.
func (m *Magnet) parseKeyVal(k, v string) error {
mk, err := newMagnetKey(k)
if err != nil {
return err
}
switch mk.Type {
case KeyAcceptableSource:
u, err := url.Parse(v)
if err != nil {
return err
}
m.AcceptableSources = append(m.AcceptableSources, *u)
case KeyDisplayName:
u, err := url.QueryUnescape(v)
if err != nil {
return err
}
m.DisplayNames = append(m.DisplayNames, u)
case KeyKeywordTopic:
u, err := url.QueryUnescape(v)
if err != nil {
return err
}
m.KeywordTopics = append(m.KeywordTopics, u)
case KeyManifestTopic:
if strings.HasPrefix(strings.ToLower(v), "urn") {
u, err := newURN(v)
if err != nil {
return err
}
m.ManifestTopics = append(m.ManifestTopics, u)
} else {
u, err := url.QueryUnescape(v)
if err != nil {
return err
}
m.ManifestTopics = append(m.ManifestTopics, u)
}
case KeyTrackerAddress:
u, err := url.QueryUnescape(v)
if err != nil {
return err
}
m.TrackerAddresses = append(m.TrackerAddresses, u)
case KeyExactLength:
u, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return err
}
m.ExactLength = u
case KeyExactSource:
m.ExactSources = append(m.ExactSources, v)
case KeyExactTopic:
u, err := newURN(v)
if err != nil {
return err
}
m.ManifestTopics = append(m.ManifestTopics, u)
case KeySuplement:
m.Suplements[k[0:2]] = append(m.Suplements[k[0:2]], Suplement{mk.Supl, v})
}
return nil
}
// Does the main split then iterates over key:value pairs.
func (m *Magnet) parseMagnet(s string) error {
a := strings.Split(s, ":?")
if len(a) != 2 {
goto error
}
if strings.ToLower(a[0]) != "magnet" {
goto error
}
a = strings.Split(a[1], "&")
if len(a) == 0 {
goto error
}
for _, v := range a {
b := strings.Split(v, "=")
if len(b) < 1 {
goto error
}
if err := m.parseKeyVal(b[0], b[1]); err != nil {
return err
}
}
return nil
error:
return ErrInvalidMagnet
}
// Creates a new *Magnet structure from a magnet string "s" or an error.
func NewMagnet(s string) (*Magnet, error) {
m := &Magnet{}
if err := m.parseMagnet(s); err != nil {
return nil, err
}
return m, nil
}