Add DHT nodes from metainfo when added to Client
This commit is contained in:
parent
2fa233345e
commit
976510db60
24
client.go
24
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue