-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnet.go
120 lines (110 loc) · 2.54 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
package metainfo
import (
"encoding/base32"
"encoding/hex"
"errors"
"fmt"
"net/url"
"strings"
)
// Magnet link components.
type Magnet struct {
InfoHash Hash // Expected in this implementation
Trackers []string // "tr" values
DisplayName string // "dn" value, if not empty
Params url.Values // All other values, such as "x.pe", "as", "xs" etc.
}
const xtPrefix = "urn:btih:"
func (m Magnet) String() string {
// Deep-copy m.Params
vs := make(url.Values, len(m.Params)+len(m.Trackers)+2)
for k, v := range m.Params {
vs[k] = append([]string(nil), v...)
}
for _, tr := range m.Trackers {
vs.Add("tr", tr)
}
if m.DisplayName != "" {
vs.Add("dn", m.DisplayName)
}
// Transmission and Deluge both expect "urn:btih:" to be unescaped. Deluge wants it to be at the
// start of the magnet link. The InfoHash field is expected to be BitTorrent in this
// implementation.
u := url.URL{
Scheme: "magnet",
RawQuery: "xt=" + xtPrefix + m.InfoHash.HexString(),
}
if len(vs) != 0 {
u.RawQuery += "&" + vs.Encode()
}
return u.String()
}
// Deprecated: Use ParseMagnetUri.
var ParseMagnetURI = ParseMagnetUri
// ParseMagnetUri parses Magnet-formatted URIs into a Magnet instance
func ParseMagnetUri(uri string) (m Magnet, err error) {
u, err := url.Parse(uri)
if err != nil {
err = fmt.Errorf("error parsing uri: %w", err)
return
}
if u.Scheme != "magnet" {
err = fmt.Errorf("unexpected scheme %q", u.Scheme)
return
}
q := u.Query()
xt := q.Get("xt")
m.InfoHash, err = parseInfohash(q.Get("xt"))
if err != nil {
err = fmt.Errorf("error parsing infohash %q: %w", xt, err)
return
}
dropFirst(q, "xt")
m.DisplayName = q.Get("dn")
dropFirst(q, "dn")
m.Trackers = q["tr"]
delete(q, "tr")
if len(q) == 0 {
q = nil
}
m.Params = q
return
}
func parseInfohash(xt string) (ih Hash, err error) {
if !strings.HasPrefix(xt, xtPrefix) {
err = errors.New("bad xt parameter prefix")
return
}
encoded := xt[len(xtPrefix):]
decode := func() func(dst, src []byte) (int, error) {
switch len(encoded) {
case 40:
return hex.Decode
case 32:
return base32.StdEncoding.Decode
}
return nil
}()
if decode == nil {
err = fmt.Errorf("unhandled xt parameter encoding (encoded length %d)", len(encoded))
return
}
n, err := decode(ih[:], []byte(encoded))
if err != nil {
err = fmt.Errorf("error decoding xt: %w", err)
return
}
if n != 20 {
panic(n)
}
return
}
func dropFirst(vs url.Values, key string) {
sl := vs[key]
switch len(sl) {
case 0, 1:
vs.Del(key)
default:
vs[key] = sl[1:]
}
}