-
Notifications
You must be signed in to change notification settings - Fork 211
/
client.go
667 lines (514 loc) · 14 KB
/
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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package noise
import (
"bufio"
"context"
"crypto/aes"
"crypto/cipher"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"go.uber.org/zap"
"io"
"net"
"sync"
"time"
)
type clientSide bool
const (
clientSideInbound clientSide = false
clientSideOutbound clientSide = true
)
func (c clientSide) String() string {
if c {
return "outbound"
}
return "inbound"
}
// Client represents an pooled inbound/outbound connection under some node. Should a client successfully undergo
// noise's protocol handshake, information about the peer representative of this client, such as its ID is available.
//
// A clients connection may be closed through (*Client).Close, through the result of a failed handshake, through
// exceeding the max inbound/outbound connection count configured on the clients associated node, through a node
// gracefully being stopped, through a Handler configured on the node returning an error upon recipient of some data,
// or through receiving unexpected/suspicious data.
//
// The lifecycle of a client may be controlled through (*Client).WaitUntilReady, and (*Client).WaitUntilClosed. It
// provably has been useful in writing unit tests where a client instance is used under high concurrency scenarios.
//
// A client in total has two goroutines associated to it: a goroutine responsible for handling writing messages, and a
// goroutine responsible for handling the recipient of messages.
type Client struct {
node *Node
id ID
addr string
side clientSide
suite cipher.AEAD
logger struct {
sync.RWMutex
*zap.Logger
}
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
readerBuf []byte
writerBuf []message
writerCond sync.Cond
writerClosed bool
requests *requestMap
ready chan struct{}
readerDone chan struct{}
writerDone chan struct{}
clientDone chan struct{}
err struct {
sync.Mutex
error
}
closeOnce sync.Once
}
func newClient(node *Node) *Client {
c := &Client{
node: node,
requests: newRequestMap(),
readerBuf: make([]byte, 4+node.maxRecvMessageSize),
ready: make(chan struct{}),
readerDone: make(chan struct{}),
writerDone: make(chan struct{}),
clientDone: make(chan struct{}),
}
c.writerCond.L = &sync.Mutex{}
c.SetLogger(node.logger)
return c
}
// ID returns an immutable copy of the ID of this client, which is established once the client has successfully
// completed the handshake protocol configured from this clients associated node.
//
// ID may be called concurrently.
func (c *Client) ID() ID {
return c.id
}
// Logger returns the underlying logger associated to this client. It may optionally be set via (*Client).SetLogger.
//
// Logger may be called concurrently.
func (c *Client) Logger() *zap.Logger {
c.logger.RLock()
defer c.logger.RUnlock()
return c.logger.Logger
}
// SetLogger updates the logger instance of this client.
//
// SetLogger may be called concurrently.
func (c *Client) SetLogger(logger *zap.Logger) {
c.logger.Lock()
defer c.logger.Unlock()
c.logger.Logger = logger
}
// Close asynchronously kills the underlying connection and signals all goroutines to stop underlying this client.
//
// Close may be called concurrently.
func (c *Client) Close() {
c.close()
}
// WaitUntilReady pauses the goroutine to which it was called within until/unless the client has successfully
// completed/failed the handshake protocol configured under the node instance to which this peer was derived from.
//
// It pauses the goroutine by reading from a channel that is closed when the client has successfully completed/failed
// the aforementioned handshake protocol.
//
// WaitUntilReady may be called concurrently.
func (c *Client) WaitUntilReady() {
c.waitUntilReady()
}
// WaitUntilClosed pauses the goroutine to which it was called within until all goroutines associated to this client
// has been closed. The goroutines associated to this client would only close should:
//
// 1) handshaking failed/succeeded,
// 2) the connection was dropped, or
// 3) (*Client).Close was called.
//
// WaitUntilReady may be called concurrently.
func (c *Client) WaitUntilClosed() {
c.waitUntilClosed()
}
// Error returns the very first error that has caused this clients connection to have dropped.
//
// Error may be called concurrently.
func (c *Client) Error() error {
c.err.Lock()
defer c.err.Unlock()
return c.err.error
}
func (c *Client) reportError(err error) {
c.err.Lock()
defer c.err.Unlock()
if c.err.error == nil {
c.err.error = err
}
}
func (c *Client) close() {
c.closeOnce.Do(func() {
c.writerCond.L.Lock()
c.writerClosed = true
c.writerCond.Signal()
c.writerCond.L.Unlock()
if c.conn != nil {
c.conn.Close()
}
})
}
func (c *Client) waitUntilReady() {
<-c.ready
}
func (c *Client) waitUntilClosed() {
<-c.clientDone
}
func (c *Client) outbound(ctx context.Context, addr string) {
c.addr = addr
c.side = clientSideInbound
defer func() {
c.node.outbound.remove(addr)
close(c.clientDone)
}()
var dialer net.Dialer
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
c.reportError(err)
close(c.ready)
close(c.writerDone)
close(c.readerDone)
return
}
c.reader = bufio.NewReader(conn)
c.writer = bufio.NewWriter(conn)
c.conn = conn
c.handshake()
go c.writeLoop()
c.recvLoop()
c.close()
c.Logger().Debug("Peer connection closed.")
for _, protocol := range c.node.protocols {
if protocol.OnPeerDisconnected == nil {
continue
}
protocol.OnPeerDisconnected(c)
}
}
func (c *Client) inbound(conn net.Conn, addr string) {
c.addr = addr
c.side = clientSideOutbound
defer func() {
c.node.inbound.remove(addr)
close(c.clientDone)
}()
c.reader = bufio.NewReader(conn)
c.writer = bufio.NewWriter(conn)
c.conn = conn
c.handshake()
if c.Error() != nil {
c.close()
return
}
go c.writeLoop()
c.recvLoop()
c.close()
for _, protocol := range c.node.protocols {
if protocol.OnPeerDisconnected == nil {
continue
}
protocol.OnPeerDisconnected(c)
}
}
func (c *Client) read() ([]byte, error) {
if c.node.idleTimeout > 0 {
if err := c.conn.SetReadDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
return nil, err
}
}
if _, err := io.ReadFull(c.reader, c.readerBuf[:4]); err != nil {
return nil, err
}
size := binary.BigEndian.Uint32(c.readerBuf[:4])
if c.node.maxRecvMessageSize > 0 && size > c.node.maxRecvMessageSize {
return nil, fmt.Errorf("got %d bytes, but limit is set to %d: %w", size, c.node.maxRecvMessageSize, ErrMessageTooLarge)
}
if _, err := io.ReadFull(c.reader, c.readerBuf[4:size+4]); err != nil {
return nil, err
}
if c.suite == nil {
return c.readerBuf[4 : size+4], nil
}
buf, err := decryptAEAD(c.suite, c.readerBuf[4:size+4])
if err != nil {
return nil, err
}
return buf, nil
}
func (c *Client) write(data []byte) error {
if c.node.idleTimeout > 0 {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
return err
}
}
if c.suite != nil {
var err error
if data, err = encryptAEAD(c.suite, data); err != nil {
return err
}
}
data = append(make([]byte, 4), data...)
binary.BigEndian.PutUint32(data[:4], uint32(len(data)-4))
if _, err := c.writer.Write(data); err != nil {
return err
}
return c.writer.Flush()
}
func (c *Client) send(nonce uint64, data []byte) error {
c.writerCond.L.Lock()
c.writerBuf = append(c.writerBuf, message{nonce: nonce, data: data})
c.writerCond.Signal()
c.writerCond.L.Unlock()
return nil
}
func (c *Client) request(ctx context.Context, data []byte) (message, error) {
// Figure out an available request nonce.
ch, nonce, err := c.requests.nextNonce()
if err != nil {
return message{}, err
}
// Send request.
if err := c.send(nonce, data); err != nil {
c.requests.markRequestFailed(nonce)
return message{}, err
}
// Await response.
var msg message
select {
case msg = <-ch:
if msg.nonce == 0 {
return message{}, io.EOF
}
case <-ctx.Done():
return message{}, ctx.Err()
}
return msg, nil
}
func (c *Client) handshake() {
defer close(c.ready)
// Generate Ed25519 ephemeral keypair to perform a Diffie-Hellman handshake.
pub, sec, err := GenerateKeys(nil)
if err != nil {
c.reportError(err)
return
}
// Send our Ed25519 ephemeral public key and signature of the message '.__noise_handshake'.
signature := sec.Sign([]byte(".__noise_handshake"))
if err := c.write(append(pub[:], signature[:]...)); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read from our peer their Ed25519 ephemeral public key and signature of the message '.__noise_handshake'.
data, err := c.read()
if err != nil {
c.reportError(err)
return
}
if len(data) != SizePublicKey+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes opening a session: expected %d byte(s), but got %d byte(s)",
SizePublicKey+SizeSignature,
len(data),
))
return
}
var peerPublicKey PublicKey
copy(peerPublicKey[:], data[:SizePublicKey])
// Verify ownership of our peers Ed25519 public key by verifying the signature they sent.
if !peerPublicKey.Verify([]byte(".__noise_handshake"), UnmarshalSignature(data[SizePublicKey:SizePublicKey+SizeSignature])) {
c.reportError(errors.New("could not verify session handshake"))
return
}
// Transform all Ed25519 points to Curve25519 points and perform a Diffie-Hellman handshake
// to derive a shared key.
shared, err := ECDH(sec, peerPublicKey)
if err != nil {
c.reportError(err)
return
}
// Use the derived shared key from Diffie-Hellman to encrypt/decrypt all future communications
// with AES-256 Galois Counter Mode (GCM).
core, err := aes.NewCipher(shared[:])
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes: %w", err))
return
}
suite, err := cipher.NewGCM(core)
if err != nil {
c.reportError(fmt.Errorf("could not instantiate aes-gcm: %w", err))
return
}
c.suite = suite
// Send to our peer our overlay ID.
buf := c.node.id.Marshal()
signature = c.node.Sign(append(buf, shared...))
buf = append(buf, signature[:]...)
if err := c.write(buf); err != nil {
c.reportError(fmt.Errorf("failed to send session handshake: %w", err))
return
}
// Read and parse from our peer their overlay ID.
data, err = c.read()
if err != nil {
c.reportError(fmt.Errorf("failed to read overlay handshake: %w", err))
return
}
id, err := UnmarshalID(data)
if err != nil {
c.reportError(fmt.Errorf("failed to parse peer id while handling overlay handshake: %w", err))
return
}
// Validate the peers ownership of the overlay ID.
buf = make([]byte, id.Size())
copy(buf, data)
if len(data) != len(buf)+SizeSignature {
c.reportError(fmt.Errorf("received invalid number of bytes handshaking: expected %d byte(s), got %d byte(s)",
len(buf)+SizeSignature,
len(data),
))
return
}
if !id.ID.Verify(append(buf, shared...), UnmarshalSignature(data[len(buf):len(buf)+SizeSignature])) {
c.reportError(errors.New("overlay handshake signature is malformed"))
return
}
c.id = id
c.SetLogger(c.Logger().With(
zap.String("peer_id", id.ID.String()),
zap.String("peer_addr", id.Address),
zap.String("remote_addr", c.conn.RemoteAddr().String()),
zap.String("session_key", hex.EncodeToString(shared[:])),
))
c.Logger().Debug("Peer connection opened.")
for _, protocol := range c.node.protocols {
if protocol.OnPeerConnected == nil {
continue
}
protocol.OnPeerConnected(c)
}
}
func (c *Client) recvLoop() {
defer close(c.readerDone)
for {
buf, err := c.read()
if err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error while sending messages.", zap.Error(err))
}
c.reportError(err)
break
}
msg, err := unmarshalMessage(buf)
if err != nil {
c.Logger().Warn("Got an error while reading incoming messages.", zap.Error(err))
c.reportError(err)
break
}
msg.data = append([]byte{}, msg.data...)
if ch := c.requests.findRequest(msg.nonce); ch != nil {
ch <- msg
close(ch)
continue
}
c.node.work <- HandlerContext{client: c, msg: msg}
for _, protocol := range c.node.protocols {
if protocol.OnMessageRecv == nil {
continue
}
protocol.OnMessageRecv(c)
}
}
}
func (c *Client) writeLoop() {
defer close(c.writerDone)
header := make([]byte, 4)
buf := make([]byte, 0, 1024)
Write:
for {
select {
case <-c.readerDone:
return
case <-c.clientDone:
return
default:
}
if c.node.idleTimeout > 0 {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.node.idleTimeout)); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error setting write deadline.", zap.Error(err))
}
c.reportError(err)
break Write
}
}
c.writerCond.L.Lock()
for len(c.writerBuf) == 0 && !c.writerClosed {
c.writerCond.Wait()
}
writerBuf, writerClosed := c.writerBuf, c.writerClosed
c.writerBuf = nil
c.writerCond.L.Unlock()
if writerClosed {
break Write
}
for _, msg := range writerBuf {
buf = buf[:0]
buf = msg.marshal(buf)
if c.suite != nil {
var err error
if buf, err = encryptAEAD(c.suite, buf); err != nil {
c.Logger().Warn("Got an error encrypting a message.", zap.Error(err))
c.reportError(err)
break Write
}
}
binary.BigEndian.PutUint32(header, uint32(len(buf)))
if _, err := c.writer.Write(header); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing header.", zap.Error(err))
}
c.reportError(err)
break Write
}
if _, err := c.writer.Write(buf); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error writing a message.", zap.Error(err))
}
c.reportError(err)
break Write
}
}
if err := c.writer.Flush(); err != nil {
if !isEOF(err) {
c.Logger().Warn("Got an error flushing.", zap.Error(err))
}
c.reportError(err)
break Write
}
for _, protocol := range c.node.protocols {
if protocol.OnMessageSent == nil {
continue
}
protocol.OnMessageSent(c)
}
}
}
func isEOF(err error) bool {
if errors.Is(err, io.EOF) {
return true
}
var netErr *net.OpError
if errors.As(err, &netErr) {
if netErr.Err.Error() == "use of closed network connection" {
return true
}
}
return false
}