-
Notifications
You must be signed in to change notification settings - Fork 128
/
fabric.go
712 lines (587 loc) · 22.2 KB
/
fabric.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
Copyright NetFoundry Inc.
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
https://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 xgress_edge_tunnel
import (
"encoding/json"
"fmt"
"github.com/cenkalti/backoff/v4"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/channel/v2/protobufs"
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/foundation/v2/concurrenz"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/openziti/sdk-golang/ziti/sdkinfo"
"github.com/openziti/secretstream/kx"
"github.com/openziti/ziti/common/build"
"github.com/openziti/ziti/common/ctrl_msg"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/openziti/ziti/controller/idgen"
"github.com/openziti/ziti/router/posture"
"github.com/openziti/ziti/router/xgress"
"github.com/openziti/ziti/router/xgress_common"
"github.com/openziti/ziti/tunnel"
cmap "github.com/orcaman/concurrent-map/v2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"math"
"net"
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
)
func ToPtr[T any](in T) *T {
return &in
}
func newProvider(factory *Factory, tunneler *tunneler) *fabricProvider {
return &fabricProvider{
factory: factory,
tunneler: tunneler,
apiSessionTokens: map[string]string{},
dialSessions: cmap.New[string](),
bindSessions: cmap.New[string](),
}
}
type fabricProvider struct {
factory *Factory
tunneler *tunneler
apiSessionLock sync.Mutex
apiSessionTokens map[string]string
currentIdentity *rest_model.IdentityDetail
dialSessions cmap.ConcurrentMap[string, string]
bindSessions cmap.ConcurrentMap[string, string]
}
func (self *fabricProvider) getDialSession(serviceName string) string {
sessionId, _ := self.dialSessions.Get(serviceName)
return sessionId
}
func (self *fabricProvider) getBindSession(serviceName string) string {
sessionId, _ := self.bindSessions.Get(serviceName)
return sessionId
}
func (self *fabricProvider) updateApiSession(ctrlId string, resp *edge_ctrl_pb.CreateApiSessionResponse) bool {
self.apiSessionLock.Lock()
defer self.apiSessionLock.Unlock()
currentToken := self.apiSessionTokens[ctrlId]
if currentToken == resp.Token {
return false
}
self.tunneler.stateManager.RemoveConnectedApiSession(currentToken)
self.apiSessionTokens[ctrlId] = resp.Token
self.currentIdentity = &rest_model.IdentityDetail{
BaseEntity: rest_model.BaseEntity{
ID: &resp.IdentityId,
},
Name: &resp.IdentityName,
DefaultHostingPrecedence: rest_model.TerminatorPrecedence(strings.ToLower(resp.DefaultHostingPrecedence.String())),
DefaultHostingCost: ToPtr(rest_model.TerminatorCost(int64(resp.DefaultHostingCost))),
AppData: &rest_model.Tags{},
ServiceHostingPrecedences: rest_model.TerminatorPrecedenceMap{},
ServiceHostingCosts: rest_model.TerminatorCostMap{},
}
for k, v := range resp.ServicePrecedences {
self.currentIdentity.ServiceHostingPrecedences[k] = v.GetZitiLabel()
}
for k, v := range resp.ServiceCosts {
self.currentIdentity.ServiceHostingCosts[k] = ToPtr(rest_model.TerminatorCost(v))
}
if resp.AppDataJson != "" {
decoder := json.NewDecoder(strings.NewReader(resp.AppDataJson))
err := decoder.Decode(&self.currentIdentity.AppData)
if err != nil {
logrus.WithError(err).Errorf("failed to decode appDataJson: '%v'", resp.AppDataJson)
}
}
self.tunneler.stateManager.AddConnectedApiSession(resp.Token)
return true
}
func (self *fabricProvider) authenticate() error {
envInfo, _ := sdkinfo.GetSdkInfo()
buildInfo := build.GetBuildInfo()
osVersion := "unknown"
osRelease := "unknown"
if envInfo.OsVersion != "" {
osVersion = envInfo.OsVersion
}
if envInfo.OsRelease != "" {
osRelease = envInfo.OsRelease
}
request := &edge_ctrl_pb.CreateApiSessionRequest{
EnvInfo: &edge_ctrl_pb.EnvInfo{
Os: runtime.GOOS,
Arch: runtime.GOARCH,
OsVersion: osVersion,
OsRelease: osRelease,
},
SdkInfo: &edge_ctrl_pb.SdkInfo{
AppId: "ziti-router",
AppVersion: buildInfo.Version(),
Branch: buildInfo.Branch(),
Revision: buildInfo.Revision(),
Type: "ziti-router:tunnel",
Version: buildInfo.Version(),
},
ConfigTypes: []string{
"f2dd2df0-9c04-4b84-a91e-71437ac229f1", // client v1
"cea49285-6c07-42cf-9f52-09a9b115c783", // server v1
"g7cIWbcGg", // intercept.v1
"NH5p4FpGR", // host.v1
"host.v2", // host.v2
},
}
ctrlMap := self.factory.ctrls.GetAll()
ctrlChMap := map[string]channel.Channel{}
for k, v := range ctrlMap {
ctrlChMap[k] = v.Channel()
}
if len(ctrlChMap) == 0 {
ctrlCh := self.factory.ctrls.AnyValidCtrlChannel()
ctrlChMap[ctrlCh.Id()] = ctrlCh
}
results := newAuthResults(len(ctrlChMap))
for k, v := range ctrlChMap {
ctrlId := k
ctrlCh := v
go func() {
respMsg, err := protobufs.MarshalTyped(request).WithTimeout(30 * time.Second).SendForReply(ctrlCh)
resp := &edge_ctrl_pb.CreateApiSessionResponse{}
if err = xgress_common.GetResultOrFailure(respMsg, err, resp); err != nil {
results.Error(err)
pfxlog.Logger().WithError(err).WithField("ctrlId", ctrlId).Error("failed to authenticate")
return
}
self.updateApiSession(ctrlId, resp)
results.Success()
}()
}
return results.GetResults()
}
func (self *fabricProvider) PrepForUse(string) {}
func (self *fabricProvider) GetCurrentIdentity() (*rest_model.IdentityDetail, error) {
return self.currentIdentity, nil
}
func (self *fabricProvider) GetCurrentIdentityWithBackoff() (*rest_model.IdentityDetail, error) {
return self.currentIdentity, nil
}
func (self *fabricProvider) TunnelService(service tunnel.Service, terminatorInstanceId string, conn net.Conn, halfClose bool, appData []byte) error {
keyPair, err := kx.NewKeyPair()
if err != nil {
return err
}
log := logrus.WithField("service", service.GetName())
peerData := make(map[uint32][]byte)
if service.IsEncryptionRequired() {
peerData[edge.PublicKeyHeader] = keyPair.Public()
}
if len(appData) > 0 {
peerData[edge.AppDataHeader] = appData
}
peerData[uint32(ctrl_msg.InitiatorLocalAddressHeader)] = []byte(conn.LocalAddr().String())
peerData[uint32(ctrl_msg.InitiatorRemoteAddressHeader)] = []byte(conn.RemoteAddr().String())
ctrlCh := self.factory.ctrls.AnyCtrlChannel()
if ctrlCh == nil {
errStr := "no controller available, cannot create circuit"
log.Error(errStr)
return errors.New(errStr)
}
if self.factory.routerConfig.Ha.Enabled {
return self.tunnelServiceV2(service, terminatorInstanceId, conn, halfClose, ctrlCh, peerData, keyPair)
}
return self.tunnelServiceV1(service, terminatorInstanceId, conn, halfClose, ctrlCh, peerData, keyPair)
}
func (self *fabricProvider) tunnelServiceV1(
service tunnel.Service,
terminatorInstanceId string,
conn net.Conn,
halfClose bool,
ctrlCh channel.Channel,
peerData map[uint32][]byte,
keyPair *kx.KeyPair) error {
log := logrus.WithField("service", service.GetName())
sessionId := self.getDialSession(service.GetName())
request := &edge_ctrl_pb.CreateCircuitForServiceRequest{
SessionId: sessionId,
ServiceName: service.GetName(),
TerminatorInstanceId: terminatorInstanceId,
PeerData: peerData,
}
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(service.GetDialTimeout()).SendForReply(ctrlCh)
response := &edge_ctrl_pb.CreateCircuitForServiceResponse{}
if err = xgress_common.GetResultOrFailure(responseMsg, err, response); err != nil {
log.WithError(err).Warn("failed to dial fabric")
return err
}
if response.ApiSession != nil {
if self.updateApiSession(ctrlCh.Id(), response.ApiSession) {
log.WithField("apiSessionId", response.ApiSession.SessionId).Info("received new apiSession")
}
}
if response.Session != nil && response.Session.SessionId != sessionId {
log.WithField("sessionId", response.Session.SessionId).Info("received new session")
self.dialSessions.Set(service.GetName(), response.Session.SessionId)
}
peerKey, peerKeyFound := response.PeerData[edge.PublicKeyHeader]
if service.IsEncryptionRequired() && !peerKeyFound {
return errors.New("service requires encryption, but public key header not returned")
}
xgConn := xgress_common.NewXgressConn(conn, halfClose, false)
if peerKeyFound {
if err = xgConn.SetupClientCrypto(keyPair, peerKey); err != nil {
return err
}
}
cleanupCallback := self.tunneler.stateManager.AddEdgeSessionRemovedListener(response.Session.Token, func(token string) {
if err = conn.Close(); err != nil {
log.WithError(err).Error("failed to close external conn when session closed")
}
})
x := xgress.NewXgress(response.CircuitId, ctrlCh.Id(), xgress.Address(response.Address), xgConn, xgress.Initiator, self.tunneler.listenOptions.Options, response.Tags)
self.tunneler.bindHandler.HandleXgressBind(x)
x.AddCloseHandler(xgress.CloseHandlerF(func(x *xgress.Xgress) { cleanupCallback() }))
x.Start()
return nil
}
func (self *fabricProvider) tunnelServiceV2(
service tunnel.Service,
terminatorInstanceId string,
conn net.Conn,
halfClose bool,
ctrlCh channel.Channel,
peerData map[uint32][]byte,
keyPair *kx.KeyPair) error {
log := logrus.WithField("service", service.GetName())
rdm := self.factory.stateManager.RouterDataModel()
if policy, err := posture.HasAccess(rdm, self.factory.routerConfig.Id.Token, service.GetId(), nil, edge_ctrl_pb.PolicyType_DialPolicy); err != nil && policy != nil {
return fmt.Errorf("router does not have access to service '%s' (%w)", service.GetName(), err)
}
request := &edge_ctrl_pb.CreateTunnelCircuitV2Request{
ServiceName: service.GetName(),
TerminatorInstanceId: terminatorInstanceId,
PeerData: peerData,
}
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(service.GetDialTimeout()).SendForReply(ctrlCh)
response := &edge_ctrl_pb.CreateTunnelCircuitV2Response{}
if err = xgress_common.GetResultOrFailure(responseMsg, err, response); err != nil {
log.WithError(err).Warn("failed to dial fabric")
return err
}
peerKey, peerKeyFound := response.PeerData[edge.PublicKeyHeader]
if service.IsEncryptionRequired() && !peerKeyFound {
return errors.New("service requires encryption, but public key header not returned")
}
xgConn := xgress_common.NewXgressConn(conn, halfClose, false)
if peerKeyFound {
if err = xgConn.SetupClientCrypto(keyPair, peerKey); err != nil {
return err
}
}
x := xgress.NewXgress(response.CircuitId, ctrlCh.Id(), xgress.Address(response.Address), xgConn, xgress.Initiator, self.tunneler.listenOptions.Options, response.Tags)
self.tunneler.bindHandler.HandleXgressBind(x)
x.Start()
return nil
}
func (self *fabricProvider) HostService(hostCtx tunnel.HostingContext) (tunnel.HostControl, error) {
id := idgen.NewUUIDString()
terminator := &tunnelTerminator{
id: id,
provider: self,
context: hostCtx,
notifyCreated: make(chan struct{}, 1),
}
self.tunneler.terminators.Set(terminator.id, terminator)
err := self.factory.env.GetRateLimiterPool().QueueWithTimeout(func() {
self.establishTerminatorWithRetry(terminator)
}, math.MaxInt64)
if err != nil { // should only happen if router is shutting down
self.tunneler.terminators.Remove(terminator.id)
return nil, err
}
return terminator, nil
}
func (self *fabricProvider) establishTerminatorWithRetry(terminator *tunnelTerminator) {
log := logrus.WithField("service", terminator.context.ServiceName())
if terminator.closed.Load() {
log.Info("not attempting to establish terminator, service not hostable")
return
}
operation := func() error {
var err error
if !terminator.created.Load() {
log.Info("attempting to establish terminator")
err = self.establishTerminator(terminator)
if err != nil && terminator.closed.Load() {
return backoff.Permanent(err)
}
}
return err
}
expBackoff := backoff.NewExponentialBackOff()
expBackoff.InitialInterval = 5 * time.Second
expBackoff.MaxInterval = 5 * time.Minute
if err := backoff.Retry(operation, expBackoff); err != nil {
log.WithError(err).Error("stopping attempts to establish terminator, service not hostable")
}
}
func (self *fabricProvider) establishTerminator(terminator *tunnelTerminator) error {
start := time.Now().UnixMilli()
log := pfxlog.Logger().
WithField("routerId", self.factory.id.Token).
WithField("service", terminator.context.ServiceName()).
WithField("terminatorId", terminator.id)
precedence := edge_ctrl_pb.TerminatorPrecedence_Default
if terminator.context.ListenOptions().Precedence == ziti.PrecedenceRequired {
precedence = edge_ctrl_pb.TerminatorPrecedence_Required
} else if terminator.context.ListenOptions().Precedence == ziti.PrecedenceFailed {
precedence = edge_ctrl_pb.TerminatorPrecedence_Failed
}
sessionId := self.getBindSession(terminator.context.ServiceName())
request := &edge_ctrl_pb.CreateTunnelTerminatorRequest{
ServiceName: terminator.context.ServiceName(),
SessionId: sessionId,
Address: terminator.id,
Cost: uint32(terminator.context.ListenOptions().Cost),
Precedence: precedence,
InstanceId: terminator.context.ListenOptions().Identity,
StartTime: start,
}
ctrlCh := self.factory.ctrls.AnyCtrlChannel()
if ctrlCh == nil {
errStr := "no controller available, cannot create terminator"
log.Error(errStr)
return errors.New(errStr)
}
err := protobufs.MarshalTyped(request).WithTimeout(self.factory.DefaultRequestTimeout()).SendAndWaitForWire(ctrlCh)
if err != nil {
return err
}
if terminator.WaitForCreated(10 * time.Second) {
return nil
}
// return an error to indicate that we need to check if a response has come back after the next interval,
// and if not, re-send
return errors.Errorf("timeout waiting for response to create terminator request for terminator %v on service %v",
terminator.id, terminator.context.ServiceName())
}
func (self *fabricProvider) HandleTunnelResponse(msg *channel.Message, ctrlCh channel.Channel) {
log := pfxlog.Logger().WithField("routerId", self.factory.id.Token)
response := &edge_ctrl_pb.CreateTunnelTerminatorResponse{}
if err := proto.Unmarshal(msg.Body, response); err != nil {
log.WithError(err).Error("error unmarshalling create tunnel terminator response")
return
}
if response.ApiSession != nil {
if self.updateApiSession(ctrlCh.Id(), response.ApiSession) {
log.WithField("apiSessionId", response.ApiSession.SessionId).Info("received new api-session")
}
}
log = log.WithField("terminatorId", response.TerminatorId)
terminator, found := self.factory.tunneler.terminators.Get(response.TerminatorId)
if !found {
log.Error("no terminator found for id")
return
}
sessionId, _ := self.bindSessions.Get(terminator.context.ServiceName())
if response.Session != nil && response.Session.SessionId != sessionId {
log.WithField("sessionId", response.Session.SessionId).Info("received new session")
self.bindSessions.Set(terminator.context.ServiceName(), response.Session.SessionId)
}
if terminator.created.CompareAndSwap(false, true) {
// TODO: How do we make sure we don't get dups here. Probably not a big problem and this will need to be refactored for JWT sessions in any case
closeCallback := self.tunneler.stateManager.AddEdgeSessionRemovedListener(response.Session.Token, func(token string) {
if err := self.removeTerminator(terminator); err != nil {
log.WithError(err).Error("failed to remove terminator after edge session was removed")
}
terminator.created.Store(false)
go self.establishTerminatorWithRetry(terminator)
})
terminator.closeCallback.Store(closeCallback)
if response.StartTime > 0 {
elapsedTime := time.Since(time.UnixMilli(response.StartTime))
log = log.WithField("createDuration", elapsedTime)
self.factory.metricsRegistry.Timer("xgress_edge_tunnel.terminator.create_timer").Update(elapsedTime)
}
log.Info("received terminator created notification")
} else {
log.Info("received additional terminator created notification")
}
terminator.NotifyCreated()
}
func (self *fabricProvider) removeTerminator(terminator *tunnelTerminator) error {
ctrlCh := self.factory.ctrls.AnyCtrlChannel()
if ctrlCh == nil {
return errors.New("no controller available, cannot remove terminator")
}
msg := channel.NewMessage(int32(edge_ctrl_pb.ContentType_RemoveTunnelTerminatorRequestType), []byte(terminator.id))
responseMsg, err := msg.WithTimeout(self.factory.DefaultRequestTimeout()).SendForReply(ctrlCh)
return xgress_common.CheckForFailureResult(responseMsg, err, edge_ctrl_pb.ContentType_RemoveTunnelTerminatorResponseType)
}
func (self *fabricProvider) updateTerminator(terminatorId string, cost *uint16, precedence *edge.Precedence) error {
ctrlCh := self.factory.ctrls.AnyCtrlChannel()
if ctrlCh == nil {
return errors.New("no controller available, cannot update terminator")
}
request := &edge_ctrl_pb.UpdateTunnelTerminatorRequest{
TerminatorId: terminatorId,
}
if cost != nil {
request.Cost = uint32(*cost)
request.UpdateCost = true
}
if precedence != nil {
request.Precedence = edge_ctrl_pb.TerminatorPrecedence_Default
request.UpdatePrecedence = true
if *precedence == edge.PrecedenceRequired {
request.Precedence = edge_ctrl_pb.TerminatorPrecedence_Required
} else if *precedence == edge.PrecedenceFailed {
request.Precedence = edge_ctrl_pb.TerminatorPrecedence_Failed
}
}
log := logrus.WithField("terminator", terminatorId).
WithField("precedence", request.Precedence).
WithField("cost", request.Cost).
WithField("updatingPrecedence", request.UpdatePrecedence).
WithField("updatingCost", request.UpdateCost)
log.Debug("updating terminator")
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(self.factory.DefaultRequestTimeout()).SendForReply(ctrlCh)
if err := xgress_common.CheckForFailureResult(responseMsg, err, edge_ctrl_pb.ContentType_UpdateTunnelTerminatorResponseType); err != nil {
log.WithError(err).Error("terminator update failed")
return err
}
log.Debug("terminator updated successfully")
return nil
}
func (self *fabricProvider) sendHealthEvent(terminatorId string, checkPassed bool) error {
ctrlCh := self.factory.ctrls.AnyCtrlChannel()
if ctrlCh == nil {
return errors.New("no controller available, cannot forward health event")
}
msg := channel.NewMessage(int32(edge_ctrl_pb.ContentType_TunnelHealthEventType), nil)
msg.Headers[int32(edge_ctrl_pb.Header_TerminatorId)] = []byte(terminatorId)
msg.PutBoolHeader(int32(edge_ctrl_pb.Header_CheckPassed), checkPassed)
logger := logrus.WithField("terminator", terminatorId).
WithField("checkPassed", checkPassed)
logger.Debug("sending health event")
if err := msg.WithTimeout(self.factory.ctrls.DefaultRequestTimeout()).Send(ctrlCh); err != nil {
logger.WithError(err).Error("health event send failed")
} else {
logger.Debug("health event sent")
}
return nil
}
func (self *fabricProvider) requestServiceList(ctrlCh channel.Channel, lastUpdateToken []byte) {
msg := channel.NewMessage(int32(edge_ctrl_pb.ContentType_ListServicesRequestType), lastUpdateToken)
if err := ctrlCh.Send(msg); err != nil {
logrus.WithError(err).Error("failed to send service list request to controller")
}
}
type tunnelTerminator struct {
id string
provider *fabricProvider
context tunnel.HostingContext
created atomic.Bool
closeCallback concurrenz.AtomicValue[func()]
closed atomic.Bool
notifyCreated chan struct{}
}
func (self *tunnelTerminator) SendHealthEvent(pass bool) error {
return self.provider.sendHealthEvent(self.id, pass)
}
func (self *tunnelTerminator) NotifyCreated() {
select {
case self.notifyCreated <- struct{}{}:
default:
}
}
func (self *tunnelTerminator) WaitForCreated(timeout time.Duration) bool {
if self.created.Load() {
return true
}
select {
case <-self.notifyCreated:
case <-time.After(timeout):
}
return self.created.Load()
}
func (self *tunnelTerminator) Close() error {
if self.closed.CompareAndSwap(false, true) {
log := logrus.WithField("service", self.context.ServiceName()).
WithField("routerId", self.provider.factory.id.Token).
WithField("terminatorId", self.id)
log.Debug("closing tunnel terminator context")
self.context.OnClose()
if cb := self.closeCallback.Load(); cb != nil {
log.Debug("unregistering session listener for tunnel terminator")
cb()
}
log.Debug("removing tunnel terminator")
if err := self.provider.removeTerminator(self); err != nil {
log.WithError(err).Error("error while removing tunnel terminator")
return err
}
log.Info("removed tunnel terminator")
return nil
}
return nil
}
func (self *tunnelTerminator) UpdateCost(cost uint16) error {
return self.updateCostAndPrecedence(&cost, nil)
}
func (self *tunnelTerminator) UpdatePrecedence(precedence edge.Precedence) error {
return self.updateCostAndPrecedence(nil, &precedence)
}
func (self *tunnelTerminator) UpdateCostAndPrecedence(cost uint16, precedence edge.Precedence) error {
return self.updateCostAndPrecedence(&cost, &precedence)
}
func (self *tunnelTerminator) updateCostAndPrecedence(cost *uint16, precedence *edge.Precedence) error {
return self.provider.updateTerminator(self.id, cost, precedence)
}
func newAuthResults(count int) *authResults {
return &authResults{
ctrlCount: count,
okCh: make(chan interface{}, 1),
errCh: make(chan error, count),
}
}
type authResults struct {
ctrlCount int
okCh chan interface{}
errCh chan error
}
func (self *authResults) Success() {
select {
case self.okCh <- nil:
default:
}
}
func (self *authResults) Error(err error) {
self.errCh <- err
}
func (self *authResults) GetResults() error {
var errList []error
for {
select {
case <-self.okCh: // if any succeeded, return nil
return nil
case err := <-self.errCh:
errList = append(errList, err)
if len(errList) == self.ctrlCount {
return errorz.MultipleErrors(errList)
}
}
}
}