forked from andeya/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
478 lines (411 loc) · 13.2 KB
/
helper.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
// Copyright 2015-2018 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/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"math/big"
"net"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/goutil/pool"
"github.com/henrylee2cn/teleport/codec"
"github.com/henrylee2cn/teleport/socket"
"github.com/henrylee2cn/teleport/utils"
)
// ServiceMethodMapper mapper service method from prefix, recvName and funcName.
// NOTE:
// @prefix is optional;
// @name is required.
type ServiceMethodMapper func(prefix, name string) (serviceMethod string)
// SetServiceMethodMapper customizes your own service method mapper.
func SetServiceMethodMapper(mapper ServiceMethodMapper) {
globalServiceMethodMapper = mapper
}
// HTTPServiceMethodMapper like most RPC services service method mapper.
// Such as: user/get
// It is the default mapper.
// The mapping rule of struct(func) name to service methods:
// `AaBb` -> `/aa_bb`
// `ABcXYz` -> `/abc_xyz`
// `Aa__Bb` -> `/aa_bb`
// `aa__bb` -> `/aa_bb`
// `ABC__XYZ` -> `/abc_xyz`
// `Aa_Bb` -> `/aa/bb`
// `aa_bb` -> `/aa/bb`
// `ABC_XYZ` -> `/abc/xyz`
//
func HTTPServiceMethodMapper(prefix, name string) string {
return path.Join("/", prefix, toServiceMethods(name, '/', true))
}
// RPCServiceMethodMapper like most RPC services service method mapper.
// Such as: User.Get
// The mapping rule of struct(func) name to service methods:
// `AaBb` -> `AaBb`
// `ABcXYz` -> `ABcXYz`
// `Aa__Bb` -> `Aa_Bb`
// `aa__bb` -> `aa_bb`
// `ABC__XYZ` -> `ABC_XYZ`
// `Aa_Bb` -> `Aa.Bb`
// `aa_bb` -> `aa.bb`
// `ABC_XYZ` -> `ABC.XYZ`
//
func RPCServiceMethodMapper(prefix, name string) string {
p := prefix + "." + toServiceMethods(name, '.', false)
return strings.Trim(p, ".")
}
// toServiceMethods maps struct(func) name to service methods.
func toServiceMethods(name string, sep rune, toSnake bool) string {
var a = []rune{}
var last rune
for _, r := range name {
if last == '_' {
if r == '_' {
last = '\x00'
continue
} else {
a[len(a)-1] = sep
}
}
if last == '\x00' && r == '_' {
continue
}
a = append(a, r)
last = r
}
name = string(a)
if toSnake {
name = goutil.SnakeString(name)
name = strings.Replace(name, "__", "_", -1)
name = strings.Replace(name, string(sep)+"_", string(sep), -1)
}
return name
}
const (
// MetaRerror reply error metadata key
MetaRerror = "X-Reply-Error"
// MetaRealIP real IP metadata key
MetaRealIP = "X-Real-IP"
// MetaAcceptBodyCodec the key of body codec that the sender wishes to accept
MetaAcceptBodyCodec = "X-Accept-Body-Codec"
)
// WithRerror sets the real IP to metadata.
func WithRerror(rerr *Rerror) MessageSetting {
b, _ := rerr.MarshalJSON()
if len(b) == 0 {
return nil
}
return socket.WithAddMeta(MetaRerror, goutil.BytesToString(b))
}
// WithRealIP sets the real IP to metadata.
func WithRealIP(ip string) MessageSetting {
return socket.WithAddMeta(MetaRealIP, ip)
}
// WithAcceptBodyCodec sets the body codec that the sender wishes to accept.
// NOTE: If the specified codec is invalid, the receiver will ignore the mate data.
func WithAcceptBodyCodec(bodyCodec byte) MessageSetting {
if bodyCodec == codec.NilCodecID {
return WithNothing()
}
return socket.WithAddMeta(MetaAcceptBodyCodec, strconv.FormatUint(uint64(bodyCodec), 10))
}
// GetAcceptBodyCodec gets the body codec that the sender wishes to accept.
// NOTE: If the specified codec is invalid, the receiver will ignore the mate data.
func GetAcceptBodyCodec(meta *utils.Args) (byte, bool) {
s := meta.Peek(MetaAcceptBodyCodec)
if len(s) == 0 || len(s) > 3 {
return 0, false
}
b, err := strconv.ParseUint(goutil.BytesToString(s), 10, 8)
if err != nil {
return 0, false
}
c := byte(b)
return c, c != codec.NilCodecID
}
// WithNothing nothing to do.
// func WithNothing() MessageSetting
var WithNothing = socket.WithNothing
// Socket is a generic stream-oriented network connection.
//
// Multiple goroutines may invoke methods on a Socket simultaneously.
type Socket = socket.Socket
// Proto pack/unpack protocol scheme of socket message.
type Proto = socket.Proto
// ProtoFunc function used to create a custom Proto interface.
type ProtoFunc = socket.ProtoFunc
// IOWithReadBuffer implements buffered I/O with buffered reader.
type IOWithReadBuffer = socket.IOWithReadBuffer
// Message a socket message data.
type Message = socket.Message
// NewBodyFunc creates a new body by header.
type NewBodyFunc = socket.NewBodyFunc
// Header message header interface
type Header = socket.Header
// Body message body interface
type Body = socket.Body
// MessageSetting is a pipe function type for setting message.
type MessageSetting = socket.MessageSetting
// WithContext sets the message handling context.
// func WithContext(ctx context.Context) MessageSetting
var WithContext = socket.WithContext
// WithMtype sets the message type.
// func WithMtype(mtype byte) MessageSetting
var WithMtype = socket.WithMtype
// WithServiceMethod sets the message service method.
// SUGGEST: max len ≤ 255!
// func WithServiceMethod(serviceMethod string) MessageSetting
var WithServiceMethod = socket.WithServiceMethod
// WithAddMeta adds 'key=value' metadata argument.
// Multiple values for the same key may be added.
// SUGGEST: urlencoded string max len ≤ 65535!
// func WithAddMeta(key, value string) MessageSetting
var WithAddMeta = socket.WithAddMeta
// WithSetMeta sets 'key=value' metadata argument.
// SUGGEST: urlencoded string max len ≤ 65535!
// func WithSetMeta(key, value string) MessageSetting
var WithSetMeta = socket.WithSetMeta
// WithBodyCodec sets the body codec.
// func WithBodyCodec(bodyCodec byte) MessageSetting
var WithBodyCodec = socket.WithBodyCodec
// WithBody sets the body object.
// func WithBody(body interface{}) MessageSetting
var WithBody = socket.WithBody
// WithNewBody resets the function of geting body.
// NOTE: newBodyFunc is only for reading form connection.
// func WithNewBody(newBodyFunc socket.NewBodyFunc) MessageSetting
var WithNewBody = socket.WithNewBody
// WithXferPipe sets transfer filter pipe.
// NOTE: Panic if the filterID is not registered.
// SUGGEST: The length can not be bigger than 255!
// func WithXferPipe(filterID ...byte) MessageSetting
var WithXferPipe = socket.WithXferPipe
// GetMessage gets a Message form message pool.
// NOTE:
// newBodyFunc is only for reading form connection;
// settings are only for writing to connection.
// func GetMessage(settings ...MessageSetting) Message
var GetMessage = socket.GetMessage
// PutMessage puts a Message to message pool.
// func PutMessage(m Message)
var PutMessage = socket.PutMessage
var (
_maxGoroutinesAmount = (1024 * 1024 * 8) / 8 // max memory 8GB (8KB/goroutine)
_maxGoroutineIdleDuration time.Duration
_gopool = pool.NewGoPool(_maxGoroutinesAmount, _maxGoroutineIdleDuration)
)
// SetGopool set or reset go pool config.
// NOTE: Make sure to call it before calling NewPeer() and Go()
func SetGopool(maxGoroutinesAmount int, maxGoroutineIdleDuration time.Duration) {
_maxGoroutinesAmount, _maxGoroutineIdleDuration := maxGoroutinesAmount, maxGoroutineIdleDuration
if _gopool != nil {
_gopool.Stop()
}
_gopool = pool.NewGoPool(_maxGoroutinesAmount, _maxGoroutineIdleDuration)
}
// Go similar to go func, but return false if insufficient resources.
func Go(fn func()) bool {
if err := _gopool.Go(fn); err != nil {
Warnf("%s", err.Error())
return false
}
return true
}
// AnywayGo similar to go func, but concurrent resources are limited.
func AnywayGo(fn func()) {
TRYGO:
if !Go(fn) {
time.Sleep(time.Second)
goto TRYGO
}
}
// TryGo tries to execute the function via goroutine.
// If there are no concurrent resources, execute it synchronously.
func TryGo(fn func()) {
_gopool.TryGo(fn)
}
var printPidOnce sync.Once
func doPrintPid() {
printPidOnce.Do(func() {
Printf("The current process PID: %d", os.Getpid())
})
}
type fakeCallCmd struct {
output Message
result interface{}
rerr *Rerror
inputMeta *utils.Args
}
// NewFakeCallCmd creates a fake CallCmd.
func NewFakeCallCmd(serviceMethod string, arg, result interface{}, rerr *Rerror) CallCmd {
return &fakeCallCmd{
output: socket.NewMessage(
socket.WithMtype(TypeCall),
socket.WithServiceMethod(serviceMethod),
socket.WithBody(arg),
),
result: result,
rerr: rerr,
}
}
var closedChan = func() <-chan struct{} {
ch := make(chan struct{})
close(ch)
return ch
}()
// TracePeer trace back the peer.
func (f *fakeCallCmd) TracePeer() (Peer, bool) {
return nil, false
}
// TraceSession trace back the session.
func (f *fakeCallCmd) TraceSession() (Session, bool) {
return nil, false
}
// Done returns the chan that indicates whether it has been completed.
func (f *fakeCallCmd) Done() <-chan struct{} {
return closedChan
}
// Output returns writed message.
func (f *fakeCallCmd) Output() Message {
return f.output
}
// Context carries a deadline, a cancelation signal, and other values across
// API boundaries.
func (f *fakeCallCmd) Context() context.Context {
return f.output.Context()
}
// Reply returns the call reply.
func (f *fakeCallCmd) Reply() (interface{}, *Rerror) {
return f.result, f.rerr
}
// Rerror returns the call error.
func (f *fakeCallCmd) Rerror() *Rerror {
return f.rerr
}
// InputBodyCodec gets the body codec type of the input message.
func (f *fakeCallCmd) InputBodyCodec() byte {
return codec.NilCodecID
}
// InputMeta returns the header metadata of input message.
func (f *fakeCallCmd) InputMeta() *utils.Args {
if f.inputMeta == nil {
f.inputMeta = utils.AcquireArgs()
}
return f.inputMeta
}
// CostTime returns the called cost time.
// If PeerConfig.CountTime=false, always returns 0.
func (f *fakeCallCmd) CostTime() time.Duration {
return 0
}
// NewTLSConfigFromFile creates a new TLS config.
func NewTLSConfigFromFile(tlsCertFile, tlsKeyFile string, insecureSkipVerifyForClient ...bool) (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile)
if err != nil {
return nil, err
}
return newTLSConfig(cert, insecureSkipVerifyForClient...), nil
}
// GenerateTLSConfigForClient setup a bare-bones(skip verify) TLS config for client.
func GenerateTLSConfigForClient() *tls.Config {
return &tls.Config{InsecureSkipVerify: true}
}
// GenerateTLSConfigForServer setup a bare-bones TLS config for server.
func GenerateTLSConfigForServer() *tls.Config {
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
panic(err)
}
template := x509.Certificate{SerialNumber: big.NewInt(1)}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
panic(err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
panic(err)
}
return newTLSConfig(cert)
}
func newTLSConfig(cert tls.Certificate, insecureSkipVerifyForClient ...bool) *tls.Config {
var insecureSkipVerify bool
if len(insecureSkipVerifyForClient) > 0 {
insecureSkipVerify = insecureSkipVerifyForClient[0]
}
return &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
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,
},
}
}
// ListenerAddress a listener address plugin
type ListenerAddress struct {
addr net.Addr
host string
port string
}
var _ PostListenPlugin = new(ListenerAddress)
// Addr returns the address object.
func (la *ListenerAddress) Addr() net.Addr {
return la.addr
}
// Port returns the port.
func (la *ListenerAddress) Port() string {
return la.port
}
// Host returns the host.
func (la *ListenerAddress) Host() string {
return la.host
}
// String returns the address string.
func (la *ListenerAddress) String() string {
return la.addr.String()
}
// Name returns plugin name.
func (la *ListenerAddress) Name() string {
return "ListenerAddressPlugin"
}
// PostListen gets the listener address.
func (la *ListenerAddress) PostListen(addr net.Addr) (err error) {
la.addr = addr
la.host, la.port, err = net.SplitHostPort(addr.String())
return
}