2014-06-28 17:40:39 +08:00
|
|
|
package metainfo
|
2012-06-28 04:26:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2021-02-24 09:11:41 +08:00
|
|
|
"net/url"
|
2012-06-28 04:26:56 +08:00
|
|
|
"os"
|
2015-10-29 22:21:09 +08:00
|
|
|
"time"
|
2014-12-02 13:28:22 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2012-06-28 04:26:56 +08:00
|
|
|
)
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
type MetaInfo struct {
|
2020-05-01 03:23:20 +08:00
|
|
|
InfoBytes bencode.Bytes `bencode:"info,omitempty"` // BEP 3
|
|
|
|
Announce string `bencode:"announce,omitempty"` // BEP 3
|
|
|
|
AnnounceList AnnounceList `bencode:"announce-list,omitempty"` // BEP 12
|
|
|
|
Nodes []Node `bencode:"nodes,omitempty"` // BEP 5
|
2020-05-13 12:33:19 +08:00
|
|
|
// Where's this specified? Mentioned at
|
|
|
|
// https://wiki.theory.org/index.php/BitTorrentSpecification: (optional) the creation time of
|
|
|
|
// the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
|
|
|
|
CreationDate int64 `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
|
|
|
|
Comment string `bencode:"comment,omitempty"`
|
|
|
|
CreatedBy string `bencode:"created by,omitempty"`
|
|
|
|
Encoding string `bencode:"encoding,omitempty"`
|
2021-11-12 07:45:47 +08:00
|
|
|
UrlList UrlList `bencode:"url-list,omitempty"` // BEP 19 WebSeeds
|
2016-08-26 18:29:05 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
|
|
|
|
// failure.
|
|
|
|
func Load(r io.Reader) (*MetaInfo, error) {
|
|
|
|
var mi MetaInfo
|
2012-06-28 04:26:56 +08:00
|
|
|
d := bencode.NewDecoder(r)
|
2014-07-13 15:36:06 +08:00
|
|
|
err := d.Decode(&mi)
|
2012-06-28 04:26:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-07-02 03:05:10 +08:00
|
|
|
return &mi, nil
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2012-07-02 03:05:10 +08:00
|
|
|
// Convenience function for loading a MetaInfo from a file.
|
|
|
|
func LoadFromFile(filename string) (*MetaInfo, error) {
|
2012-06-28 04:26:56 +08:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2012-06-30 06:23:02 +08:00
|
|
|
return Load(f)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 10:25:40 +08:00
|
|
|
func (mi MetaInfo) UnmarshalInfo() (info Info, err error) {
|
|
|
|
err = bencode.Unmarshal(mi.InfoBytes, &info)
|
2015-10-29 22:21:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-26 18:29:05 +08:00
|
|
|
func (mi MetaInfo) HashInfoBytes() (infoHash Hash) {
|
|
|
|
return HashBytes(mi.InfoBytes)
|
2012-06-28 04:26:56 +08:00
|
|
|
}
|
2015-10-29 22:21:09 +08:00
|
|
|
|
|
|
|
// Encode to bencoded form.
|
2016-08-26 18:29:05 +08:00
|
|
|
func (mi MetaInfo) Write(w io.Writer) error {
|
2015-10-29 22:21:09 +08:00
|
|
|
return bencode.NewEncoder(w).Encode(mi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set good default values in preparation for creating a new MetaInfo file.
|
|
|
|
func (mi *MetaInfo) SetDefaults() {
|
|
|
|
mi.CreatedBy = "github.com/anacrolix/torrent"
|
|
|
|
mi.CreationDate = time.Now().Unix()
|
|
|
|
}
|
2016-03-28 17:38:30 +08:00
|
|
|
|
2021-02-24 09:03:16 +08:00
|
|
|
// Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.
|
2022-06-14 16:23:00 +08:00
|
|
|
func (mi MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet) {
|
2021-03-25 06:56:04 +08:00
|
|
|
m.Trackers = append(m.Trackers, mi.UpvertedAnnounceList().DistinctValues()...)
|
2021-02-24 09:03:16 +08:00
|
|
|
if info != nil {
|
2022-03-18 06:15:50 +08:00
|
|
|
m.DisplayName = info.BestName()
|
2021-02-24 09:03:16 +08:00
|
|
|
}
|
|
|
|
if infoHash != nil {
|
|
|
|
m.InfoHash = *infoHash
|
|
|
|
} else {
|
|
|
|
m.InfoHash = mi.HashInfoBytes()
|
|
|
|
}
|
2021-02-24 09:11:41 +08:00
|
|
|
m.Params = make(url.Values)
|
|
|
|
m.Params["ws"] = mi.UrlList
|
2016-04-04 11:48:39 +08:00
|
|
|
return
|
2016-03-28 18:57:04 +08:00
|
|
|
}
|
2017-02-15 17:05:28 +08:00
|
|
|
|
|
|
|
// Returns the announce list converted from the old single announce field if
|
|
|
|
// necessary.
|
|
|
|
func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList {
|
|
|
|
if mi.AnnounceList.OverridesAnnounce(mi.Announce) {
|
|
|
|
return mi.AnnounceList
|
|
|
|
}
|
|
|
|
if mi.Announce != "" {
|
2019-08-19 10:15:54 +08:00
|
|
|
return [][]string{{mi.Announce}}
|
2017-02-15 17:05:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|