From 976510db602abd192a400bb0aa9351dc96ef5e35 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 24 Feb 2016 21:56:50 +1100 Subject: [PATCH] Add DHT nodes from metainfo when added to Client --- client.go | 24 ++++++++++++++++++++++-- client_test.go | 6 +++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index cff3e562..3a15bb3a 100644 --- a/client.go +++ b/client.go @@ -2526,6 +2526,9 @@ func (me *Client) AddMagnet(uri string) (T Torrent, err error) { 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 @@ func (me *Client) AddTorrentFromFile(filename string) (T Torrent, err error) { 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 05c81a6d..25a82d28 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()