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