-
-
Notifications
You must be signed in to change notification settings - Fork 621
/
tracker_client.go
352 lines (321 loc) · 8.82 KB
/
tracker_client.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package webtorrent
import (
"crypto/rand"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/anacrolix/log"
"github.com/anacrolix/torrent/tracker"
"github.com/gorilla/websocket"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v3"
)
type TrackerClientStats struct {
Dials int64
ConvertedInboundConns int64
ConvertedOutboundConns int64
}
// Client represents the webtorrent client
type TrackerClient struct {
Url string
GetAnnounceRequest func(_ tracker.AnnounceEvent, infoHash [20]byte) (tracker.AnnounceRequest, error)
PeerId [20]byte
OnConn onDataChannelOpen
Logger log.Logger
mu sync.Mutex
cond sync.Cond
outboundOffers map[string]outboundOffer // OfferID to outboundOffer
wsConn *websocket.Conn
closed bool
stats TrackerClientStats
pingTicker *time.Ticker
}
func (me *TrackerClient) Stats() TrackerClientStats {
me.mu.Lock()
defer me.mu.Unlock()
return me.stats
}
func (me *TrackerClient) peerIdBinary() string {
return binaryToJsonString(me.PeerId[:])
}
// outboundOffer represents an outstanding offer.
type outboundOffer struct {
originalOffer webrtc.SessionDescription
peerConnection *wrappedPeerConnection
dataChannel *webrtc.DataChannel
infoHash [20]byte
}
type DataChannelContext struct {
Local, Remote webrtc.SessionDescription
OfferId string
LocalOffered bool
InfoHash [20]byte
}
type onDataChannelOpen func(_ datachannel.ReadWriteCloser, dcc DataChannelContext)
func (tc *TrackerClient) doWebsocket() error {
metrics.Add("websocket dials", 1)
tc.mu.Lock()
tc.stats.Dials++
tc.mu.Unlock()
c, _, err := websocket.DefaultDialer.Dial(tc.Url, nil)
if err != nil {
return fmt.Errorf("dialing tracker: %w", err)
}
defer c.Close()
tc.Logger.WithDefaultLevel(log.Info).Printf("connected")
tc.mu.Lock()
tc.wsConn = c
tc.cond.Broadcast()
tc.mu.Unlock()
tc.announceOffers()
closeChan := make(chan struct{})
go func() {
for {
select {
case <-tc.pingTicker.C:
tc.mu.Lock()
err := c.WriteMessage(websocket.PingMessage, []byte{})
tc.mu.Unlock()
if err != nil {
return
}
case <-closeChan:
return
}
}
}()
err = tc.trackerReadLoop(tc.wsConn)
close(closeChan)
tc.mu.Lock()
c.Close()
tc.mu.Unlock()
return err
}
func (tc *TrackerClient) Run() error {
tc.pingTicker = time.NewTicker(60 * time.Second)
tc.cond.L = &tc.mu
tc.mu.Lock()
for !tc.closed {
tc.mu.Unlock()
err := tc.doWebsocket()
level := log.Info
tc.mu.Lock()
if tc.closed {
level = log.Debug
}
tc.mu.Unlock()
tc.Logger.WithDefaultLevel(level).Printf("websocket instance ended: %v", err)
time.Sleep(time.Minute)
tc.mu.Lock()
}
tc.mu.Unlock()
return nil
}
func (tc *TrackerClient) Close() error {
tc.mu.Lock()
tc.closed = true
if tc.wsConn != nil {
tc.wsConn.Close()
}
tc.closeUnusedOffers()
tc.pingTicker.Stop()
tc.mu.Unlock()
tc.cond.Broadcast()
return nil
}
func (tc *TrackerClient) announceOffers() {
// tc.Announce grabs a lock on tc.outboundOffers. It also handles the case where outboundOffers
// is nil. Take ownership of outboundOffers here.
tc.mu.Lock()
offers := tc.outboundOffers
tc.outboundOffers = nil
tc.mu.Unlock()
if offers == nil {
return
}
// Iterate over our locally-owned offers, close any existing "invalid" ones from before the
// socket reconnected, reannounce the infohash, adding it back into the tc.outboundOffers.
tc.Logger.WithDefaultLevel(log.Info).Printf("reannouncing %d infohashes after restart", len(offers))
for _, offer := range offers {
// TODO: Capture the errors? Are we even in a position to do anything with them?
offer.peerConnection.Close()
// Use goroutine here to allow read loop to start and ensure the buffer drains.
go tc.Announce(tracker.Started, offer.infoHash)
}
}
func (tc *TrackerClient) closeUnusedOffers() {
for _, offer := range tc.outboundOffers {
offer.peerConnection.Close()
}
tc.outboundOffers = nil
}
func (tc *TrackerClient) Announce(event tracker.AnnounceEvent, infoHash [20]byte) error {
metrics.Add("outbound announces", 1)
var randOfferId [20]byte
_, err := rand.Read(randOfferId[:])
if err != nil {
return fmt.Errorf("generating offer_id bytes: %w", err)
}
offerIDBinary := binaryToJsonString(randOfferId[:])
pc, dc, offer, err := newOffer()
if err != nil {
return fmt.Errorf("creating offer: %w", err)
}
request, err := tc.GetAnnounceRequest(event, infoHash)
if err != nil {
pc.Close()
return fmt.Errorf("getting announce parameters: %w", err)
}
req := AnnounceRequest{
Numwant: 1, // If higher we need to create equal amount of offers.
Uploaded: request.Uploaded,
Downloaded: request.Downloaded,
Left: request.Left,
Event: request.Event.String(),
Action: "announce",
InfoHash: binaryToJsonString(infoHash[:]),
PeerID: tc.peerIdBinary(),
Offers: []Offer{{
OfferID: offerIDBinary,
Offer: offer,
}},
}
data, err := json.Marshal(req)
if err != nil {
pc.Close()
return fmt.Errorf("marshalling request: %w", err)
}
tc.mu.Lock()
defer tc.mu.Unlock()
err = tc.writeMessage(data)
if err != nil {
pc.Close()
return fmt.Errorf("write AnnounceRequest: %w", err)
}
if tc.outboundOffers == nil {
tc.outboundOffers = make(map[string]outboundOffer)
}
tc.outboundOffers[offerIDBinary] = outboundOffer{
peerConnection: pc,
dataChannel: dc,
originalOffer: offer,
infoHash: infoHash,
}
return nil
}
func (tc *TrackerClient) writeMessage(data []byte) error {
for tc.wsConn == nil {
if tc.closed {
return fmt.Errorf("%T closed", tc)
}
tc.cond.Wait()
}
return tc.wsConn.WriteMessage(websocket.TextMessage, data)
}
func (tc *TrackerClient) trackerReadLoop(tracker *websocket.Conn) error {
for {
_, message, err := tracker.ReadMessage()
if err != nil {
return fmt.Errorf("read message error: %w", err)
}
//tc.Logger.WithDefaultLevel(log.Debug).Printf("received message from tracker: %q", message)
var ar AnnounceResponse
if err := json.Unmarshal(message, &ar); err != nil {
tc.Logger.WithDefaultLevel(log.Warning).Printf("error unmarshalling announce response: %v", err)
continue
}
switch {
case ar.Offer != nil:
ih, err := jsonStringToInfoHash(ar.InfoHash)
if err != nil {
tc.Logger.WithDefaultLevel(log.Warning).Printf("error decoding info_hash in offer: %v", err)
break
}
tc.handleOffer(*ar.Offer, ar.OfferID, ih, ar.PeerID)
case ar.Answer != nil:
tc.handleAnswer(ar.OfferID, *ar.Answer)
}
}
}
func (tc *TrackerClient) handleOffer(
offer webrtc.SessionDescription,
offerId string,
infoHash [20]byte,
peerId string,
) error {
peerConnection, answer, err := newAnsweringPeerConnection(offer)
if err != nil {
return fmt.Errorf("write AnnounceResponse: %w", err)
}
response := AnnounceResponse{
Action: "announce",
InfoHash: binaryToJsonString(infoHash[:]),
PeerID: tc.peerIdBinary(),
ToPeerID: peerId,
Answer: &answer,
OfferID: offerId,
}
data, err := json.Marshal(response)
if err != nil {
peerConnection.Close()
return fmt.Errorf("marshalling response: %w", err)
}
tc.mu.Lock()
defer tc.mu.Unlock()
if err := tc.writeMessage(data); err != nil {
peerConnection.Close()
return fmt.Errorf("writing response: %w", err)
}
timer := time.AfterFunc(30*time.Second, func() {
metrics.Add("answering peer connections timed out", 1)
peerConnection.Close()
})
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
setDataChannelOnOpen(d, peerConnection, func(dc datachannel.ReadWriteCloser) {
timer.Stop()
metrics.Add("answering peer connection conversions", 1)
tc.mu.Lock()
tc.stats.ConvertedInboundConns++
tc.mu.Unlock()
tc.OnConn(dc, DataChannelContext{
Local: answer,
Remote: offer,
OfferId: offerId,
LocalOffered: false,
InfoHash: infoHash,
})
})
})
return nil
}
func (tc *TrackerClient) handleAnswer(offerId string, answer webrtc.SessionDescription) {
tc.mu.Lock()
defer tc.mu.Unlock()
offer, ok := tc.outboundOffers[offerId]
if !ok {
tc.Logger.WithDefaultLevel(log.Warning).Printf("could not find offer for id %q", offerId)
return
}
//tc.Logger.WithDefaultLevel(log.Debug).Printf("offer %q got answer %v", offerId, answer)
metrics.Add("outbound offers answered", 1)
err := offer.setAnswer(answer, func(dc datachannel.ReadWriteCloser) {
metrics.Add("outbound offers answered with datachannel", 1)
tc.mu.Lock()
tc.stats.ConvertedOutboundConns++
tc.mu.Unlock()
tc.OnConn(dc, DataChannelContext{
Local: offer.originalOffer,
Remote: answer,
OfferId: offerId,
LocalOffered: true,
InfoHash: offer.infoHash,
})
})
if err != nil {
tc.Logger.WithDefaultLevel(log.Warning).Printf("error using outbound offer answer: %v", err)
return
}
delete(tc.outboundOffers, offerId)
go tc.Announce(tracker.None, offer.infoHash)
}