-
Notifications
You must be signed in to change notification settings - Fork 0
/
wirenet.go
619 lines (517 loc) · 12.9 KB
/
wirenet.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
package wirenet
import (
"context"
"crypto/tls"
"errors"
"io"
"net"
"os"
"strings"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/mediabuyerbot/go-wirenet/pb"
"github.com/google/uuid"
"github.com/hashicorp/yamux"
)
type (
// SessionHook is used when opening or closing a session, and each session interception
// is performed in a separate goroutine.
SessionHook func(Session)
// ErrorHandler is used for error logging.
ErrorHandler func(context.Context, error)
// RetryPolicy retry policy is used when there is no connection to the server.
// Used only on the client side.
// The default is DefaultRetryPolicy, but you can write your own policy.
RetryPolicy func(min, max time.Duration, attemptNum int) time.Duration
// Handler is used to handle payload in a named stream.
Handler func(context.Context, Stream)
// Sessions represents a map of active sessions.
// The key is session id and session is value.
Sessions map[uuid.UUID]Session
// TokenValidator used to validate the token if the token is set on the client side with WithIdentification().
// Token and identifier can be any sequence of bytes.
TokenValidator func(streamName string, id Identification, token Token) error
)
// Wire is used to initialize a server or client side connection.
type Wire interface {
// Sessions returns a list of active sessions.
Sessions() Sessions
// Session returns the session by UUID.
Session(sessionID uuid.UUID) (Session, error)
// Stream registers the handler for the given name.
// If a named stream already exists, stream overwrite.
Stream(name string, h Handler)
// Close gracefully shutdown the server without interrupting any active connections.
Close() error
// Connect creates a new connection.
// If the wire is on the client-side, then used dial().
// If the wire is on the server-side, then used listener().
Connect() error
}
// Mount constructs a new connection point with the given addr and Options as the server side.
func Mount(addr string, opts ...Option) (Wire, error) {
return newWire(addr, serverSide, opts...)
}
// Hub constructs a new connection point with the given addr and Options as the server side.
func Hub(addr string, opts ...Option) (Wire, error) {
opts = append(opts, func(wire *wire) {
wire.hubMode = true
})
return newWire(addr, serverSide, opts...)
}
// Join constructs a new connection point with the given addr and Options as the client side.
func Join(addr string, opts ...Option) (Wire, error) {
return newWire(addr, clientSide, opts...)
}
const (
clientSide role = 1
serverSide role = 2
)
type role int
func (s role) IsClientSide() bool {
return s == clientSide
}
func (s role) IsServerSide() bool {
return s == serverSide
}
func (s role) String() (side string) {
switch s {
case clientSide:
side = "client side"
case serverSide:
side = "server side"
default:
side = "unknown"
}
return side
}
type wire struct {
addr string
readTimeout time.Duration
writeTimeout time.Duration
sessCloseTimeout time.Duration
role role
openSessHook SessionHook
closeSessHook SessionHook
onConnect func(io.Closer)
transportConf *yamux.Config
hubMode bool
closed bool
conn bool
connCounter int
closeCh chan chan *ShutdownError
waitCh chan struct{}
retryWaitMin time.Duration
retryWaitMax time.Duration
retryMax int
retryPolicy RetryPolicy
sessions Sessions
streamIndex map[string]Session
token Token
verifyToken TokenValidator
identification Identification
tlsConfig *tls.Config
handlers map[string]Handler
errorHandler ErrorHandler
mu sync.RWMutex
}
func newWire(addr string, role role, opts ...Option) (Wire, error) {
if len(addr) == 0 {
return nil, ErrAddrEmpty
}
wire := &wire{
addr: addr,
handlers: make(map[string]Handler),
errorHandler: func(ctx context.Context, err error) {},
readTimeout: DefaultReadTimeout,
writeTimeout: DefaultWriteTimeout,
sessCloseTimeout: DefaultSessionCloseTimeout,
sessions: make(Sessions),
streamIndex: make(map[string]Session),
role: role,
openSessHook: func(Session) {},
closeSessHook: func(Session) {},
closeCh: make(chan chan *ShutdownError),
onConnect: func(_ io.Closer) {},
retryMax: DefaultRetryMax,
retryWaitMin: DefaultRetryWaitMin,
retryWaitMax: DefaultRetryWaitMax,
retryPolicy: DefaultRetryPolicy,
transportConf: &yamux.Config{
AcceptBacklog: DefaultAcceptBacklog,
EnableKeepAlive: DefaultEnableKeepAlive,
KeepAliveInterval: DefaultKeepAliveInterval,
ConnectionWriteTimeout: DefaultWriteTimeout,
MaxStreamWindowSize: DefaultAcceptBacklog * 1024,
LogOutput: os.Stderr,
},
}
for _, opt := range opts {
if opt == nil {
continue
}
opt(wire)
}
return wire, nil
}
func (w *wire) Session(sid uuid.UUID) (Session, error) {
w.mu.RLock()
defer w.mu.RUnlock()
sess, found := w.sessions[sid]
if !found {
return nil, ErrSessionNotFound
}
return sess, nil
}
func (w *wire) Sessions() Sessions {
w.mu.RLock()
defer w.mu.RUnlock()
return w.sessions
}
func (w *wire) Connect() (err error) {
switch w.role {
case clientSide:
err = w.acceptClient()
case serverSide:
err = w.acceptServer()
}
return err
}
func (w *wire) Stream(name string, h Handler) {
w.mu.Lock()
w.handlers[name] = h
w.mu.Unlock()
}
func (w *wire) isClosed() bool {
w.mu.RLock()
defer w.mu.RUnlock()
return w.closed
}
func (w *wire) isHubMode() bool {
w.mu.RLock()
defer w.mu.RUnlock()
return w.hubMode
}
func (w *wire) findHandler(name string) (Handler, error) {
w.mu.RLock()
defer w.mu.RUnlock()
h, ok := w.handlers[name]
if !ok {
return nil, ErrStreamHandlerNotFound
}
return h, nil
}
func (w *wire) findSession(name string) (Session, error) {
w.mu.RLock()
defer w.mu.RUnlock()
if len(w.sessions) == 0 || w.closed {
return nil, ErrSessionClosed
}
sess, found := w.streamIndex[name]
if !found {
return nil, ErrSessionNotFound
}
return sess, nil
}
func (w *wire) isConnOk() bool {
w.mu.RLock()
defer w.mu.RUnlock()
return w.conn
}
func (w *wire) setConnFlag(flag bool) {
w.mu.Lock()
defer w.mu.Unlock()
w.conn = flag
}
func (w *wire) Close() (err error) {
closeup := func() {
w.mu.Lock()
w.closed = true
w.mu.Unlock()
}
if !w.isConnOk() {
closeup()
return nil
}
if w.isClosed() {
return ErrWireClosed
}
closeup()
return w.close()
}
func (w *wire) close() (err error) {
errCh := make(chan *ShutdownError)
w.closeCh <- errCh
shutErr := <-errCh
if shutErr.IsFilled() {
err = shutErr
}
return err
}
func (w *wire) acceptClient() (err error) {
tryClose := func(c io.Closer) {
w.setConnFlag(false)
c.Close()
}
for {
attemptNum := w.connCounter
if attemptNum >= w.retryMax || w.isClosed() {
break
}
w.connCounter++
w.setConnFlag(false)
conn, dialErr := w.dial()
if dialErr != nil {
err = dialErr
if isNotConnErr(dialErr) {
break
}
retryWait := w.retryPolicy(
w.retryWaitMin,
w.retryWaitMax,
attemptNum)
timeout := time.Now().Add(retryWait)
for {
if (timeout.Unix() <= time.Now().Unix()) || w.isClosed() {
break
}
time.Sleep(500 * time.Millisecond)
}
continue
}
w.setConnFlag(true)
w.connCounter = 0
wrapConn, serveErr := yamux.Client(conn, w.transportConf)
if serveErr != nil {
tryClose(conn)
return serveErr
}
sid, remoteStreamNames, sErr := w.openSession(wrapConn)
if sErr != nil {
tryClose(conn)
return sErr
}
w.waitCh = make(chan struct{})
w.closeCh = make(chan chan *ShutdownError)
go w.shutdown(wrapConn)
openSession(sid, w.identification, wrapConn, w, remoteStreamNames)
go w.onConnect(w)
<-w.waitCh
}
return err
}
func (w *wire) listen() (listener net.Listener, err error) {
if w.tlsConfig != nil {
listener, err = tls.Listen("tcp", w.addr, w.tlsConfig)
} else {
listener, err = net.Listen("tcp", w.addr)
}
return listener, err
}
func (w *wire) dial() (conn net.Conn, err error) {
if w.tlsConfig != nil {
conn, err = tls.Dial("tcp", w.addr, w.tlsConfig)
} else {
conn, err = net.Dial("tcp", w.addr)
}
return
}
func (w *wire) acceptServer() (err error) {
listener, err := w.listen()
if err != nil {
return err
}
defer func() {
listener.Close()
w.setConnFlag(false)
}()
go w.shutdown(listener)
go w.onConnect(w)
w.setConnFlag(true)
for {
conn, acceptErr := listener.Accept()
if acceptErr != nil {
if !w.isClosed() {
err = acceptErr
}
break
}
// close all new connections
if w.isClosed() {
_ = conn.Close()
continue
}
wrapConn, serveErr := yamux.Server(conn, w.transportConf)
if serveErr != nil {
err = serveErr
break
}
sid, id, localStreamNames, sErr := w.confirmSession(wrapConn, w.verifyToken)
if sErr != nil {
conn.Close()
continue
}
openSession(sid, id, wrapConn, w, localStreamNames)
}
return err
}
func (w *wire) shutdown(conn io.Closer) {
errCh, ok := <-w.closeCh
if !ok {
return
}
shutdownErr := NewShutdownError()
w.mu.RLock()
sessions := make([]Session, 0, len(w.sessions))
for _, sess := range w.sessions {
sessions = append(sessions, sess)
}
w.mu.RUnlock()
var err error
for _, sess := range sessions {
err = sess.Close()
if err != nil && err != ErrSessionClosed {
shutdownErr.Add(err)
}
}
if err := conn.Close(); err != nil {
shutdownErr.Add(err)
}
if w.role.IsClientSide() {
close(w.waitCh)
}
errCh <- shutdownErr
}
func (w *wire) registerSession(s Session) {
w.mu.Lock()
defer w.mu.Unlock()
w.sessions[s.ID()] = s
if w.hubMode {
for _, streamName := range s.StreamNames() {
w.streamIndex[streamName] = s
}
}
}
func (w *wire) unregisterSession(s Session) {
var isEmptySessions bool
w.mu.Lock()
if w.hubMode {
for _, streamName := range s.StreamNames() {
delete(w.streamIndex, streamName)
}
}
delete(w.sessions, s.ID())
isEmptySessions = len(w.sessions) == 0
w.mu.Unlock()
if w.role.IsClientSide() && isEmptySessions && !w.isClosed() {
w.close()
}
}
func (w *wire) openSession(conn *yamux.Session) (sid uuid.UUID, rsn []string, err error) {
stream, err := conn.OpenStream()
if err != nil {
return sid, rsn, err
}
defer stream.Close()
if err := stream.SetDeadline(deadline(w)); err != nil {
return sid, rsn, err
}
sid = uuid.New()
sessID, err := sid.MarshalBinary()
if err != nil {
return sid, rsn, err
}
remoteStreamNames, err := openSessionRequest(
stream,
sessID,
w.token,
w.identification,
localStreamNames(w.handlers))
if err != nil {
return sid, nil, err
}
return sid, remoteStreamNames, nil
}
func (w *wire) confirmSession(conn *yamux.Session, tv TokenValidator) (sid uuid.UUID, id Identification, lsn []string, err error) {
stream, err := conn.AcceptStream()
if err != nil {
return sid, nil, nil, err
}
defer stream.Close()
if err := stream.SetDeadline(deadline(w)); err != nil {
return sid, nil, lsn, err
}
return confirmSessionRequest(
stream,
w.verifyToken,
localStreamNames(w.handlers),
)
}
func localStreamNames(m map[string]Handler) (names []string) {
names = make([]string, 0, len(m))
for n, _ := range m {
names = append(names, n)
}
return
}
func deadline(w *wire) time.Time {
return time.Now().Add(w.transportConf.ConnectionWriteTimeout)
}
func confirmSessionRequest(conn *yamux.Stream, fn TokenValidator, localStreamNames []string) (sid uuid.UUID, id Identification, lsn []string, err error) {
frm, err := newDecoder(conn).Decode()
if err != nil {
return sid, nil, nil, err
}
var req pb.OpenSessionRequest
if err := proto.Unmarshal(frm.Payload(), &req); err != nil {
return sid, nil, nil, err
}
resp := &pb.OpenSessionResponse{
Sid: req.Sid,
RemoteStreamNames: localStreamNames,
}
if fn != nil {
if err := fn("confirmSession", req.Identification, req.Token); err != nil {
resp.Err = err.Error()
}
}
p, err := proto.Marshal(resp)
if err != nil {
return sid, nil, nil, err
}
if err := newEncoder(conn).Encode(confSessType, "confirmSession", p); err != nil {
return sid, nil, nil, err
}
sid, err = uuid.FromBytes(req.Sid)
return sid, req.Identification, req.LocalStreamNames, err
}
func openSessionRequest(conn *yamux.Stream, sessID []byte, token Token, id Identification, localStreamNames []string) ([]string, error) {
payload, err := proto.Marshal(&pb.OpenSessionRequest{
Sid: sessID,
Token: token,
LocalStreamNames: localStreamNames,
Identification: id,
})
if err != nil {
return nil, err
}
if err := newEncoder(conn).Encode(openSessTyp, "openSession", payload); err != nil {
return nil, err
}
frm, err := newDecoder(conn).Decode()
if err != nil {
return nil, err
}
var resp pb.OpenSessionResponse
if err := proto.Unmarshal(frm.Payload(), &resp); err != nil {
return nil, err
}
if len(resp.Err) > 0 {
return nil, errors.New(resp.Err)
}
return resp.RemoteStreamNames, nil
}
func isNotConnErr(err error) bool {
return !strings.Contains(err.Error(), "connection refused")
}