-
Notifications
You must be signed in to change notification settings - Fork 410
/
helper.go
337 lines (291 loc) · 8.42 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
// Copyright 2015-2019 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 erpc
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"math/big"
"net"
"os"
"sync"
"time"
"github.com/andeya/erpc/v7/codec"
"github.com/andeya/erpc/v7/socket"
"github.com/andeya/erpc/v7/utils"
"github.com/andeya/goutil/pool"
)
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()) {
_gopool.MustGo(fn)
}
// MustGo always try to use goroutine callbacks
// until execution is complete or the context is canceled.
func MustGo(fn func(), ctx ...context.Context) error {
return _gopool.MustGo(fn, ctx...)
}
// 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{}
stat *Status
inputMeta *utils.Args
}
// NewFakeCallCmd creates a fake CallCmd.
func NewFakeCallCmd(serviceMethod string, arg, result interface{}, stat *Status) CallCmd {
return &fakeCallCmd{
output: socket.NewMessage(
withMtype(TypeCall),
socket.WithServiceMethod(serviceMethod),
socket.WithBody(arg),
),
result: result,
stat: stat,
}
}
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{}, *Status) {
return f.result, f.stat
}
// StatusOK returns the call status is OK or not.
func (f *fakeCallCmd) StatusOK() bool {
return f.stat.OK()
}
// Status returns the call error.
func (f *fakeCallCmd) Status() *Status {
return f.stat
}
// 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 "ListenerAddress"
}
// 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
}
// FakeAddr is a fake address object that implements net.Add interface
type FakeAddr struct {
network string
addr string
host string
port string
udpAddr *net.UDPAddr
}
var _ net.Addr = (*FakeAddr)(nil)
// NewFakeAddr creates an object that implements net.Add interface.
func NewFakeAddr(network, host, port string) *FakeAddr {
if network == "" {
network = "tcp"
}
if host == "" {
host = "0.0.0.0"
}
if port == "" {
port = "0"
}
addr := net.JoinHostPort(host, port)
return &FakeAddr{
network: network,
addr: addr,
host: host,
port: port,
}
}
// NewFakeAddr2 creates an object that implements net.Add interface.
func NewFakeAddr2(network, addr string) (*FakeAddr, error) {
if addr == "" {
return NewFakeAddr(network, "", ""), nil
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
return NewFakeAddr(network, host, port), nil
}
// Network returns the address's network name.
func (f *FakeAddr) Network() string {
return f.network
}
// String returns the string form of address.
func (f *FakeAddr) String() string {
return f.addr
}
// Host returns the address's host(ip).
func (f *FakeAddr) Host() string {
return f.host
}
// Port returns the address's port.
func (f *FakeAddr) Port() string {
return f.port
}