github.com/openziti/transport@v0.1.5/transwarptls/dialer.go (about)

     1  /*
     2  	Copyright NetFoundry, Inc.
     3  
     4  	Licensed under the Apache License, Version 2.0 (the "License");
     5  	you may not use this file except in compliance with the License.
     6  	You may obtain a copy of the License at
     7  
     8  	https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  	Unless required by applicable law or agreed to in writing, software
    11  	distributed under the License is distributed on an "AS IS" BASIS,
    12  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  	See the License for the specific language governing permissions and
    14  	limitations under the License.
    15  */
    16  
    17  package transwarptls
    18  
    19  import (
    20  	"github.com/openziti/dilithium/cf"
    21  	"github.com/openziti/dilithium/protocol/westlsworld3"
    22  	"github.com/openziti/dilithium/protocol/westworld3"
    23  	"github.com/openziti/foundation/identity/identity"
    24  	"github.com/openziti/transport"
    25  	"github.com/pkg/errors"
    26  	"github.com/sirupsen/logrus"
    27  	"net"
    28  )
    29  
    30  func Dial(endpoint *net.UDPAddr, name string, id *identity.TokenId, tcfg transport.Configuration) (transport.Connection, error) {
    31  	profileId := byte(0)
    32  	if tcfg != nil {
    33  		profile := westworld3.NewBaselineProfile()
    34  		if err := profile.Load(cf.MapIToMapS(tcfg)); err != nil {
    35  			return nil, errors.Wrap(err, "load profile")
    36  		}
    37  		newProfileId, err := westworld3.AddProfile(profile)
    38  		if err != nil {
    39  			return nil, errors.Wrap(err, "register profile")
    40  		}
    41  		profileId = newProfileId
    42  	}
    43  	logrus.Infof("westworld3 profile = [\n%s\n]", westworld3.GetProfile(profileId).Dump())
    44  
    45  	tlsConfig := id.ClientTLSConfig()
    46  	tlsConfig.ServerName = endpoint.IP.String()
    47  	socket, err := westlsworld3.Dial(endpoint, tlsConfig, profileId)
    48  	if err != nil {
    49  		return nil, errors.Wrap(err, "dial")
    50  	}
    51  
    52  	return &Connection{
    53  		detail: &transport.ConnectionDetail{
    54  			Address: "transwarptls:" + endpoint.String(),
    55  			InBound: false,
    56  			Name:    name,
    57  		},
    58  		socket: socket,
    59  	}, nil
    60  }
    61  
    62  func DialWithLocalBinding(endpoint *net.UDPAddr, name, localBinding string, id *identity.TokenId, tcfg transport.Configuration) (transport.Connection, error) {
    63  	logrus.Warn("Local interface binding is not yet supported with transwarptls. Dialing on the default interface")
    64  	return Dial(endpoint, name, id, tcfg)
    65  }