github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/tcp/dialer.go (about)

     1  //go:build !confonly
     2  // +build !confonly
     3  
     4  package tcp
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/v2fly/v2ray-core/v4/common"
    10  	"github.com/v2fly/v2ray-core/v4/common/net"
    11  	"github.com/v2fly/v2ray-core/v4/common/session"
    12  	"github.com/v2fly/v2ray-core/v4/transport/internet"
    13  	"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
    14  )
    15  
    16  // Dial dials a new TCP connection to the given destination.
    17  func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
    18  	newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
    19  	conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
    25  		tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
    26  		/*
    27  			if config.IsExperiment8357() {
    28  				conn = tls.UClient(conn, tlsConfig)
    29  			} else {
    30  				conn = tls.Client(conn, tlsConfig)
    31  			}
    32  		*/
    33  		conn = tls.Client(conn, tlsConfig)
    34  	}
    35  
    36  	tcpSettings := streamSettings.ProtocolSettings.(*Config)
    37  	if tcpSettings.HeaderSettings != nil {
    38  		headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
    39  		if err != nil {
    40  			return nil, newError("failed to get header settings").Base(err).AtError()
    41  		}
    42  		auth, err := internet.CreateConnectionAuthenticator(headerConfig)
    43  		if err != nil {
    44  			return nil, newError("failed to create header authenticator").Base(err).AtError()
    45  		}
    46  		conn = auth.Client(conn)
    47  	}
    48  	return internet.Connection(conn), nil
    49  }
    50  
    51  func init() {
    52  	common.Must(internet.RegisterTransportDialer(protocolName, Dial))
    53  }