-
Notifications
You must be signed in to change notification settings - Fork 0
/
announce.go
228 lines (208 loc) · 4.99 KB
/
announce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package dht
// get_peers and announce_peers.
import (
"log"
"time"
"github.com/anacrolix/sync"
"github.com/willf/bloom"
"github.com/anacrolix/torrent/logonce"
)
// 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{}
triedAddrs *bloom.BloomFilter
pending int
server *Server
infoHash string
numContacted int
announcePort int
announcePortImplied bool
}
// 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
}
// 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) {
s.mu.Lock()
startAddrs := func() (ret []Addr) {
for _, n := range s.closestGoodNodes(160, infoHash) {
ret = append(ret, n.addr)
}
return
}()
s.mu.Unlock()
if len(startAddrs) == 0 {
addrs, err := bootstrapAddrs(s.bootstrapNodes)
if err != nil {
return nil, err
}
for _, addr := range addrs {
startAddrs = append(startAddrs, NewAddr(addr))
}
}
disc := &Announce{
Peers: make(chan PeersValues, 100),
stop: make(chan struct{}),
values: make(chan PeersValues),
triedAddrs: bloom.NewWithEstimates(1000, 0.5),
server: s,
infoHash: infoHash,
announcePort: port,
announcePortImplied: impliedPort,
}
// Function ferries from values to Values until discovery is halted.
go func() {
defer close(disc.Peers)
for {
select {
case psv := <-disc.values:
select {
case disc.Peers <- psv:
case <-disc.stop:
return
}
case <-disc.stop:
return
}
}
}()
for i, addr := range startAddrs {
if i != 0 {
time.Sleep(time.Millisecond)
}
disc.mu.Lock()
disc.contact(addr)
disc.mu.Unlock()
}
return disc, nil
}
func (me *Announce) gotNodeAddr(addr Addr) {
if addr.UDPAddr().Port == 0 {
// Not a contactable address.
return
}
if me.triedAddrs.Test([]byte(addr.String())) {
return
}
if me.server.ipBlocked(addr.UDPAddr().IP) {
return
}
me.server.mu.Lock()
if me.server.badNodes.Test([]byte(addr.String())) {
me.server.mu.Unlock()
return
}
me.server.mu.Unlock()
me.contact(addr)
}
func (me *Announce) contact(addr Addr) {
me.numContacted++
me.triedAddrs.Add([]byte(addr.String()))
if err := me.getPeers(addr); err != nil {
log.Printf("error sending get_peers request to %s: %#v", addr, err)
return
}
me.pending++
}
func (me *Announce) transactionClosed() {
me.pending--
if me.pending == 0 {
me.close()
return
}
}
func (me *Announce) responseNode(node NodeInfo) {
me.gotNodeAddr(node.Addr)
}
func (me *Announce) closingCh() chan struct{} {
return me.stop
}
// Announce to a peer, if appropriate.
func (me *Announce) maybeAnnouncePeer(to Addr, token, peerId string) {
me.server.mu.Lock()
defer me.server.mu.Unlock()
if !me.server.config.NoSecurity {
if len(peerId) != 20 {
return
}
if !NodeIdSecure(peerId, to.UDPAddr().IP) {
return
}
}
err := me.server.announcePeer(to, me.infoHash, me.announcePort, token, me.announcePortImplied)
if err != nil {
logonce.Stderr.Printf("error announcing peer: %s", err)
}
}
func (me *Announce) getPeers(addr Addr) error {
me.server.mu.Lock()
defer me.server.mu.Unlock()
t, err := me.server.getPeers(addr, me.infoHash)
if err != nil {
return err
}
t.SetResponseHandler(func(m Msg, ok bool) {
// Register suggested nodes closer to the target info-hash.
if m.R != nil {
me.mu.Lock()
for _, n := range m.R.Nodes {
me.responseNode(n)
}
me.mu.Unlock()
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:
}
}
me.maybeAnnouncePeer(addr, m.R.Token, m.SenderID())
}
me.mu.Lock()
me.transactionClosed()
me.mu.Unlock()
})
return nil
}
// 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 {
Peers []Peer // Peers given in get_peers response.
NodeInfo // The node that gave the response.
}
// Stop the announce.
func (me *Announce) Close() {
me.mu.Lock()
defer me.mu.Unlock()
me.close()
}
func (ps *Announce) close() {
select {
case <-ps.stop:
default:
close(ps.stop)
}
}