github.com/eagleql/xray-core@v1.4.4/transport/internet/tcp/dialer.go (about)

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