forked from anacrolix/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
announce.go
214 lines (188 loc) · 5.17 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
package dht
// get_peers and announce_peers.
import (
"context"
"fmt"
"sync"
"sync/atomic"
"github.com/anacrolix/chansync"
"github.com/anacrolix/chansync/events"
"github.com/anacrolix/log"
"github.com/anacrolix/dht/v2/int160"
dhtutil "github.com/anacrolix/dht/v2/k-nearest-nodes"
"github.com/anacrolix/dht/v2/krpc"
"github.com/anacrolix/dht/v2/traversal"
)
// Maintains state for an ongoing Announce operation. An Announce is started by calling
// Server.Announce.
type Announce struct {
Peers chan PeersValues
server *Server
infoHash int160.T // Target
announcePeerOpts *AnnouncePeerOpts
scrape bool
peerAnnounced chansync.SetOnce
traversal *traversal.Operation
closed chansync.SetOnce
}
func (a *Announce) String() string {
return fmt.Sprintf("%[1]T %[1]p of %v on %v", a, a.infoHash, a.server)
}
// Returns the number of distinct remote addresses the announce has queried.
func (a *Announce) NumContacted() uint32 {
return atomic.LoadUint32(&a.traversal.Stats().NumAddrsTried)
}
func (a *Announce) TraversalStats() TraversalStats {
return *a.traversal.Stats()
}
// Server.Announce option
type AnnounceOpt func(a *Announce)
// Scrape BEP 33 bloom filters in queries.
func Scrape() AnnounceOpt {
return func(a *Announce) {
a.scrape = true
}
}
// Arguments for announce_peer from a Server.Announce.
type AnnouncePeerOpts struct {
// The peer port that we're announcing.
Port int
// The peer port should be determined by the receiver to be the source port of the query packet.
ImpliedPort bool
}
// Finish an Announce get_peers traversal with an announce of a local peer.
func AnnouncePeer(opts AnnouncePeerOpts) AnnounceOpt {
return func(a *Announce) {
a.announcePeerOpts = &opts
}
}
// Deprecated: Use Server.AnnounceTraversal.
// Traverses the DHT graph toward nodes that store peers for the infohash, streaming them to the
// caller, and announcing the local node to each responding node if port is non-zero or impliedPort
// is true.
func (s *Server) Announce(infoHash [20]byte, port int, impliedPort bool, opts ...AnnounceOpt) (_ *Announce, err error) {
if port != 0 || impliedPort {
opts = append([]AnnounceOpt{AnnouncePeer(AnnouncePeerOpts{
Port: port,
ImpliedPort: impliedPort,
})}, opts...)
}
return s.AnnounceTraversal(infoHash, opts...)
}
// Traverses the DHT graph toward nodes that store peers for the infohash, streaming them to the
// caller.
func (s *Server) AnnounceTraversal(infoHash [20]byte, opts ...AnnounceOpt) (_ *Announce, err error) {
infoHashInt160 := int160.FromByteArray(infoHash)
a := &Announce{
Peers: make(chan PeersValues),
server: s,
infoHash: infoHashInt160,
}
for _, opt := range opts {
opt(a)
}
a.traversal = traversal.Start(traversal.OperationInput{
Target: infoHash,
DoQuery: a.getPeers,
NodeFilter: s.TraversalNodeFilter,
DataFilter: func(data any) bool {
_, ok := data.(string)
return ok
},
})
nodes, err := s.TraversalStartingNodes()
if err != nil {
a.traversal.Stop()
return
}
a.traversal.AddNodes(nodes)
go func() {
<-a.traversal.Stalled()
a.traversal.Stop()
<-a.traversal.Stopped()
if a.announcePeerOpts != nil {
a.announceClosest()
}
a.peerAnnounced.Set()
close(a.Peers)
}()
return a, nil
}
func (a *Announce) announceClosest() {
var wg sync.WaitGroup
a.traversal.Closest().Range(func(elem dhtutil.Elem) {
wg.Add(1)
go func() {
a.logger().Levelf(log.Debug,
"announce_peer to %v: %v",
elem, a.announcePeer(elem),
)
wg.Done()
}()
})
wg.Wait()
}
func (a *Announce) announcePeer(peer dhtutil.Elem) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
select {
case <-a.closed.Done():
cancel()
case <-ctx.Done():
}
}()
return a.server.announcePeer(
ctx,
NewAddr(peer.Addr.UDP()),
a.infoHash,
a.announcePeerOpts.Port,
peer.Data.(string),
a.announcePeerOpts.ImpliedPort,
QueryRateLimiting{},
).Err
}
func (a *Announce) getPeers(ctx context.Context, addr krpc.NodeAddr) traversal.QueryResult {
res := a.server.GetPeers(ctx, NewAddr(addr.UDP()), a.infoHash, a.scrape, QueryRateLimiting{})
if r := res.Reply.R; r != nil {
peersValues := PeersValues{
Peers: r.Values,
NodeInfo: krpc.NodeInfo{
Addr: addr,
ID: r.ID,
},
Return: *r,
}
select {
case a.Peers <- peersValues:
case <-a.traversal.Stopped():
}
}
return res.TraversalQueryResult(addr)
}
// 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.
krpc.NodeInfo // The node that gave the response.
krpc.Return
}
// Stop the announce.
func (a *Announce) Close() {
a.StopTraversing()
// This will prevent peer announces from proceeding.
a.closed.Set()
}
func (a *Announce) logger() log.Logger {
return a.server.logger()
}
// Halts traversal, but won't block peer announcing.
func (a *Announce) StopTraversing() {
a.traversal.Stop()
}
// Traversal and peer announcing steps are done.
func (a *Announce) Finished() events.Done {
// This is the last step in an announce.
return a.peerAnnounced.Done()
}