2016-04-04 11:48:39 +08:00
|
|
|
package metainfo
|
2014-06-26 22:57:07 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base32"
|
|
|
|
"encoding/hex"
|
|
|
|
"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 {
|
2016-04-04 11:48:39 +08:00
|
|
|
InfoHash Hash
|
2014-06-26 22:57:07 +08:00
|
|
|
Trackers []string
|
|
|
|
DisplayName string
|
|
|
|
}
|
|
|
|
|
|
|
|
const xtPrefix = "urn:btih:"
|
|
|
|
|
2016-04-04 11:48:39 +08:00
|
|
|
func (m Magnet) String() string {
|
2015-02-25 11:50:01 +08:00
|
|
|
// net.URL likes to assume //, and encodes ':' on us, so we do most of
|
|
|
|
// this manually.
|
2016-04-03 21:51:46 +08:00
|
|
|
ret := "magnet:?xt="
|
2015-02-25 11:50:01 +08:00
|
|
|
ret += xtPrefix + hex.EncodeToString(m.InfoHash[:])
|
|
|
|
if m.DisplayName != "" {
|
|
|
|
ret += "&dn=" + url.QueryEscape(m.DisplayName)
|
|
|
|
}
|
|
|
|
for _, tr := range m.Trackers {
|
|
|
|
ret += "&tr=" + url.QueryEscape(tr)
|
|
|
|
}
|
2016-04-03 21:51:46 +08:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-03-23 01:50:16 +08:00
|
|
|
// ParseMagnetURI parses Magnet-formatted URIs into a Magnet instance
|
2014-06-26 22:57:07 +08:00
|
|
|
func ParseMagnetURI(uri string) (m Magnet, err error) {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error parsing uri: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-11 12:19:23 +08:00
|
|
|
if u.Scheme != "magnet" {
|
|
|
|
err = fmt.Errorf("unexpected scheme: %q", u.Scheme)
|
|
|
|
return
|
|
|
|
}
|
2014-06-26 22:57:07 +08:00
|
|
|
xt := u.Query().Get("xt")
|
|
|
|
if !strings.HasPrefix(xt, xtPrefix) {
|
|
|
|
err = fmt.Errorf("bad xt parameter")
|
|
|
|
return
|
|
|
|
}
|
2015-03-23 01:50:16 +08:00
|
|
|
infoHash := xt[len(xtPrefix):]
|
|
|
|
|
|
|
|
// BTIH hash can be in HEX or BASE32 encoding
|
2016-09-07 07:43:29 +08:00
|
|
|
// will assign appropriate func judging from symbol length
|
2015-03-23 01:50:16 +08:00
|
|
|
var decode func(dst, src []byte) (int, error)
|
|
|
|
switch len(infoHash) {
|
|
|
|
case 40:
|
|
|
|
decode = hex.Decode
|
|
|
|
case 32:
|
|
|
|
decode = base32.StdEncoding.Decode
|
|
|
|
}
|
|
|
|
|
2014-06-26 22:57:07 +08:00
|
|
|
if decode == nil {
|
2015-03-23 01:50:16 +08:00
|
|
|
err = fmt.Errorf("unhandled xt parameter encoding: encoded length %d", len(infoHash))
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
2015-03-23 01:50:16 +08:00
|
|
|
n, err := decode(m.InfoHash[:], []byte(infoHash))
|
2014-06-26 22:57:07 +08:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error decoding xt: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != 20 {
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
m.DisplayName = u.Query().Get("dn")
|
|
|
|
m.Trackers = u.Query()["tr"]
|
|
|
|
return
|
|
|
|
}
|