FedP2P/metainfo/magnet.go

121 lines
2.5 KiB
Go
Raw Normal View History

package metainfo
import (
"encoding/base32"
"encoding/hex"
"errors"
"fmt"
"net/url"
"strings"
)
// Magnet link components.
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.
}
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...)
2015-02-25 11:50:01 +08:00
}
2015-02-25 11:50:01 +08:00
for _, tr := range m.Trackers {
vs.Add("tr", tr)
}
if m.DisplayName != "" {
vs.Add("dn", m.DisplayName)
2015-02-25 11:50:01 +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{
Scheme: "magnet",
RawQuery: "xt=" + xtPrefix + m.InfoHash.HexString(),
}
if len(vs) != 0 {
u.RawQuery += "&" + vs.Encode()
}
return u.String()
}
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) {
u, err := url.Parse(uri)
if err != nil {
err = fmt.Errorf("error parsing uri: %w", err)
return
}
2014-09-11 12:19:23 +08:00
if u.Scheme != "magnet" {
err = fmt.Errorf("unexpected scheme %q", u.Scheme)
2014-09-11 12:19:23 +08:00
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
2015-03-23 01:50:16 +08:00
}
m.Params = q
return
}
2015-03-23 01:50:16 +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
}()
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:]
}
}