2014-11-18 18:38:13 +00:00
|
|
|
package dht
|
|
|
|
|
2015-01-29 03:17:27 +00:00
|
|
|
// get_peers and announce_peers.
|
|
|
|
|
2014-11-18 18:38:13 +00:00
|
|
|
import (
|
2014-12-26 06:21:48 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-08-03 14:29:01 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-03-20 05:46:39 +00:00
|
|
|
"github.com/anacrolix/sync"
|
2015-12-06 16:28:28 +00:00
|
|
|
"github.com/willf/bloom"
|
2015-12-17 13:50:22 +00:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/logonce"
|
2014-11-18 18:38:13 +00:00
|
|
|
)
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Maintains state for an ongoing Announce operation. An Announce is started
|
|
|
|
// by calling Server.Announce.
|
|
|
|
type Announce struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
Peers chan PeersValues
|
|
|
|
// Inner chan is set to nil when on close.
|
|
|
|
values chan PeersValues
|
|
|
|
stop chan struct{}
|
2015-01-29 03:20:21 +00:00
|
|
|
triedAddrs *bloom.BloomFilter
|
|
|
|
pending int
|
|
|
|
server *Server
|
|
|
|
infoHash string
|
|
|
|
numContacted int
|
|
|
|
announcePort int
|
|
|
|
announcePortImplied bool
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Returns the number of distinct remote addresses the announce has queried.
|
|
|
|
func (me *Announce) NumContacted() int {
|
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
|
|
|
return me.numContacted
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// This is kind of the main thing you want to do with DHT. It traverses the
|
|
|
|
// graph toward nodes that store peers for the infohash, streaming them to the
|
|
|
|
// caller, and announcing the local node to each node if allowed and
|
|
|
|
// specified.
|
|
|
|
func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announce, error) {
|
2014-11-18 18:38:13 +00:00
|
|
|
s.mu.Lock()
|
2014-12-26 06:21:48 +00:00
|
|
|
startAddrs := func() (ret []dHTAddr) {
|
2014-11-18 18:38:13 +00:00
|
|
|
for _, n := range s.closestGoodNodes(160, infoHash) {
|
|
|
|
ret = append(ret, n.addr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
s.mu.Unlock()
|
|
|
|
if len(startAddrs) == 0 {
|
2015-04-02 22:35:30 +00:00
|
|
|
addrs, err := bootstrapAddrs(s.bootstrapNodes)
|
2014-11-18 18:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 23:09:11 +00:00
|
|
|
for _, addr := range addrs {
|
2014-12-26 06:21:48 +00:00
|
|
|
startAddrs = append(startAddrs, newDHTAddr(addr))
|
2014-12-19 23:09:11 +00:00
|
|
|
}
|
2014-12-07 03:22:12 +00:00
|
|
|
}
|
2015-04-01 06:29:55 +00:00
|
|
|
disc := &Announce{
|
|
|
|
Peers: make(chan PeersValues, 100),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
values: make(chan PeersValues),
|
2015-01-29 03:20:21 +00:00
|
|
|
triedAddrs: bloom.NewWithEstimates(1000, 0.5),
|
|
|
|
server: s,
|
|
|
|
infoHash: infoHash,
|
|
|
|
announcePort: port,
|
|
|
|
announcePortImplied: impliedPort,
|
2014-12-07 03:22:12 +00:00
|
|
|
}
|
2014-12-09 03:57:53 +00:00
|
|
|
// Function ferries from values to Values until discovery is halted.
|
|
|
|
go func() {
|
2015-04-01 06:29:55 +00:00
|
|
|
defer close(disc.Peers)
|
2014-12-09 03:57:53 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case psv := <-disc.values:
|
|
|
|
select {
|
2015-04-01 06:29:55 +00:00
|
|
|
case disc.Peers <- psv:
|
2014-12-09 03:57:53 +00:00
|
|
|
case <-disc.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-disc.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-12-09 06:24:05 +00:00
|
|
|
for i, addr := range startAddrs {
|
|
|
|
if i != 0 {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
2014-12-20 02:02:12 +00:00
|
|
|
disc.mu.Lock()
|
2014-11-18 18:38:13 +00:00
|
|
|
disc.contact(addr)
|
2014-12-20 02:02:12 +00:00
|
|
|
disc.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
return disc, nil
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) gotNodeAddr(addr dHTAddr) {
|
2015-08-03 14:29:01 +00:00
|
|
|
if missinggo.AddrPort(addr) == 0 {
|
2014-12-09 02:00:42 +00:00
|
|
|
// Not a contactable address.
|
|
|
|
return
|
|
|
|
}
|
2014-12-19 23:10:22 +00:00
|
|
|
if me.triedAddrs.Test([]byte(addr.String())) {
|
2014-12-09 02:00:42 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-30 12:12:33 +00:00
|
|
|
if me.server.ipBlocked(addr.UDPAddr().IP) {
|
2014-12-09 02:00:42 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-03 14:31:53 +00:00
|
|
|
me.server.mu.Lock()
|
|
|
|
if me.server.badNodes.Test([]byte(addr.String())) {
|
|
|
|
me.server.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
me.server.mu.Unlock()
|
2014-12-19 23:10:22 +00:00
|
|
|
me.contact(addr)
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) contact(addr dHTAddr) {
|
2015-01-29 03:20:21 +00:00
|
|
|
me.numContacted++
|
2014-12-19 23:10:22 +00:00
|
|
|
me.triedAddrs.Add([]byte(addr.String()))
|
2014-12-09 02:00:42 +00:00
|
|
|
if err := me.getPeers(addr); err != nil {
|
2015-02-21 04:00:48 +00:00
|
|
|
log.Printf("error sending get_peers request to %s: %#v", addr, err)
|
2014-12-09 01:14:10 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
me.pending++
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) transactionClosed() {
|
2014-12-09 02:00:42 +00:00
|
|
|
me.pending--
|
|
|
|
if me.pending == 0 {
|
2014-12-20 02:02:12 +00:00
|
|
|
me.close()
|
2014-12-09 02:00:42 +00:00
|
|
|
return
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) responseNode(node NodeInfo) {
|
2014-12-09 02:00:42 +00:00
|
|
|
me.gotNodeAddr(node.Addr)
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) closingCh() chan struct{} {
|
|
|
|
return me.stop
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 13:50:22 +00:00
|
|
|
// Announce to a peer, if appropriate.
|
|
|
|
func (me *Announce) maybeAnnouncePeer(to dHTAddr, token, peerId string) {
|
2015-02-09 13:21:22 +00:00
|
|
|
me.server.mu.Lock()
|
2015-12-17 13:50:22 +00:00
|
|
|
defer me.server.mu.Unlock()
|
|
|
|
if !me.server.config.NoSecurity {
|
|
|
|
if len(peerId) != 20 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !NodeIdSecure(peerId, to.IP()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
err := me.server.announcePeer(to, me.infoHash, me.announcePort, token, me.announcePortImplied)
|
|
|
|
if err != nil {
|
|
|
|
logonce.Stderr.Printf("error announcing peer: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (me *Announce) getPeers(addr dHTAddr) error {
|
2014-11-18 18:38:13 +00:00
|
|
|
me.server.mu.Lock()
|
|
|
|
defer me.server.mu.Unlock()
|
|
|
|
t, err := me.server.getPeers(addr, me.infoHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 16:28:28 +00:00
|
|
|
t.SetResponseHandler(func(m Msg, ok bool) {
|
2015-02-21 04:00:48 +00:00
|
|
|
// Register suggested nodes closer to the target info-hash.
|
2015-10-23 01:41:45 +00:00
|
|
|
if m.R != nil {
|
|
|
|
me.mu.Lock()
|
|
|
|
for _, n := range m.R.Nodes {
|
|
|
|
me.responseNode(n)
|
|
|
|
}
|
|
|
|
me.mu.Unlock()
|
2015-01-29 03:20:21 +00:00
|
|
|
|
2015-10-23 01:41:45 +00:00
|
|
|
if vs := m.R.Values; len(vs) != 0 {
|
|
|
|
nodeInfo := NodeInfo{
|
|
|
|
Addr: t.remoteAddr,
|
|
|
|
}
|
|
|
|
copy(nodeInfo.ID[:], m.SenderID())
|
|
|
|
select {
|
|
|
|
case me.values <- PeersValues{
|
|
|
|
Peers: func() (ret []Peer) {
|
|
|
|
for _, cp := range vs {
|
|
|
|
ret = append(ret, Peer(cp))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}(),
|
|
|
|
NodeInfo: nodeInfo,
|
|
|
|
}:
|
|
|
|
case <-me.stop:
|
2015-08-03 14:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-21 04:00:48 +00:00
|
|
|
|
2015-12-17 13:50:22 +00:00
|
|
|
me.maybeAnnouncePeer(addr, m.R.Token, m.SenderID())
|
2015-02-21 04:00:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Lock()
|
|
|
|
me.transactionClosed()
|
|
|
|
me.mu.Unlock()
|
2015-02-21 04:00:48 +00:00
|
|
|
})
|
2014-11-18 18:38:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Corresponds to the "values" key in a get_peers KRPC response. A list of
|
|
|
|
// peers that a node has reported as being in the swarm for a queried info
|
|
|
|
// hash.
|
|
|
|
type PeersValues struct {
|
2015-08-17 09:52:47 +00:00
|
|
|
Peers []Peer // Peers given in get_peers response.
|
|
|
|
NodeInfo // The node that gave the response.
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
// Stop the announce.
|
|
|
|
func (me *Announce) Close() {
|
|
|
|
me.mu.Lock()
|
|
|
|
defer me.mu.Unlock()
|
|
|
|
me.close()
|
2014-12-20 02:02:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:29:55 +00:00
|
|
|
func (ps *Announce) close() {
|
2014-11-18 18:38:13 +00:00
|
|
|
select {
|
|
|
|
case <-ps.stop:
|
|
|
|
default:
|
|
|
|
close(ps.stop)
|
|
|
|
}
|
|
|
|
}
|