2016-09-11 12:09:40 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2021-08-01 19:38:35 +08:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-11 12:09:40 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2021-10-10 08:22:29 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2016-09-11 12:09:40 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
|
|
|
)
|
|
|
|
|
2021-10-10 08:22:29 +08:00
|
|
|
// Specifies a new torrent for adding to a client, or additions to an existing Torrent. There are
|
|
|
|
// constructor functions for magnet URIs and torrent metainfo files. TODO: This type should be
|
|
|
|
// dismantled into a new Torrent option type, and separate Torrent mutate method(s).
|
2016-09-11 12:09:40 +08:00
|
|
|
type TorrentSpec struct {
|
|
|
|
// The tiered tracker URIs.
|
2021-10-10 08:22:29 +08:00
|
|
|
Trackers [][]string
|
|
|
|
// TODO: Move into a "new" Torrent opt type.
|
2016-09-11 12:09:40 +08:00
|
|
|
InfoHash metainfo.Hash
|
|
|
|
InfoBytes []byte
|
|
|
|
// The name to use if the Name field from the Info isn't available.
|
|
|
|
DisplayName string
|
2022-04-26 08:46:01 +08:00
|
|
|
// WebSeed URLs. For additional options add the URLs separately with Torrent.AddWebSeeds
|
|
|
|
// instead.
|
|
|
|
Webseeds []string
|
|
|
|
DhtNodes []string
|
|
|
|
PeerAddrs []string
|
2020-06-02 13:06:05 +08:00
|
|
|
// The combination of the "xs" and "as" fields in magnet links, for now.
|
|
|
|
Sources []string
|
2020-06-02 11:53:25 +08:00
|
|
|
|
2021-10-10 08:22:29 +08:00
|
|
|
// The chunk size to use for outbound requests. Defaults to 16KiB if not set. Can only be set
|
|
|
|
// for new Torrents. TODO: Move into a "new" Torrent opt type.
|
|
|
|
ChunkSize pp.Integer
|
|
|
|
// TODO: Move into a "new" Torrent opt type.
|
|
|
|
Storage storage.ClientImpl
|
2021-10-19 10:39:31 +08:00
|
|
|
|
2021-10-11 08:33:40 +08:00
|
|
|
DisableInitialPieceCheck bool
|
2020-06-04 03:17:47 +08:00
|
|
|
|
|
|
|
// Whether to allow data download or upload
|
|
|
|
DisallowDataUpload bool
|
|
|
|
DisallowDataDownload bool
|
2016-09-11 12:09:40 +08:00
|
|
|
}
|
|
|
|
|
2020-11-09 07:56:27 +08:00
|
|
|
func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
|
|
|
|
m, err := metainfo.ParseMagnetUri(uri)
|
2016-09-11 12:09:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
spec = &TorrentSpec{
|
|
|
|
Trackers: [][]string{m.Trackers},
|
|
|
|
DisplayName: m.DisplayName,
|
|
|
|
InfoHash: m.InfoHash,
|
2020-06-02 11:53:25 +08:00
|
|
|
Webseeds: m.Params["ws"],
|
2020-06-02 13:06:05 +08:00
|
|
|
Sources: append(m.Params["xs"], m.Params["as"]...),
|
2020-11-12 12:25:06 +08:00
|
|
|
PeerAddrs: m.Params["x.pe"], // BEP 9
|
|
|
|
// TODO: What's the parameter for DHT nodes?
|
2016-09-11 12:09:40 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-12 07:46:35 +08:00
|
|
|
// The error will be from unmarshalling the info bytes. The TorrentSpec is still filled out as much
|
|
|
|
// as possible in this case.
|
2021-08-01 19:38:35 +08:00
|
|
|
func TorrentSpecFromMetaInfoErr(mi *metainfo.MetaInfo) (*TorrentSpec, error) {
|
2020-06-02 11:53:25 +08:00
|
|
|
info, err := mi.UnmarshalInfo()
|
|
|
|
if err != nil {
|
2021-11-12 07:46:35 +08:00
|
|
|
err = fmt.Errorf("unmarshalling info: %w", err)
|
2020-06-02 11:53:25 +08:00
|
|
|
}
|
|
|
|
return &TorrentSpec{
|
|
|
|
Trackers: mi.UpvertedAnnounceList(),
|
|
|
|
InfoHash: mi.HashInfoBytes(),
|
2016-09-11 12:09:40 +08:00
|
|
|
InfoBytes: mi.InfoBytes,
|
|
|
|
DisplayName: info.Name,
|
2020-06-02 11:53:25 +08:00
|
|
|
Webseeds: mi.UrlList,
|
|
|
|
DhtNodes: func() (ret []string) {
|
2021-08-11 10:27:00 +08:00
|
|
|
ret = make([]string, 0, len(mi.Nodes))
|
2020-06-02 11:53:25 +08:00
|
|
|
for _, node := range mi.Nodes {
|
|
|
|
ret = append(ret, string(node))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}(),
|
2021-11-12 07:46:35 +08:00
|
|
|
}, err
|
2021-08-01 19:38:35 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 07:46:35 +08:00
|
|
|
// Panics if there was anything missing from the metainfo.
|
2021-08-01 19:38:35 +08:00
|
|
|
func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
|
|
|
|
ts, err := TorrentSpecFromMetaInfoErr(mi)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-09-11 12:09:40 +08:00
|
|
|
}
|
2021-08-01 19:38:35 +08:00
|
|
|
return ts
|
2016-09-11 12:09:40 +08:00
|
|
|
}
|