github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/domainsocket/dial.go (about)

     1  // +build !windows
     2  // +build !wasm
     3  // +build !confonly
     4  
     5  package domainsocket
     6  
     7  import (
     8  	"context"
     9  
    10  	"v2ray.com/core/common"
    11  	"v2ray.com/core/common/net"
    12  	"v2ray.com/core/transport/internet"
    13  	"v2ray.com/core/transport/internet/tls"
    14  	"v2ray.com/core/transport/internet/xtls"
    15  )
    16  
    17  func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.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  }