github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/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/xmplusdev/xmcore/common"
    10  	"github.com/xmplusdev/xmcore/common/net"
    11  	"github.com/xmplusdev/xmcore/transport/internet"
    12  	"github.com/xmplusdev/xmcore/transport/internet/reality"
    13  	"github.com/xmplusdev/xmcore/transport/internet/stat"
    14  	"github.com/xmplusdev/xmcore/transport/internet/tls"
    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 := reality.ConfigFromStreamSettings(streamSettings); config != nil {
    32  		return reality.UClient(conn, config, ctx, dest)
    33  	}
    34  
    35  	return conn, nil
    36  }
    37  
    38  func init() {
    39  	common.Must(internet.RegisterTransportDialer(protocolName, Dial))
    40  }