This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
peerconn.go
516 lines (444 loc) · 12 KB
/
peerconn.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package dctk
import (
"crypto/tls"
"fmt"
"net"
"time"
"github.com/aler9/go-dc/adc"
"github.com/aler9/go-dc/nmdc"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/protoadc"
"github.com/aler9/dctk/pkg/protocommon"
"github.com/aler9/dctk/pkg/protonmdc"
)
var errorDelegatedUpload = fmt.Errorf("delegated upload")
type nickDirectionPair struct {
nick string
direction string
}
type peerConn struct {
client *Client
isEncrypted bool
isActive bool
terminateRequested bool
terminate chan struct{}
state string
conn conn
tlsConn *tls.Conn
adcToken string
passiveIP string
passivePort uint
peer *Peer
localDirection string
localBet uint
remoteIsUpload bool
remoteBet uint
direction string
transfer transfer
}
func newPeerConn(client *Client, isEncrypted bool, isActive bool,
rawconn net.Conn, ip string, port uint, adcToken string,
) *peerConn {
p := &peerConn{
client: client,
isEncrypted: isEncrypted,
isActive: isActive,
terminate: make(chan struct{}),
adcToken: adcToken,
}
p.client.peerConns[p] = struct{}{}
if isActive {
log.Log(client.conf.LogLevel, log.LevelInfo, "[peer] incoming %s%s", rawconn.RemoteAddr(), func() string {
if p.isEncrypted {
return " (secure)"
}
return ""
}())
p.state = "connected"
if p.isEncrypted {
p.tlsConn = rawconn.(*tls.Conn)
}
if client.protoIsAdc() {
p.conn = protoadc.NewConn(p.client.conf.LogLevel, "p", rawconn, true, true)
} else {
p.conn = protonmdc.NewConn(p.client.conf.LogLevel, "p", rawconn, true, true)
}
} else {
log.Log(client.conf.LogLevel, log.LevelInfo, "[peer] outgoing %s:%d%s", ip, port, func() string {
if p.isEncrypted {
return " (secure)"
}
return ""
}())
p.state = "connecting"
p.passiveIP = ip
p.passivePort = port
}
p.client.wg.Add(1)
go p.do()
return p
}
func (p *peerConn) close() {
if p.terminateRequested {
return
}
p.terminateRequested = true
close(p.terminate)
}
func (p *peerConn) do() {
defer p.client.wg.Done()
err := func() error {
// connect to peer
connect := false
p.client.Safe(func() {
if p.state == "connecting" {
connect = true
}
})
if connect {
ce := newConnEstablisher(
fmt.Sprintf("%s:%d", p.passiveIP, p.passivePort),
10*time.Second, 3)
select {
case <-p.terminate:
return protocommon.ErrorTerminated
case <-ce.Wait:
}
if ce.Error != nil {
return ce.Error
}
rawconn := ce.Conn
if p.isEncrypted {
p.tlsConn = tls.Client(rawconn, &tls.Config{InsecureSkipVerify: true})
rawconn = p.tlsConn
}
if p.client.protoIsAdc() {
p.conn = protoadc.NewConn(p.client.conf.LogLevel, "p", rawconn, true, true)
} else {
p.conn = protonmdc.NewConn(p.client.conf.LogLevel, "p", rawconn, true, true)
}
p.client.Safe(func() {
p.state = "connected"
})
log.Log(p.client.conf.LogLevel, log.LevelInfo, "[peer] connected %s%s", rawconn.RemoteAddr(),
func() string {
if p.isEncrypted {
return " (secure)"
}
return ""
}())
// if transfer is passive, we are the first to talk
if p.client.protoIsAdc() {
p.conn.Write(&protoadc.AdcCSupports{ //nolint:govet
&adc.ClientPacket{},
&adc.Supported{adc.ModFeatures{ //nolint:govet
adc.FeaBAS0: true,
adc.FeaBASE: true,
adc.FeaTIGR: true,
adc.FeaBZIP: true,
adc.FeaZLIG: true,
}},
})
} else {
p.conn.Write(&nmdc.MyNick{Name: nmdc.Name(p.client.conf.Nick)})
p.conn.Write(&nmdc.Lock{
Lock: "EXTENDEDPROTOCOLABCABCABCABCABCABC",
PK: p.client.conf.PkValue,
Ref: fmt.Sprintf("%s:%d", p.client.hubSolvedIP, p.client.hubPort),
})
}
}
readDone := make(chan error)
go func() {
readDone <- func() error {
for {
msg, err := p.conn.Read()
if err != nil {
return err
}
p.client.Safe(func() {
// pre-transfer
if p.state != "delegated_download" {
err = p.handleMessage(msg)
// download
} else {
d := p.transfer.(*Download)
err = d.handleDownload(msg)
if err == protocommon.ErrorTerminated {
p.transfer = nil
p.state = "wait_download"
d.handleExit(nil)
err = nil // do not close connection
}
}
})
// upload
if err == errorDelegatedUpload {
u := p.transfer.(*upload)
err := u.handleUpload()
if err != nil {
return err
}
p.client.Safe(func() {
p.transfer = nil
p.state = "wait_upload"
u.handleExit(nil)
})
} else if err != nil {
return err
}
}
}()
}()
select {
case <-p.terminate:
p.conn.Close()
<-readDone
return protocommon.ErrorTerminated
case err := <-readDone:
p.conn.Close()
return err
}
}()
p.client.Safe(func() {
if !p.terminateRequested {
log.Log(p.client.conf.LogLevel, log.LevelInfo, "ERR (peerConn): %s", err)
}
// transfer abruptly interrupted, doesnt care if the conn was terminated or not
switch p.state {
case "delegated_upload", "delegated_download":
p.transfer.handleExit(err)
}
if p.conn != nil {
p.conn.Close()
}
delete(p.client.peerConns, p)
if p.peer != nil && p.direction != "" {
delete(p.client.peerConnsByKey, nickDirectionPair{p.peer.Nick, p.direction})
}
log.Log(p.client.conf.LogLevel, log.LevelInfo, "[peer] disconnected")
})
}
func (p *peerConn) handleMessage(msgi protocommon.MsgDecodable) error {
switch msg := msgi.(type) {
case *protoadc.AdcCStatus:
if msg.Msg.Sev != adc.Success {
return fmt.Errorf("error (%d): %s", msg.Msg.Code, msg.Msg.Msg)
}
case *protoadc.AdcCSupports:
if p.state != "connected" {
return fmt.Errorf("[Supports] invalid state: %s", p.state)
}
p.state = "supports"
if p.isActive {
p.conn.Write(&protoadc.AdcCSupports{ //nolint:govet
&adc.ClientPacket{},
&adc.Supported{adc.ModFeatures{ //nolint:govet
adc.FeaBAS0: true,
adc.FeaBASE: true,
adc.FeaTIGR: true,
adc.FeaBZIP: true,
adc.FeaZLIG: true,
}},
})
} else {
info := &adc.UserInfo{}
info.Id = p.client.clientID
info.Token = p.adcToken
p.conn.Write(&protoadc.AdcCInfos{ //nolint:govet
&adc.ClientPacket{},
info,
})
}
case *protoadc.AdcCInfos:
if p.state != "supports" {
return fmt.Errorf("[Infos] invalid state: %s", p.state)
}
p.state = "infos"
p.peer = p.client.peerByClientID(msg.Msg.Id)
if p.peer == nil {
return fmt.Errorf("unknown client id (%s)", msg.Msg.Id)
}
if p.isActive {
if msg.Msg.Token == "" {
return fmt.Errorf("token not provided")
}
p.adcToken = msg.Msg.Token
info := &adc.UserInfo{}
info.Id = p.client.clientID
// token is not sent back when in active mode
p.conn.Write(&protoadc.AdcCInfos{ //nolint:govet
&adc.ClientPacket{},
info,
})
// validate peer fingerprint
// can be performed on client-side only since many clients do not send
// their certificate when in passive mode
} else if p.client.protoIsAdc() && p.isEncrypted &&
p.peer.adcFingerprint != "" {
connFingerprint := protoadc.AdcCertFingerprint(
p.tlsConn.ConnectionState().PeerCertificates[0])
if connFingerprint != p.peer.adcFingerprint {
return fmt.Errorf("unable to validate peer fingerprint (%s vs %s)",
connFingerprint, p.peer.adcFingerprint)
}
log.Log(p.client.conf.LogLevel, log.LevelInfo, "[peer] fingerprint validated")
}
dl := p.client.downloadByAdcToken(p.adcToken)
if dl != nil {
key := nickDirectionPair{p.peer.Nick, "download"}
if _, ok := p.client.peerConnsByKey[key]; ok {
return fmt.Errorf("a connection with this peer and direction already exists")
}
p.client.peerConnsByKey[key] = p
p.direction = "download"
p.state = "delegated_download"
p.transfer = dl
dl.pconn = p
dl.state = "processing"
dl.peerChan <- struct{}{}
} else {
key := nickDirectionPair{p.peer.Nick, "upload"}
if _, ok := p.client.peerConnsByKey[key]; ok {
return fmt.Errorf("a connection with this peer and direction already exists")
}
p.client.peerConnsByKey[key] = p
p.direction = "upload"
p.state = "wait_upload"
}
case *protoadc.AdcCGetFile:
if p.state != "wait_upload" {
return fmt.Errorf("[AdcGet] invalid state: %s", p.state)
}
query := msg.Msg.Type + " " + msg.Msg.Path
ok := newUpload(p.client, p, query, uint64(msg.Msg.Start),
msg.Msg.Bytes, msg.Msg.Compressed)
if ok {
return errorDelegatedUpload
}
case *nmdc.MyNick:
if p.state != "connected" {
return fmt.Errorf("[MyNick] invalid state: %s", p.state)
}
p.state = "mynick"
p.peer = p.client.peerByNick(string(msg.Name))
if p.peer == nil {
return fmt.Errorf("peer not connected to hub (%s)", msg.Name)
}
case *nmdc.Lock:
if p.state != "mynick" {
return fmt.Errorf("[Lock] invalid state: %s", p.state)
}
p.state = "lock"
// if transfer is active, wait remote before sending MyNick and Lock
if p.isActive {
p.conn.Write(&nmdc.MyNick{Name: nmdc.Name(p.client.conf.Nick)})
p.conn.Write(&nmdc.Lock{
Lock: "EXTENDEDPROTOCOLABCABCABCABCABCABC",
PK: p.client.conf.PkValue,
})
}
features := []string{
nmdc.ExtMinislots,
nmdc.ExtXmlBZList,
nmdc.ExtADCGet,
nmdc.ExtTTHL,
nmdc.ExtTTHF,
}
if !p.client.conf.PeerDisableCompression {
features = append(features, nmdc.ExtZLIG)
}
p.conn.Write(&nmdc.Supports{features}) //nolint:govet
p.localBet = uint(randomInt(1, 0x7FFF))
// try download
if p.client.downloadPendingByPeer(p.peer) != nil {
p.localDirection = "download"
p.conn.Write(&nmdc.Direction{
Upload: false,
Number: p.localBet,
})
// upload
} else {
p.localDirection = "upload"
p.conn.Write(&nmdc.Direction{
Upload: true,
Number: p.localBet,
})
}
p.conn.Write(msg.Key())
case *nmdc.Supports:
if p.state != "lock" {
return fmt.Errorf("[Supports] invalid state: %s", p.state)
}
p.state = "supports"
case *nmdc.Direction:
if p.state != "supports" {
return fmt.Errorf("[Direction] invalid state: %s", p.state)
}
p.state = "direction"
p.remoteIsUpload = msg.Upload
p.remoteBet = msg.Number
case *nmdc.Key:
if p.state != "direction" {
return fmt.Errorf("[Key] invalid state: %s", p.state)
}
p.state = "key"
var direction string
switch {
case p.localDirection == "upload" && !p.remoteIsUpload:
direction = "upload"
case p.localDirection == "download" && p.remoteIsUpload:
direction = "download"
case p.localDirection == "download" && !p.remoteIsUpload:
switch {
// bet win
case p.localBet > p.remoteBet:
direction = "download"
// bet lost
case p.localBet < p.remoteBet:
direction = "upload"
// if there's a pending download, request another connection
if dl := p.client.downloadPendingByPeer(p.peer); dl != nil {
p.client.peerRequestConnection(dl.conf.Peer, "")
}
default:
return fmt.Errorf("equal random numbers")
}
default:
return fmt.Errorf("double upload request")
}
key := nickDirectionPair{p.peer.Nick, direction}
if _, ok := p.client.peerConnsByKey[key]; ok {
return fmt.Errorf("a connection with this peer and direction already exists")
}
p.client.peerConnsByKey[key] = p
p.direction = direction
// upload
if p.direction == "upload" {
p.state = "wait_upload"
// download
} else {
dl := p.client.downloadPendingByPeer(p.peer)
if dl == nil {
return fmt.Errorf("download connection but cannot find download")
}
p.state = "delegated_download"
p.transfer = dl
dl.pconn = p
dl.state = "processing"
dl.peerChan <- struct{}{}
}
case *nmdc.ADCGet:
if p.state != "wait_upload" {
return fmt.Errorf("[AdcGet] invalid state: %s", p.state)
}
query := string(msg.ContentType) + " " + string(msg.Identifier)
ok := newUpload(p.client, p, query, msg.Start, msg.Length, msg.Compressed)
if ok {
return errorDelegatedUpload
}
default:
return fmt.Errorf("unhandled: %T %+v", msgi, msgi)
}
return nil
}