forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.go
486 lines (448 loc) · 14 KB
/
peer.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
// Copyright 2015-2017 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tp
import (
"context"
"crypto/tls"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/goutil/coarsetime"
"github.com/henrylee2cn/goutil/errors"
"github.com/henrylee2cn/teleport/codec"
"github.com/henrylee2cn/teleport/socket"
)
type (
// BasePeer peer with the common method set
BasePeer interface {
// Close closes peer.
Close() (err error)
// CountSession returns the number of sessions.
CountSession() int
// Dial connects with the peer of the destination address.
Dial(addr string, protoFunc ...socket.ProtoFunc) (Session, *Rerror)
// DialContext connects with the peer of the destination address, using the provided context.
DialContext(ctx context.Context, addr string, protoFunc ...socket.ProtoFunc) (Session, *Rerror)
// GetSession gets the session by id.
GetSession(sessionId string) (Session, bool)
// RangeSession ranges all sessions. If fn returns false, stop traversing.
RangeSession(fn func(sess Session) bool)
// ServeConn serves the connection and returns a session.
ServeConn(conn net.Conn, protoFunc ...socket.ProtoFunc) (Session, error)
// SetTlsConfig sets the TLS config.
SetTlsConfig(tlsConfig *tls.Config)
// SetTlsConfigFromFile sets the TLS config from file.
SetTlsConfigFromFile(tlsCertFile, tlsKeyFile string) error
// TlsConfig returns the TLS config.
TlsConfig() *tls.Config
}
// EarlyPeer the communication peer that has just been created
EarlyPeer interface {
BasePeer
// SubRoute adds handler group.
SubRoute(pathPrefix string, plugin ...Plugin) *Router
// RoutePull registers PULL handler.
RoutePull(ctrlStruct interface{}, plugin ...Plugin)
// RoutePush registers PUSH handler.
RoutePush(ctrlStruct interface{}, plugin ...Plugin)
// SetUnknownPull sets the default handler, which is called when no handler for PULL is found.
SetUnknownPull(fn func(UnknownPullCtx) (interface{}, *Rerror), plugin ...Plugin)
// SetUnknownPush sets the default handler, which is called when no handler for PUSH is found.
SetUnknownPush(fn func(UnknownPushCtx) *Rerror, plugin ...Plugin)
}
// Peer the communication peer which is server or client role
Peer interface {
EarlyPeer
// Listen turns on the listening service.
Listen(protoFunc ...socket.ProtoFunc) error
}
peer struct {
rootRouter *RootRouter
pluginContainer *PluginContainer
sessHub *SessionHub
closeCh chan struct{}
freeContext *readHandleCtx
ctxLock sync.Mutex
defaultSessionAge time.Duration // Default session max age, if less than or equal to 0, no time limit
defaultContextAge time.Duration // Default PULL or PUSH context max age, if less than or equal to 0, no time limit
tlsConfig *tls.Config
slowCometDuration time.Duration
defaultBodyCodec byte
printBody bool
countTime bool
timeNow func() time.Time
timeSince func(time.Time) time.Duration
mu sync.Mutex
network string
// only for client role
defaultDialTimeout time.Duration
redialTimes int32
// only for server role
listenAddr string
listen net.Listener
}
)
var (
_ BasePeer = new(peer)
_ EarlyPeer = new(peer)
_ Peer = new(peer)
)
// NewPeer creates a new peer.
func NewPeer(cfg PeerConfig, plugin ...Plugin) Peer {
pluginContainer := newPluginContainer()
pluginContainer.AppendRight(plugin...)
pluginContainer.PreNewPeer(&cfg)
if err := cfg.check(); err != nil {
Fatalf("%v", err)
}
var p = &peer{
rootRouter: newRootRouter("/", pluginContainer),
pluginContainer: pluginContainer,
sessHub: newSessionHub(),
defaultSessionAge: cfg.DefaultSessionAge,
defaultContextAge: cfg.DefaultContextAge,
closeCh: make(chan struct{}),
slowCometDuration: cfg.slowCometDuration,
defaultDialTimeout: cfg.DefaultDialTimeout,
network: cfg.Network,
listenAddr: cfg.ListenAddress,
printBody: cfg.PrintBody,
countTime: cfg.CountTime,
redialTimes: cfg.RedialTimes,
}
if c, err := codec.GetByName(cfg.DefaultBodyCodec); err != nil {
Fatalf("%v", err)
} else {
p.defaultBodyCodec = c.Id()
}
if p.countTime {
p.timeNow = time.Now
p.timeSince = time.Since
} else {
t0 := time.Time{}
p.timeNow = func() time.Time { return t0 }
p.timeSince = func(time.Time) time.Duration { return 0 }
}
addPeer(p)
p.pluginContainer.PostNewPeer(p)
return p
}
// TlsConfig returns the TLS config.
func (p *peer) TlsConfig() *tls.Config {
return p.tlsConfig
}
// SetTlsConfig sets the TLS config.
func (p *peer) SetTlsConfig(tlsConfig *tls.Config) {
p.tlsConfig = tlsConfig
}
// SetTlsConfigFromFile sets the TLS config from file.
func (p *peer) SetTlsConfigFromFile(tlsCertFile, tlsKeyFile string) error {
cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
if err != nil {
return err
}
p.tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"http/1.1", "h2"},
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
},
}
return nil
}
// GetSession gets the session by id.
func (p *peer) GetSession(sessionId string) (Session, bool) {
return p.sessHub.Get(sessionId)
}
// RangeSession ranges all sessions.
// If fn returns false, stop traversing.
func (p *peer) RangeSession(fn func(sess Session) bool) {
p.sessHub.sessions.Range(func(key, value interface{}) bool {
return fn(value.(*session))
})
}
// CountSession returns the number of sessions.
func (p *peer) CountSession() int {
return p.sessHub.sessions.Len()
}
// Dial connects with the peer of the destination address.
func (p *peer) Dial(addr string, protoFunc ...socket.ProtoFunc) (Session, *Rerror) {
return p.newSessionForClient(func() (net.Conn, error) {
return net.DialTimeout(p.network, addr, p.defaultDialTimeout)
}, addr, protoFunc)
}
// DialContext connects with the peer of the destination address,
// using the provided context.
func (p *peer) DialContext(ctx context.Context, addr string, protoFunc ...socket.ProtoFunc) (Session, *Rerror) {
return p.newSessionForClient(func() (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, p.network, addr)
}, addr, protoFunc)
}
func (p *peer) newSessionForClient(dialFunc func() (net.Conn, error), addr string, protoFuncs []socket.ProtoFunc) (*session, *Rerror) {
var conn, dialErr = dialFunc()
if dialErr != nil {
rerr := rerrDialFailed.Copy()
rerr.Detail = dialErr.Error()
return nil, rerr
}
if p.tlsConfig != nil {
conn = tls.Client(conn, p.tlsConfig)
}
var sess = newSession(p, conn, protoFuncs)
if p.redialTimes > 0 {
sess.redialForClientFunc = func() bool {
var err error
for i := p.redialTimes; i > 0; i-- {
err = p.renewSessionForClient(sess, dialFunc, addr, protoFuncs)
if err == nil {
return true
}
}
if err != nil {
Errorf("redial fail (network:%s, addr:%s, id:%s): %s", p.network, sess.RemoteIp(), sess.Id(), err.Error())
}
return false
}
}
sess.socket.SetId(sess.LocalIp())
if rerr := p.pluginContainer.PostDial(sess); rerr != nil {
sess.Close()
return nil, rerr
}
AnywayGo(sess.startReadAndHandle)
p.sessHub.Set(sess)
Infof("dial ok (network:%s, addr:%s, id:%s)", p.network, sess.RemoteIp(), sess.Id())
return sess, nil
}
func (p *peer) renewSessionForClient(sess *session, dialFunc func() (net.Conn, error), addr string, protoFuncs []socket.ProtoFunc) error {
var conn, dialErr = dialFunc()
if dialErr != nil {
return dialErr
}
if p.tlsConfig != nil {
conn = tls.Client(conn, p.tlsConfig)
}
oldIp := sess.LocalIp()
oldId := sess.Id()
sess.socket.Reset(conn, protoFuncs...)
if oldIp == oldId {
sess.socket.SetId(sess.LocalIp())
} else {
sess.socket.SetId(oldId)
}
if rerr := p.pluginContainer.PostDial(sess); rerr != nil {
sess.Close()
return rerr.ToError()
}
atomic.StoreInt32(&sess.status, statusOk)
AnywayGo(sess.startReadAndHandle)
p.sessHub.Set(sess)
Infof("redial ok (network:%s, addr:%s, id:%s)", p.network, sess.RemoteIp(), sess.Id())
return nil
}
// ErrListenClosed listener is closed error.
var ErrListenClosed = errors.New("listener is closed")
// Listen turns on the listening service.
func (p *peer) Listen(protoFunc ...socket.ProtoFunc) error {
lis, err := listen(p.network, p.listenAddr, p.tlsConfig)
if err != nil {
Fatalf("%v", err)
}
defer lis.Close()
p.pluginContainer.PostListen(lis.Addr())
p.listen = lis
network := lis.Addr().Network()
addr := lis.Addr().String()
Printf("listen ok (network:%s, addr:%s)", network, addr)
var (
tempDelay time.Duration // how long to sleep on accept failure
closeCh = p.closeCh
)
for {
conn, e := lis.Accept()
if e != nil {
select {
case <-closeCh:
return ErrListenClosed
default:
}
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
Tracef("accept error: %s; retrying in %v", e.Error(), tempDelay)
time.Sleep(tempDelay)
continue
}
return e
}
tempDelay = 0
AnywayGo(func() {
if c, ok := conn.(*tls.Conn); ok {
if p.defaultSessionAge > 0 {
c.SetReadDeadline(coarsetime.CeilingTimeNow().Add(p.defaultSessionAge))
}
if p.defaultContextAge > 0 {
c.SetReadDeadline(coarsetime.CeilingTimeNow().Add(p.defaultContextAge))
}
if err := c.Handshake(); err != nil {
Errorf("TLS handshake error from %s: %s", c.RemoteAddr(), err.Error())
return
}
}
var sess = newSession(p, conn, protoFunc)
if rerr := p.pluginContainer.PostAccept(sess); rerr != nil {
sess.Close()
return
}
Tracef("accept session(network:%s, addr:%s, id:%s)", network, sess.RemoteIp(), sess.Id())
p.sessHub.Set(sess)
sess.startReadAndHandle()
})
}
}
// ServeConn serves the connection and returns a session.
func (p *peer) ServeConn(conn net.Conn, protoFunc ...socket.ProtoFunc) (Session, error) {
network := conn.LocalAddr().Network()
if strings.Contains(network, "udp") {
return nil, fmt.Errorf("invalid network: %s,\nrefer to the following: tcp, tcp4, tcp6, unix or unixpacket", network)
}
if c, ok := conn.(*tls.Conn); ok {
if p.defaultSessionAge > 0 {
c.SetReadDeadline(coarsetime.CeilingTimeNow().Add(p.defaultSessionAge))
}
if p.defaultContextAge > 0 {
c.SetReadDeadline(coarsetime.CeilingTimeNow().Add(p.defaultContextAge))
}
if err := c.Handshake(); err != nil {
return nil, errors.Errorf("TLS handshake error from %s: %s", c.RemoteAddr(), err.Error())
}
}
var sess = newSession(p, conn, protoFunc)
if rerr := p.pluginContainer.PostAccept(sess); rerr != nil {
sess.Close()
return nil, rerr.ToError()
}
Tracef("accept session(network:%s, addr:%s, id:%s)", network, sess.RemoteIp(), sess.Id())
p.sessHub.Set(sess)
AnywayGo(sess.startReadAndHandle)
return sess, nil
}
// Close closes peer.
func (p *peer) Close() (err error) {
defer func() {
if p := recover(); p != nil {
err = errors.Errorf("panic:\n%v\n%s", p, goutil.PanicTrace(2))
}
}()
close(p.closeCh)
delete(peers.list, p)
var (
count int
errCh = make(chan error, 10)
)
p.sessHub.Range(func(sess *session) bool {
count++
if !Go(func() {
errCh <- sess.Close()
}) {
errCh <- sess.Close()
}
return true
})
for i := 0; i < count; i++ {
err = errors.Merge(err, <-errCh)
}
close(errCh)
return err
}
func (p *peer) getContext(s *session, withWg bool) *readHandleCtx {
p.ctxLock.Lock()
if withWg {
// count get context
s.graceCtxWaitGroup.Add(1)
}
ctx := p.freeContext
if ctx == nil {
ctx = newReadHandleCtx()
} else {
p.freeContext = ctx.next
}
ctx.reInit(s)
p.ctxLock.Unlock()
return ctx
}
func (p *peer) putContext(ctx *readHandleCtx, withWg bool) {
p.ctxLock.Lock()
defer p.ctxLock.Unlock()
if withWg {
// count get context
ctx.sess.graceCtxWaitGroup.Done()
}
ctx.clean()
ctx.next = p.freeContext
p.freeContext = ctx
}
// SubRoute adds handler group.
func (p *peer) SubRoute(pathPrefix string, plugin ...Plugin) *Router {
return p.rootRouter.SubRoute(pathPrefix, plugin...)
}
// RoutePull registers PULL handler.
func (p *peer) RoutePull(ctrlStruct interface{}, plugin ...Plugin) {
p.rootRouter.RoutePull(ctrlStruct, plugin...)
}
// RoutePush registers PUSH handler.
func (p *peer) RoutePush(ctrlStruct interface{}, plugin ...Plugin) {
p.rootRouter.RoutePush(ctrlStruct, plugin...)
}
// SetUnknownPull sets the default handler,
// which is called when no handler for PULL is found.
func (p *peer) SetUnknownPull(fn func(UnknownPullCtx) (interface{}, *Rerror), plugin ...Plugin) {
p.rootRouter.SetUnknownPull(fn, plugin...)
}
// SetUnknownPush sets the default handler,
// which is called when no handler for PUSH is found.
func (p *peer) SetUnknownPush(fn func(UnknownPushCtx) *Rerror, plugin ...Plugin) {
p.rootRouter.SetUnknownPush(fn, plugin...)
}
// maybe useful
func (p *peer) getPullHandler(uriPath string) (*Handler, bool) {
return p.rootRouter.getPull(uriPath)
}
func (p *peer) getPushHandler(uriPath string) (*Handler, bool) {
return p.rootRouter.getPush(uriPath)
}