2016-04-04 11:48:39 +08:00
|
|
|
package metainfo
|
2014-06-26 22:57:07 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base32"
|
|
|
|
"encoding/hex"
|
2019-09-24 13:52:18 +08:00
|
|
|
"errors"
|
2014-06-26 22:57:07 +08:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
// Magnet link components.
|
2014-06-26 22:57:07 +08:00
|
|
|
type Magnet struct {
|
2019-09-24 14:29:29 +08:00
|
|
|
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.
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const xtPrefix = "urn:btih:"
|
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
func (m Magnet) String() string {
|
2019-09-24 13:52:18 +08:00
|
|
|
// 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...)
|
2015-02-25 11:50:01 +08:00
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
|
2015-02-25 11:50:01 +08:00
|
|
|
for _, tr := range m.Trackers {
|
2019-09-24 13:52:18 +08:00
|
|
|
vs.Add("tr", tr)
|
|
|
|
}
|
|
|
|
if m.DisplayName != "" {
|
|
|
|
vs.Add("dn", m.DisplayName)
|
2015-02-25 11:50:01 +08:00
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
|
2019-09-24 14:29:17 +08:00
|
|
|
// 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{
|
2019-09-24 13:52:18 +08:00
|
|
|
Scheme: "magnet",
|
2019-09-24 14:29:17 +08:00
|
|
|
RawQuery: "xt=" + xtPrefix + m.InfoHash.HexString(),
|
|
|
|
}
|
|
|
|
if len(vs) != 0 {
|
|
|
|
u.RawQuery += "&" + vs.Encode()
|
|
|
|
}
|
|
|
|
return u.String()
|
2016-04-03 21:51:46 +08:00
|
|
|
}
|
|
|
|
|
2020-12-21 07:16:08 +08:00
|
|
|
// Deprecated: Use ParseMagnetUri.
|
|
|
|
var ParseMagnetURI = ParseMagnetUri
|
|
|
|
|
2020-11-09 07:56:27 +08:00
|
|
|
// ParseMagnetUri parses Magnet-formatted URIs into a Magnet instance
|
|
|
|
func ParseMagnetUri(uri string) (m Magnet, err error) {
|
2014-06-26 22:57:07 +08:00
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
2019-09-24 13:52:18 +08:00
|
|
|
err = fmt.Errorf("error parsing uri: %w", err)
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
2014-09-11 12:19:23 +08:00
|
|
|
if u.Scheme != "magnet" {
|
2019-09-24 13:52:18 +08:00
|
|
|
err = fmt.Errorf("unexpected scheme %q", u.Scheme)
|
2014-09-11 12:19:23 +08:00
|
|
|
return
|
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
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)
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
dropFirst(q, "xt")
|
|
|
|
m.DisplayName = q.Get("dn")
|
|
|
|
dropFirst(q, "dn")
|
|
|
|
m.Trackers = q["tr"]
|
|
|
|
delete(q, "tr")
|
|
|
|
if len(q) == 0 {
|
|
|
|
q = nil
|
2015-03-23 01:50:16 +08:00
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
m.Params = q
|
|
|
|
return
|
|
|
|
}
|
2015-03-23 01:50:16 +08:00
|
|
|
|
2019-09-24 13:52:18 +08:00
|
|
|
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
|
|
|
|
}()
|
2014-06-26 22:57:07 +08:00
|
|
|
if decode == nil {
|
2019-09-24 13:52:18 +08:00
|
|
|
err = fmt.Errorf("unhandled xt parameter encoding (encoded length %d)", len(encoded))
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
n, err := decode(ih[:], []byte(encoded))
|
2014-06-26 22:57:07 +08:00
|
|
|
if err != nil {
|
2019-09-24 13:52:18 +08:00
|
|
|
err = fmt.Errorf("error decoding xt: %w", err)
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != 20 {
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-09-24 13:52:18 +08:00
|
|
|
|
|
|
|
func dropFirst(vs url.Values, key string) {
|
|
|
|
sl := vs[key]
|
|
|
|
switch len(sl) {
|
|
|
|
case 0, 1:
|
|
|
|
vs.Del(key)
|
|
|
|
default:
|
|
|
|
vs[key] = sl[1:]
|
|
|
|
}
|
|
|
|
}
|