-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
433 lines (390 loc) · 10.3 KB
/
server.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
package adaptiveservice
import (
"crypto/rand"
"encoding/hex"
"errors"
"net"
"os"
"reflect"
"strings"
"sync"
)
// Server provides services.
type Server struct {
sync.Mutex
*conf
publisher string
broadcastPort string
rootRegistry bool
autoReverseProxy bool
serviceLister bool
ipObserver bool
msgTracer bool
errRecovers chan errorRecover
mq *msgQ
residentWorkers int
qSizePerCore int
qWeight int
msgTypeCheck bool
closers []closer
initialized bool
closed chan struct{}
}
const (
// DefaultQSizePerCore is the default value for qSizePerCore
DefaultQSizePerCore int = 128
// DefaultQWeight is the default value for qWeight
DefaultQWeight int = 8
)
// NewServer creates a server which publishes services.
func NewServer(options ...Option) *Server {
s := &Server{
conf: newConf(),
publisher: "default.org",
errRecovers: make(chan errorRecover, 1),
residentWorkers: 1,
qSizePerCore: DefaultQSizePerCore,
qWeight: DefaultQWeight,
msgTypeCheck: true,
closed: make(chan struct{}),
}
for _, o := range options {
o(s.conf)
}
s.lg.Debugf("new server created")
return s
}
func genID() string {
var b []byte
itfs, _ := net.Interfaces()
for _, itf := range itfs {
dev, err := os.Readlink("/sys/class/net/" + itf.Name)
if err != nil || strings.Contains(dev, "virtual") {
continue
}
if len(itf.HardwareAddr) != 0 {
b = itf.HardwareAddr
break
}
}
if len(b) == 0 {
b = make([]byte, 6)
rand.Read(b)
}
id := hex.EncodeToString(b)
return id
}
func (s *Server) init() error {
if s.initialized {
return nil
}
s.initialized = true
initSigCleaner(s.lg)
addSigCloser(s)
s.mq = newMsgQ(s.residentWorkers, s.qSizePerCore, s.qWeight, s.lg)
s.addCloser(s.mq)
if s.scope&ScopeLAN == ScopeLAN {
s.lg.Infof("configuring server in local network scope")
c := NewClient(WithScope(ScopeProcess|ScopeOS), WithLogger(s.lg)).SetDiscoverTimeout(0)
conn := <-c.Discover(BuiltinPublisher, SrvLANRegistry)
if conn != nil {
conn.Close()
s.lg.Infof("LAN registry running")
} else {
if len(s.broadcastPort) == 0 {
s.lg.Infof("LAN registry not found or configured")
s.scope &= ^ScopeLAN // not ScopeLAN
} else {
if err := s.publishLANRegistryService(); err != nil {
return err
}
s.lg.Infof("user specified broadcast port: %s, LAN registry service started", s.broadcastPort)
}
}
}
if s.scope&ScopeWAN == ScopeWAN {
s.lg.Infof("configuring server in public network scope")
if addr, err := discoverRegistryAddr(s.lg); err != nil {
if len(s.registryAddr) == 0 {
s.lg.Infof("root registry address not found or configured")
s.scope &= ^ScopeWAN // not ScopeWAN
} else {
if err := s.publishRegistryInfoService(); err != nil {
return err
}
s.lg.Infof("user specified root registry address: %s, registry info service started", s.registryAddr)
}
} else {
s.registryAddr = addr
s.lg.Infof("discovered root registry address: %s", addr)
}
}
if s.scope&ScopeLAN == ScopeLAN || s.scope&ScopeWAN == ScopeWAN {
if len(s.providerID) != 0 {
s.lg.Infof("user specified provider ID: %s", s.providerID)
} else if id, err := discoverProviderID(s.lg); err != nil {
if len(s.providerID) == 0 {
s.providerID = genID()
}
if err := s.publishProviderInfoService(); err != nil {
return err
}
s.lg.Infof("provider info service started with provider ID: %s", s.providerID)
} else {
s.providerID = id
s.lg.Infof("discovered provider ID: %s", s.providerID)
}
}
if s.rootRegistry {
if len(s.registryAddr) == 0 {
return errors.New("root registry address not configured")
}
if s.scope&ScopeWAN != ScopeWAN {
panic("scope error")
}
_, port, _ := net.SplitHostPort(s.registryAddr)
if err := s.startRootRegistry(port); err != nil {
return err
}
s.lg.Infof("root registry started at %s", port)
}
if s.scope&ScopeWAN == ScopeWAN && s.autoReverseProxy {
canProxy := func() bool {
if s.rootRegistry {
return true
}
network := 0
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
if ip.To4() != nil && !ip.IsLoopback() {
network++
}
}
if network < 2 {
s.lg.Debugf("reverse proxy not needed in less than 2 networks")
return false
}
lnr, err := net.Listen("tcp", ":0")
if err != nil {
s.lg.Debugf("auto reverse proxy: listen error: %v", err)
return false
}
defer lnr.Close()
go func() {
for {
netconn, err := lnr.Accept()
if err != nil {
return
}
netconn.Close()
}
}()
addr := lnr.Addr().String()
_, port, _ := net.SplitHostPort(addr) // from [::]:43807
c := NewClient(WithScope(ScopeWAN), WithLogger(s.lg))
conn, err := c.newTCPConnection(s.registryAddr)
if err != nil {
s.lg.Debugf("root registry not reachable: %v", err)
return false
}
defer conn.Close()
if err := conn.SendRecv(&testReverseProxy{port: port}, nil); err != nil {
s.lg.Debugf("reverse port not reachable: %v", err)
return false
}
return true
}
if canProxy() {
scope := ScopeLAN
if s.rootRegistry {
scope |= ScopeWAN
}
if err := s.publishReverseProxyService(scope); err != nil {
return err
}
s.lg.Infof("reverse proxy started")
}
}
if s.serviceLister {
if err := s.publishServiceListerService(ScopeProcess | ScopeOS); err != nil {
return err
}
s.lg.Infof("service lister started")
}
if s.ipObserver {
if err := s.publishIPObserverService(); err != nil {
return err
}
s.lg.Infof("IP observer started")
}
if s.msgTracer {
if err := s.publishMessageTracingService(); err != nil {
return err
}
s.lg.Infof("message tracer started")
}
mTraceHelper.init(s.lg)
s.lg.Debugf("server initialized")
return nil
}
type service struct {
publisherName string
serviceName string
providerID string
knownMsgTypes map[reflect.Type]struct{}
s *Server
scope Scope
fnOnNewStream func(Context) // called on new stream accepted
fnOnStreamClose func(Context) // called on stream closed
fnOnConnect func(Netconn) (takeOver bool) // called on new connection established
fnOnDisconnect func(Netconn) // called on connection disconnected
}
func (svc *service) canHandle(msg interface{}) bool {
if _, ok := msg.(KnownMessage); !ok {
return false
}
if svc.s.msgTypeCheck {
_, has := svc.knownMsgTypes[reflect.TypeOf(msg)]
return has
}
return true
}
func (s *Server) publish(scope Scope, publisherName, serviceName string, knownMessages []KnownMessage, options ...ServiceOption) error {
s.lg.Debugf("publishing %s %s in scope %b", publisherName, serviceName, scope)
if !s.initialized {
if err := s.init(); err != nil {
return err
}
}
scope = s.scope & scope
s.lg.Debugf("adjusted %s %s in scope %b", publisherName, serviceName, scope)
newService := func() *service {
svc := &service{
publisherName: publisherName,
serviceName: serviceName,
providerID: s.providerID,
knownMsgTypes: make(map[reflect.Type]struct{}),
s: s,
scope: scope,
}
knownMessages = append(knownMessages, QueryMsgQInfo{})
for _, msg := range knownMessages {
tp := reflect.TypeOf(msg)
svc.knownMsgTypes[tp] = struct{}{}
}
return svc
}
svc := newService()
for _, opt := range options {
opt(svc)
}
if scope&ScopeProcess == ScopeProcess {
tran, err := svc.newChanTransport()
if err != nil {
return err
}
s.addCloser(tran)
}
if scope&ScopeOS == ScopeOS {
tran, err := svc.newUDSTransport()
if err != nil {
return err
}
s.addCloser(tran)
}
if scope&ScopeLAN == ScopeLAN || scope&ScopeWAN == ScopeWAN {
tran, err := svc.newTCPTransport("")
if err != nil {
return err
}
s.addCloser(tran)
}
return nil
}
func checkNameConvention(name string) error {
if strings.ContainsAny(name, "_/ \n\r\t") {
return errors.New("name should not contain _ or / or whitespace")
}
return nil
}
// Publish publishes service to all available scope of Server s.
// knownMessages are messages that the service can handle, e.g.
// []KnownMessage{(*PublicStructA)(nil), (*PublicStructB)(nil), ...},
// where (*PublicStructA) and (*PublicStructB) are the known messages that
// have `Handle(stream ContextStream) reply interface{}` method.
//
// Publish panics if serviceName contains "_" or "/" or whitespace.
func (s *Server) Publish(serviceName string, knownMessages []KnownMessage, options ...ServiceOption) error {
if err := checkNameConvention(serviceName); err != nil {
panic(err)
}
return s.publish(s.scope, s.publisher, serviceName, knownMessages, options...)
}
// PublishIn is like Publish, but with specified scope which should be
// a subset of the scope of Server s.
func (s *Server) PublishIn(scope Scope, serviceName string, knownMessages []KnownMessage, options ...ServiceOption) error {
if err := checkNameConvention(serviceName); err != nil {
panic(err)
}
return s.publish(scope, s.publisher, serviceName, knownMessages, options...)
}
// Serve starts serving.
func (s *Server) Serve() error {
if !s.initialized {
if err := s.init(); err != nil {
return err
}
}
defer s.doClose()
s.lg.Infof("server in serve")
for e := range s.errRecovers {
if (e == noError{}) {
break
}
if e.Recover() {
s.lg.Infof("error recovered: %s : %v", e.String(), e.Error())
} else {
s.lg.Errorf("error not recovered: %s : %v", e.String(), e.Error())
return e.Error()
}
}
return nil
}
func (s *Server) addCloser(closer closer) {
s.closers = append(s.closers, closer)
}
// Close triggers the close procedure of the server.
func (s *Server) Close() {
if s.closers == nil {
return
}
mTraceHelper.tryWait()
s.errRecovers <- noError{}
}
// CloseWait triggers the close procedure and waits
// until the server is fully closed.
func (s *Server) CloseWait() {
s.Close()
<-s.closed
}
func (s *Server) close() {
if s.closers == nil {
return
}
mTraceHelper.tryWait()
s.errRecovers <- unrecoverableError{ErrServerClosed}
}
func (s *Server) doClose() {
s.Lock()
defer s.Unlock()
if s.closers == nil {
return
}
for _, closer := range s.closers {
closer.close()
}
s.lg.Infof("server closed")
s.closers = nil
close(s.closed)
}