mirror of
https://github.com/openziti/ziti.git
synced 2026-10-12 15:28:05 +00:00
105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
/*
|
|
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 handler_ctrl
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/netfoundry/ziti-fabric/pb/ctrl_pb"
|
|
"github.com/netfoundry/ziti-fabric/router/xgress"
|
|
"github.com/netfoundry/ziti-fabric/router/xlink"
|
|
"github.com/netfoundry/ziti-foundation/channel2"
|
|
"github.com/netfoundry/ziti-foundation/identity/identity"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type dialHandler struct {
|
|
id *identity.TokenId
|
|
ctrl xgress.CtrlChannel
|
|
dialers []xlink.Dialer
|
|
}
|
|
|
|
func newDialHandler(id *identity.TokenId, ctrl xgress.CtrlChannel, dialers []xlink.Dialer) *dialHandler {
|
|
return &dialHandler{
|
|
id: id,
|
|
ctrl: ctrl,
|
|
dialers: dialers,
|
|
}
|
|
}
|
|
|
|
func (self *dialHandler) ContentType() int32 {
|
|
return int32(ctrl_pb.ContentType_DialType)
|
|
}
|
|
|
|
func (self *dialHandler) HandleReceive(msg *channel2.Message, _ channel2.Channel) {
|
|
logrus.Info("received link connect request")
|
|
|
|
dial := &ctrl_pb.Dial{}
|
|
if err := proto.Unmarshal(msg.Body, dial); err == nil {
|
|
linkId := self.id.ShallowCloneWithNewToken(dial.Id)
|
|
if len(self.dialers) == 1 {
|
|
if err := self.dialers[0].Dial(dial.Address, linkId); err == nil {
|
|
if err := self.sendLinkMessage(linkId); err != nil {
|
|
logrus.Errorf("error sending link message [l/%s] (%v)", linkId.Token, err)
|
|
}
|
|
logrus.Infof("link [l/%s] established", linkId.Token)
|
|
|
|
} else {
|
|
logrus.Errorf("link dialing failed [%s] (%v)", dial.Address, err)
|
|
if err := self.sendLinkFault(linkId); err != nil {
|
|
logrus.Errorf("error sending fault [l/%s] (%v)", linkId.Token, err)
|
|
}
|
|
}
|
|
} else {
|
|
logrus.Errorf("invalid Xlink dialers configuration")
|
|
if err := self.sendLinkFault(linkId); err != nil {
|
|
logrus.Errorf("error sending link fault [l/%s] (%v)", linkId.Token, err)
|
|
}
|
|
}
|
|
} else {
|
|
logrus.Errorf("error unmarshaling dial message (%v)", err)
|
|
}
|
|
}
|
|
|
|
func (self *dialHandler) sendLinkMessage(linkId *identity.TokenId) error {
|
|
linkMsg := &ctrl_pb.Link{Id: linkId.Token}
|
|
body, err := proto.Marshal(linkMsg)
|
|
if err == nil {
|
|
msg := channel2.NewMessage(int32(ctrl_pb.ContentType_LinkType), body)
|
|
if err := self.ctrl.Channel().Send(msg); err != nil {
|
|
return fmt.Errorf("error sending link message (%w)", err)
|
|
}
|
|
} else {
|
|
return fmt.Errorf("error marshaling link message (%w)", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *dialHandler) sendLinkFault(linkId *identity.TokenId) error {
|
|
fault := &ctrl_pb.Fault{Subject: ctrl_pb.FaultSubject_LinkFault, Id: linkId.Token}
|
|
body, err := proto.Marshal(fault)
|
|
if err == nil {
|
|
msg := channel2.NewMessage(int32(ctrl_pb.ContentType_FaultType), body)
|
|
if err := self.ctrl.Channel().Send(msg); err != nil {
|
|
return fmt.Errorf("error sending fault (%w)", err)
|
|
}
|
|
} else {
|
|
return fmt.Errorf("error marshaling fault (%w)", err)
|
|
}
|
|
return nil
|
|
}
|