github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/transport/internet/transportcommon/dialer.go (about)

     1  package transportcommon
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/v2fly/v2ray-core/v5/transport/internet/security"
     7  
     8  	"github.com/v2fly/v2ray-core/v5/common/environment"
     9  	"github.com/v2fly/v2ray-core/v5/common/environment/envctx"
    10  
    11  	"github.com/v2fly/v2ray-core/v5/common/net"
    12  	"github.com/v2fly/v2ray-core/v5/transport/internet"
    13  )
    14  
    15  func DialWithSecuritySettings(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
    16  	transportEnvironment := envctx.EnvironmentFromContext(ctx).(environment.TransportEnvironment)
    17  	dialer := transportEnvironment.Dialer()
    18  	conn, err := dialer.Dial(ctx, nil, dest, streamSettings.SocketSettings)
    19  	if err != nil {
    20  		return nil, newError("failed to dial to ", dest).Base(err)
    21  	}
    22  	securityEngine, err := security.CreateSecurityEngineFromSettings(ctx, streamSettings)
    23  	if err != nil {
    24  		return nil, newError("unable to create security engine").Base(err)
    25  	}
    26  
    27  	if securityEngine != nil {
    28  		conn, err = securityEngine.Client(conn, security.OptionWithDestination{Dest: dest})
    29  		if err != nil {
    30  			return nil, newError("unable to create security protocol client from security engine").Base(err)
    31  		}
    32  	}
    33  	return internet.Connection(conn), nil
    34  }