github.com/xraypb/xray-core@v1.6.6/transport/internet/domainsocket/dial.go (about)

     1  //go:build !windows && !wasm
     2  // +build !windows,!wasm
     3  
     4  package domainsocket
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/xraypb/xray-core/common"
    10  	"github.com/xraypb/xray-core/common/net"
    11  	"github.com/xraypb/xray-core/transport/internet"
    12  	"github.com/xraypb/xray-core/transport/internet/stat"
    13  	"github.com/xraypb/xray-core/transport/internet/tls"
    14  	"github.com/xraypb/xray-core/transport/internet/xtls"
    15  )
    16  
    17  func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
    18  	settings := streamSettings.ProtocolSettings.(*Config)
    19  	addr, err := settings.GetUnixAddr()
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	conn, err := net.DialUnix("unix", nil, addr)
    25  	if err != nil {
    26  		return nil, newError("failed to dial unix: ", settings.Path).Base(err).AtWarning()
    27  	}
    28  
    29  	if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
    30  		return tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest))), nil
    31  	} else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
    32  		return xtls.Client(conn, config.GetXTLSConfig(xtls.WithDestination(dest))), nil
    33  	}
    34  
    35  	return conn, nil
    36  }
    37  
    38  func init() {
    39  	common.Must(internet.RegisterTransportDialer(protocolName, Dial))
    40  }