]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add support for the x.pe magnet link parameter
authorMatt Joiner <anacrolix@gmail.com>
Thu, 12 Nov 2020 04:25:06 +0000 (15:25 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 16 Nov 2020 05:37:11 +0000 (16:37 +1100)
client.go
peerconn.go
spec.go

index 8dce3d40864f7713a2d022f407a0546f25644eef..2ca1661db3f581310820ee3cf7f3cdc37aabcbfa 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1164,6 +1164,13 @@ func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err e
        return
 }
 
+type stringAddr string
+
+var _ net.Addr = stringAddr("")
+
+func (stringAddr) Network() string   { return "" }
+func (me stringAddr) String() string { return string(me) }
+
 // The trackers will be merged with the existing ones. If the Info isn't yet known, it will be set.
 // spec.DisallowDataDownload/Upload will be read and applied
 // The display name is replaced if the new spec provides one. Note that any `Storage` is ignored.
@@ -1185,6 +1192,13 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
        for _, url := range spec.Webseeds {
                t.addWebSeed(url)
        }
+       for _, peerAddr := range spec.PeerAddrs {
+               t.addPeer(PeerInfo{
+                       Addr:    stringAddr(peerAddr),
+                       Source:  PeerSourceDirect,
+                       Trusted: true,
+               })
+       }
        if spec.ChunkSize != 0 {
                t.setChunkSize(pp.Integer(spec.ChunkSize))
        }
index 48acbc111f111386b06c76254af616043c430f54..50c86ad6ecfec0f8d866f4681c86080283b0eb30 100644 (file)
@@ -34,6 +34,8 @@ const (
        PeerSourceDhtGetPeers     = "Hg" // Peers we found by searching a DHT.
        PeerSourceDhtAnnouncePeer = "Ha" // Peers that were announced to us by a DHT.
        PeerSourcePex             = "X"
+       // The peer was given directly, such as through a magnet link.
+       PeerSourceDirect = "M"
 )
 
 type peer struct {
diff --git a/spec.go b/spec.go
index 0818a99cd6fae423af8018bb711a982f8d144f21..7de7aa611139f2ec6927479616e718b1fe390778 100644 (file)
--- a/spec.go
+++ b/spec.go
@@ -16,6 +16,7 @@ type TorrentSpec struct {
        DisplayName string
        Webseeds    []string
        DhtNodes    []string
+       PeerAddrs   []string
        // The combination of the "xs" and "as" fields in magnet links, for now.
        Sources []string
 
@@ -39,7 +40,8 @@ func TorrentSpecFromMagnetUri(uri string) (spec *TorrentSpec, err error) {
                InfoHash:    m.InfoHash,
                Webseeds:    m.Params["ws"],
                Sources:     append(m.Params["xs"], m.Params["as"]...),
-               // TODO: What's the parameter for DHT nodes or bootstrap peers in a magnet link?
+               PeerAddrs:   m.Params["x.pe"], // BEP 9
+               // TODO: What's the parameter for DHT nodes?
        }
        return
 }