]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add webseeds from magnet links
authorMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2020 03:53:25 +0000 (13:53 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2020 03:53:25 +0000 (13:53 +1000)
client.go
spec.go

index 9861f7ef3ee052c9aa3e664ed6fe933a8b25f840..96a37c025176aca3ee9c442120f965c38f344d83 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1155,8 +1155,12 @@ func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err e
                        return
                }
        }
+       cl.AddDHTNodes(spec.DhtNodes)
        cl.lock()
        defer cl.unlock()
+       for _, url := range spec.Webseeds {
+               t.addWebSeed(url)
+       }
        if spec.ChunkSize != 0 {
                t.setChunkSize(pp.Integer(spec.ChunkSize))
        }
@@ -1230,12 +1234,6 @@ func (cl *Client) AddMagnet(uri string) (T *Torrent, err error) {
 
 func (cl *Client) AddTorrent(mi *metainfo.MetaInfo) (T *Torrent, err error) {
        T, _, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
-       var ss []string
-       slices.MakeInto(&ss, mi.Nodes)
-       cl.AddDHTNodes(ss)
-       for _, url := range mi.UrlList {
-               T.addWebSeed(url)
-       }
        return
 }
 
diff --git a/spec.go b/spec.go
index 5dbd472bda965b5dbffed780ecefa3a456d13a3a..e0c13bd8ce5db01edf670e204ed49108442dac88 100644 (file)
--- a/spec.go
+++ b/spec.go
@@ -5,8 +5,8 @@ import (
        "github.com/anacrolix/torrent/storage"
 )
 
-// Specifies a new torrent for adding to a client. There are helpers for
-// magnet URIs and torrent metainfo files.
+// Specifies a new torrent for adding to a client. There are helpers for magnet URIs and torrent
+// metainfo files.
 type TorrentSpec struct {
        // The tiered tracker URIs.
        Trackers  [][]string
@@ -14,8 +14,10 @@ type TorrentSpec struct {
        InfoBytes []byte
        // The name to use if the Name field from the Info isn't available.
        DisplayName string
-       // The chunk size to use for outbound requests. Defaults to 16KiB if not
-       // set.
+       Webseeds    []string
+       DhtNodes    []string
+
+       // The chunk size to use for outbound requests. Defaults to 16KiB if not set.
        ChunkSize int
        Storage   storage.ClientImpl
 }
@@ -29,20 +31,29 @@ func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
                Trackers:    [][]string{m.Trackers},
                DisplayName: m.DisplayName,
                InfoHash:    m.InfoHash,
+               Webseeds:    m.Params["ws"],
+               // TODO: What's the parameter for DHT nodes or bootstrap peers in a magnet link?
        }
        return
 }
 
-func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
-       info, _ := mi.UnmarshalInfo()
-       spec = &TorrentSpec{
-               Trackers:    mi.AnnounceList,
+func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
+       info, err := mi.UnmarshalInfo()
+       if err != nil {
+               panic(err)
+       }
+       return &TorrentSpec{
+               Trackers:    mi.UpvertedAnnounceList(),
+               InfoHash:    mi.HashInfoBytes(),
                InfoBytes:   mi.InfoBytes,
                DisplayName: info.Name,
-               InfoHash:    mi.HashInfoBytes(),
+               Webseeds:    mi.UrlList,
+               DhtNodes: func() (ret []string) {
+                       ret = make([]string, len(mi.Nodes))
+                       for _, node := range mi.Nodes {
+                               ret = append(ret, string(node))
+                       }
+                       return
+               }(),
        }
-       if spec.Trackers == nil && mi.Announce != "" {
-               spec.Trackers = [][]string{{mi.Announce}}
-       }
-       return
 }