-
Notifications
You must be signed in to change notification settings - Fork 9
/
client2hub.go
622 lines (565 loc) · 14 KB
/
client2hub.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package client
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"strconv"
"sync"
"time"
adcp "github.com/direct-connect/go-dc/adc"
"github.com/direct-connect/go-dcpp/adc"
"github.com/direct-connect/go-dcpp/version"
)
// DialHub connects to a hub and runs a handshake.
func DialHub(addr string, info *Config) (*Conn, error) {
conn, err := adc.Dial(addr)
if err != nil {
return nil, err
}
return HubHandshake(conn, info)
}
type Config struct {
PID adc.PID
Name string
Extensions adcp.ExtFeatures
}
func (c *Config) validate() error {
if c.PID.IsZero() {
return errors.New("PID should not be empty")
}
if c.Name == "" {
return errors.New("name should be set")
}
return nil
}
// HubHandshake begins a Client-Hub handshake on a connection.
func HubHandshake(conn *adc.Conn, conf *Config) (*Conn, error) {
if err := conf.validate(); err != nil {
return nil, err
}
sid, mutual, err := protocolToHub(conn)
if err != nil {
_ = conn.Close()
return nil, err
}
c := &Conn{
conn: conn,
sid: sid, pid: conf.PID,
fea: mutual,
closing: make(chan struct{}),
closed: make(chan struct{}),
}
c.user.Pid = &conf.PID
c.user.Name = conf.Name
c.user.Features = conf.Extensions
c.user.Slots = 1
if err := identifyToHub(conn, sid, &c.user); err != nil {
conn.Close()
return nil, err
}
c.ext = make(map[adcp.Feature]struct{})
for _, ext := range c.user.Features {
c.ext[ext] = struct{}{}
}
if err := c.acceptUsersList(); err != nil {
conn.Close()
return nil, err
}
//c.conn.KeepAlive(time.Minute / 2)
go c.readLoop()
return c, nil
}
func protocolToHub(conn *adc.Conn) (adc.SID, adcp.ModFeatures, error) {
ourFeatures := adcp.ModFeatures{
// should always be set for ADC
adcp.FeaBASE: true,
adcp.FeaBAS0: true,
adcp.FeaTIGR: true,
// extensions
// TODO: some hubs will stop the handshake after sending the hub info
// if this extension is specified
//adc.FeaPING: true,
adcp.FeaBZIP: true,
// TODO: ZLIG
}
// Send supported features (SUP), initiating the PROTOCOL state.
// We expect SUP followed by SID to transition to IDENTIFY.
//
// https://adc.sourceforge.io/ADC.html#_protocol
err := conn.WriteHubMsg(adcp.Supported{
Features: ourFeatures,
})
if err != nil {
return adc.SID{}, nil, err
}
if err := conn.Flush(); err != nil {
return adc.SID{}, nil, err
}
// shouldn't take longer than this
deadline := time.Now().Add(time.Second * 5)
// first, we expect a SUP from the hub with a list of supported features
msg, err := conn.ReadInfoMsg(deadline)
if err != nil {
return adc.SID{}, nil, err
}
sup, ok := msg.(adcp.Supported)
if !ok {
return adc.SID{}, nil, fmt.Errorf("expected SUP command, got: %#v", msg)
}
hubFeatures := sup.Features
// check mutual features
mutual := ourFeatures.Intersect(hubFeatures)
if !mutual.IsSet(adcp.FeaBASE) && !mutual.IsSet(adcp.FeaBAS0) {
return adc.SID{}, nil, fmt.Errorf("hub does not support BASE")
} else if !mutual.IsSet(adcp.FeaTIGR) {
return adc.SID{}, nil, fmt.Errorf("hub does not support TIGR")
}
// next, we expect a SID that will assign a Session ID
msg, err = conn.ReadInfoMsg(deadline)
if err != nil {
return adc.SID{}, nil, err
}
sid, ok := msg.(adcp.SIDAssign)
if !ok {
return adc.SID{}, nil, fmt.Errorf("expected SID command, got: %#v", msg)
}
return sid.SID, mutual, nil
}
func identifyToHub(conn *adc.Conn, sid adc.SID, user *adcp.UserInfo) error {
// Hub may send INF, but it's not required.
// The client should broadcast INF with PD/ID and other required fields.
//
// https://adc.sourceforge.io/ADC.html#_identify
if user.Id.IsZero() {
user.Id = user.Pid.Hash()
}
if user.Application == "" {
user.Application = version.Name
user.Version = version.Vers
}
for _, f := range []adcp.Feature{adcp.FeaSEGA, adcp.FeaTCP4} {
if !user.Features.Has(f) {
user.Features = append(user.Features, f)
}
}
err := conn.WriteBroadcast(sid, user)
if err != nil {
return err
}
if err := conn.Flush(); err != nil {
return err
}
// TODO: registered user
return nil
}
// Conn represents a Client-to-Hub connection.
type Conn struct {
conn *adc.Conn
fea adcp.ModFeatures
closing chan struct{}
closed chan struct{}
pid adc.PID
sid adc.SID
user adcp.UserInfo
ext map[adcp.Feature]struct{}
hub adcp.HubInfo
peers struct {
sync.RWMutex
// keeps both online and offline users
byCID map[adc.CID]*Peer
// only keeps online users
bySID map[adc.SID]*Peer
}
revConn struct {
sync.Mutex
tokens map[string]revConnToken
}
}
type revConnToken struct {
cid adc.CID
cancel <-chan struct{}
addr chan string
errc chan error
}
// PID returns Private ID associated with this connection.
func (c *Conn) PID() adc.PID { return c.pid }
// CID returns Client ID associated with this connection.
func (c *Conn) CID() adc.CID { return c.user.Id }
// SID returns Session ID associated with this connection.
// Only valid after a Client-Hub handshake.
func (c *Conn) SID() adc.SID { return c.sid }
// Hub returns hub information.
func (c *Conn) Hub() adcp.HubInfo { return c.hub }
// Features returns a set of negotiated features.
func (c *Conn) Features() adcp.ModFeatures { return c.fea.Clone() }
func (c *Conn) Close() error {
select {
case <-c.closing:
<-c.closed
return nil
default:
}
close(c.closing)
err := c.conn.Close()
<-c.closed
return err
}
func (c *Conn) writeDirect(to adc.SID, msg adcp.Message) error {
if err := c.conn.WriteDirect(c.SID(), to, msg); err != nil {
return err
}
return c.conn.Flush()
}
func (c *Conn) revConnToken(ctx context.Context, cid adc.CID) (token string, addr <-chan string, _ <-chan error) {
ch := make(chan string, 1)
errc := make(chan error, 1)
for {
tok := strconv.Itoa(rand.Int())
c.revConn.Lock()
_, ok := c.revConn.tokens[tok]
if !ok {
if c.revConn.tokens == nil {
c.revConn.tokens = make(map[string]revConnToken)
}
c.revConn.tokens[tok] = revConnToken{cancel: ctx.Done(), cid: cid, addr: ch, errc: errc}
c.revConn.Unlock()
return tok, ch, errc
}
c.revConn.Unlock()
// collision, pick another token
}
}
func (c *Conn) readLoop() {
defer close(c.closed)
for {
cmd, err := c.conn.ReadPacketRaw(time.Time{})
if err != nil {
log.Println(err)
return
}
switch cmd := cmd.(type) {
case *adcp.BroadcastPacket:
if err := c.handleBroadcast(cmd); err != nil {
log.Println(err)
return
}
case *adcp.InfoPacket:
if err := c.handleInfo(cmd); err != nil {
log.Println(err)
return
}
case *adcp.FeaturePacket:
if err := c.handleFeature(cmd); err != nil {
log.Println(err)
return
}
case *adcp.DirectPacket:
// TODO: ADC flaw: why ever send the client his own SID? hub should append it instead
// same for the sending party
if cmd.To != c.SID() {
log.Println("direct command to a wrong destination:", cmd.To)
return
}
if err := c.handleDirect(cmd); err != nil {
log.Println(err)
return
}
default:
log.Printf("unhandled command: %T", cmd)
}
}
}
func (c *Conn) handleBroadcast(p *adcp.BroadcastPacket) error {
// we could decode the message and type-switch, but for cases
// below it's better to decode later
switch p.Msg.Cmd() {
case (adcp.UserInfo{}).Cmd():
// easier to merge while decoding
return c.peerUpdate(p.ID, p)
case (adcp.SearchRequest{}).Cmd():
peer := c.peerBySID(p.ID)
c.handleSearch(peer, p)
return nil
// TODO: MSG
default:
log.Printf("unhandled broadcast command: %v", p.Msg.Cmd())
return nil
}
}
func (c *Conn) handleInfo(p *adcp.InfoPacket) error {
err := p.DecodeMessage()
if err != nil {
return err
}
switch msg := p.Msg.(type) {
case adcp.ChatMessage:
// TODO: ADC: maybe hub should take a AAAA SID for itself
// and this will become B-MSG AAAA, instead of I-MSG
fmt.Printf("%s\n", msg.Text)
return nil
case adcp.Disconnect:
// TODO: ADC flaw: this should be B-QUI, not I-QUI
// it always includes a SID and is, in fact, a broadcast
return c.peerQuit(msg.ID)
default:
log.Printf("unhandled info command: %v", p.Msg.Cmd())
return nil
}
}
func (c *Conn) handleFeature(cmd *adcp.FeaturePacket) error {
// TODO: ADC protocol: this is another B-XXX command, but with a feature selector
// might be a good idea to extend selector with some kind of tags
// it may work for extensions, geo regions, chat channels, etc
// TODO: ADC flaw: shouldn't the hub convert F-XXX to B-XXX if the current client
// supports all listed extensions? does the client care about the selector?
for _, f := range cmd.Sel {
if _, enabled := c.ext[f.Fea]; enabled != f.Sel {
return nil
}
}
// FIXME: this allows F-MSG that we should probably avoid
return c.handleBroadcast(&adcp.BroadcastPacket{
ID: cmd.ID, Msg: cmd.Msg,
})
}
func (c *Conn) handleDirect(cmd *adcp.DirectPacket) error {
err := cmd.DecodeMessage()
if err != nil {
return err
}
switch msg := cmd.Msg.(type) {
case adcp.ConnectRequest:
c.revConn.Lock()
tok, ok := c.revConn.tokens[msg.Token]
delete(c.revConn.tokens, msg.Token)
c.revConn.Unlock()
if !ok {
// TODO: handle a direct connection request from peers
log.Printf("ignoring connection attempt from %v", cmd.ID)
return nil
}
p := c.peerBySID(cmd.ID)
go c.handleConnReq(p, tok, msg)
return nil
default:
log.Printf("unhandled direct command: %v", cmd.Msg.Cmd())
return nil
}
}
func (c *Conn) handleConnReq(p *Peer, tok revConnToken, s adcp.ConnectRequest) {
if p == nil {
tok.errc <- ErrPeerOffline
return
}
if s.Proto != adcp.ProtoADC {
tok.errc <- fmt.Errorf("unsupported protocol: %q", s.Proto)
return
}
if s.Port == 0 {
tok.errc <- errors.New("no port to connect to")
return
}
addr := p.Info().Ip4
if addr == "" {
tok.errc <- errors.New("no address to connect to")
return
}
tok.addr <- addr + ":" + strconv.Itoa(s.Port)
}
func (c *Conn) handleSearch(p *Peer, pck adcp.Packet) {
var sch adcp.SearchRequest
if err := pck.DecodeMessageTo(&sch); err != nil {
log.Println("failed to decode search:", err)
return
}
log.Printf("search: %+v", sch)
}
func (c *Conn) OnlinePeers() []*Peer {
c.peers.RLock()
defer c.peers.RUnlock()
arr := make([]*Peer, 0, len(c.peers.bySID))
for _, p := range c.peers.bySID {
arr = append(arr, p)
}
return arr
}
func (c *Conn) peerBySID(sid adc.SID) *Peer {
c.peers.RLock()
p := c.peers.bySID[sid]
c.peers.RUnlock()
return p
}
func (c *Conn) peerJoins(sid adc.SID, u adcp.UserInfo) *Peer {
c.peers.Lock()
defer c.peers.Unlock()
if c.peers.byCID == nil {
c.peers.bySID = make(map[adc.SID]*Peer)
c.peers.byCID = make(map[adc.CID]*Peer)
}
p, ok := c.peers.byCID[u.Id]
if ok {
c.peers.bySID[sid] = p
p.online(sid)
p.update(u)
return p
}
p = &Peer{hub: c, user: &u}
c.peers.bySID[sid] = p
c.peers.byCID[u.Id] = p
p.online(sid)
return p
}
func (c *Conn) peerQuit(sid adc.SID) error {
c.peers.Lock()
defer c.peers.Unlock()
p := c.peers.bySID[sid]
if p == nil {
return fmt.Errorf("unknown user quits: %v", sid)
}
p.offline()
delete(c.peers.bySID, sid)
return nil
}
func (c *Conn) peerUpdate(sid adc.SID, pck adcp.Packet) error {
c.peers.Lock()
p, ok := c.peers.bySID[sid]
if ok {
c.peers.Unlock()
p.mu.Lock()
defer p.mu.Unlock()
return pck.DecodeMessageTo(p.user)
}
defer c.peers.Unlock()
var u adcp.UserInfo
if err := pck.DecodeMessageTo(&u); err != nil {
return err
}
if c.peers.byCID == nil {
c.peers.bySID = make(map[adc.SID]*Peer)
c.peers.byCID = make(map[adc.CID]*Peer)
}
p = &Peer{hub: c, user: &u}
c.peers.bySID[sid] = p
c.peers.byCID[u.Id] = p
p.online(sid)
return nil
}
func (c *Conn) acceptUsersList() error {
// https://adc.sourceforge.io/ADC.html#_identify
deadline := time.Now().Add(time.Minute)
// Accept commands in the following order:
// 1) Hub info (I-INF)
// 2) Status (I-STA, optional)
// 3) User info (B-INF, xN)
// 3.1) Our own info (B-INF)
const (
hubInfo = iota
optStatus
userList
)
stage := hubInfo
for {
cmd, err := c.conn.ReadPacketRaw(deadline)
if err != nil {
return err
}
switch cmd := cmd.(type) {
case *adcp.InfoPacket:
typ := cmd.Msg.Cmd()
switch stage {
case hubInfo:
// waiting for hub info
if typ != (adcp.UserInfo{}).Cmd() {
return fmt.Errorf("expected hub info, received: %#v", cmd)
}
if err := cmd.DecodeMessageTo(&c.hub); err != nil {
return err
}
stage = optStatus
case optStatus:
// optionally wait for status command
if typ != (adcp.Status{}).Cmd() {
return fmt.Errorf("expected status, received: %#v", cmd)
}
var st adcp.Status
if err := cmd.DecodeMessageTo(&st); err != nil {
return err
} else if !st.Ok() {
return st.Err()
}
stage = userList
default:
return fmt.Errorf("unexpected command in stage %d: %#v", stage, cmd)
}
case *adcp.BroadcastPacket:
switch stage {
case optStatus:
stage = userList
fallthrough
case userList:
if cmd.ID == c.sid {
// make sure to wipe PID, so we don't send it later occasionally
c.user.Pid = nil
if err := cmd.DecodeMessageTo(&c.user); err != nil {
return err
}
// done, should switch to NORMAL
return nil
}
// other users
var u adcp.UserInfo
if err := cmd.DecodeMessageTo(&u); err != nil {
return err
}
_ = c.peerJoins(cmd.ID, u)
// continue until we see ourselves in the list
default:
return fmt.Errorf("unexpected command in stage %d: %#v", stage, cmd)
}
default:
return fmt.Errorf("unexpected command: %#v", cmd)
}
}
}
type Peer struct {
hub *Conn
mu sync.RWMutex
sid *adc.SID // may change if user disconnects
user *adcp.UserInfo
}
func (p *Peer) online(sid adc.SID) {
p.mu.Lock()
p.sid = &sid
p.mu.Unlock()
}
func (p *Peer) offline() {
p.mu.Lock()
p.sid = nil
p.mu.Unlock()
}
func (p *Peer) getSID() *adc.SID {
p.mu.RLock()
sid := p.sid
p.mu.RUnlock()
return sid
}
func (p *Peer) Online() bool {
return p.getSID() != nil
}
func (p *Peer) Info() adcp.UserInfo {
p.mu.RLock()
user := *p.user
p.mu.RUnlock()
return user
}
func (p *Peer) update(u adcp.UserInfo) {
p.mu.Lock()
if p.user.Id != u.Id {
p.mu.Unlock()
panic("wrong cid")
}
*p.user = u
p.mu.Unlock()
}