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