mirror of
https://github.com/hanzozt/zt.git
synced 2026-11-02 11:38:22 +00:00
- Repo names: ziti-* → zt-* - Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt - Docker images: hanzozt/ziti-* → hanzozt/zt-* - C/Java/JS symbols: ziti_* → zt_* - File and directory names updated
96 lines
3.1 KiB
Go
96 lines
3.1 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 xgress_edge_tunnel
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/michaelquigley/pfxlog"
|
|
"github.com/hanzozt/sdk-golang/xgress"
|
|
"github.com/hanzozt/sdk-golang/zt/edge"
|
|
"github.com/hanzozt/zt/v2/common/ctrl_msg"
|
|
"github.com/hanzozt/zt/v2/common/inspect"
|
|
"github.com/hanzozt/zt/v2/common/logcontext"
|
|
"github.com/hanzozt/zt/v2/controller/xt"
|
|
"github.com/hanzozt/zt/v2/router/xgress_common"
|
|
"github.com/hanzozt/zt/v2/router/xgress_router"
|
|
"github.com/hanzozt/zt/v2/tunnel"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (self *tunneler) IsTerminatorValid(id string, destination string) bool {
|
|
self.WaitForInitialized()
|
|
terminator, found := self.hostedServices.Get(id)
|
|
if found {
|
|
self.hostedServices.markEstablished(terminator, "validation message received")
|
|
}
|
|
return found
|
|
}
|
|
|
|
func (self *tunneler) Dial(params xgress_router.DialParams) (xt.PeerData, error) {
|
|
destination := params.GetDestination()
|
|
circuitId := params.GetCircuitId()
|
|
|
|
log := pfxlog.ChannelLogger(logcontext.EstablishPath).Wire(params.GetLogContext()).
|
|
WithField("binding", "tunnel").
|
|
WithField("destination", destination)
|
|
|
|
terminator, ok := self.hostedServices.Get(destination)
|
|
if !ok {
|
|
return nil, xgress.InvalidTerminatorError{InnerError: errors.Errorf("tunnel terminator for destination %v not found", destination)}
|
|
}
|
|
|
|
options, err := tunnel.AppDataToMap(circuitId.Data[uint32(edge.AppDataHeader)])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//TODO: Figure out timeout
|
|
conn, halfClose, err := terminator.context.Dial(options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Debugf("successful connection %v->%v for destination %v", conn.LocalAddr(), conn.RemoteAddr(), destination)
|
|
|
|
xgConn := xgress_common.NewXgressConn(conn, halfClose, false)
|
|
peerData := make(xt.PeerData, 3)
|
|
if peerKey, ok := circuitId.Data[uint32(edge.PublicKeyHeader)]; ok {
|
|
if publicKey, err := xgConn.SetupServerCrypto(peerKey); err != nil {
|
|
return nil, err
|
|
} else {
|
|
peerData[uint32(edge.PublicKeyHeader)] = publicKey
|
|
}
|
|
}
|
|
|
|
peerData[uint32(ctrl_msg.TerminatorLocalAddressHeader)] = []byte(conn.LocalAddr().String())
|
|
peerData[uint32(ctrl_msg.TerminatorRemoteAddressHeader)] = []byte(conn.RemoteAddr().String())
|
|
|
|
x := xgress.NewXgress(circuitId.Token, params.GetCtrlId(), params.GetAddress(), xgConn, xgress.Terminator, self.dialOptions.Options, params.GetCircuitTags())
|
|
params.GetBindHandler().HandleXgressBind(x)
|
|
x.Start()
|
|
|
|
return peerData, nil
|
|
}
|
|
|
|
func (self *tunneler) Inspect(key string, _ time.Duration) any {
|
|
if key == inspect.ErtTerminatorsKey {
|
|
return self.hostedServices.Inspect()
|
|
}
|
|
return nil
|
|
}
|