client.go | 24 ++++++++++++++++++++++-- client_test.go | 6 +++++- diff --git a/client.go b/client.go index cff3e5624b411b95177115b68a2bd54219a9f6c6..3a15bb3ab3b8977cfb0e0e2b3d3a20883ba13ef6 100644 --- a/client.go +++ b/client.go @@ -2526,6 +2526,9 @@ } func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) { T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) + var ss []string + missinggo.CastSlice(&ss, mi.Nodes) + me.AddDHTNodes(ss) return } @@ -2534,10 +2537,27 @@ mi, err := metainfo.LoadFromFile(filename) if err != nil { return } - T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) - return + return me.AddTorrent(mi) } func (me *Client) DHT() *dht.Server { return me.dHT } + +func (me *Client) AddDHTNodes(nodes []string) { + for _, n := range nodes { + hmp := missinggo.SplitHostPort(n) + ip := net.ParseIP(hmp.Host) + if ip == nil { + log.Printf("won't add DHT node with bad IP: %q", hmp.Host) + continue + } + ni := dht.NodeInfo{ + Addr: dht.NewAddr(&net.UDPAddr{ + IP: ip, + Port: hmp.Port, + }), + } + me.DHT().AddNode(ni) + } +} diff --git a/client_test.go b/client_test.go index 05c81a6d5198905b3d7fbea641423f869cc95def..25a82d28e9808eb85c9a38401392dc63ba5c5654 100644 --- a/client_test.go +++ b/client_test.go @@ -755,9 +755,13 @@ func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) { testAddTorrentPriorPieceCompletion(t, false) } -func TestAddIssue65Torrent(t *testing.T) { +func TestAddMetainfoWithNodes(t *testing.T) { cfg := TestingConfig cfg.NoDHT = false + // For now, we want to just jam the nodes into the table, without + // verifying them first. Also the DHT code doesn't support mixing secure + // and insecure nodes if security is enabled (yet). + cfg.DHTConfig.NoSecurity = true cl, err := NewClient(&cfg) require.NoError(t, err) defer cl.Close()