v2fly/transport/internet/tcp/dialer.go

52 lines
1.6 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
package tcp
import (
"context"
2021-06-19 13:36:54 +00:00
"github.com/v2fly/v2ray-core/v4/common/serial"
2017-01-01 20:19:12 +00:00
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/transport/internet"
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
2016-06-14 20:54:08 +00:00
)
2018-01-02 17:57:22 +00:00
// Dial dials a new TCP connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
return nil, err
2016-06-14 20:54:08 +00:00
}
2017-12-16 23:53:17 +00:00
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
2020-07-20 05:59:46 +00:00
/*
if config.IsExperiment8357() {
conn = tls.UClient(conn, tlsConfig)
} else {
conn = tls.Client(conn, tlsConfig)
}
*/
conn = tls.Client(conn, tlsConfig)
}
tcpSettings := streamSettings.ProtocolSettings.(*Config)
if tcpSettings.HeaderSettings != nil {
2021-06-19 13:36:54 +00:00
headerConfig, err := serial.GetInstanceOf(tcpSettings.HeaderSettings)
if err != nil {
2017-04-09 11:30:46 +00:00
return nil, newError("failed to get header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
2017-04-09 11:30:46 +00:00
return nil, newError("failed to create header authenticator").Base(err).AtError()
2016-11-02 21:26:21 +00:00
}
conn = auth.Client(conn)
2016-09-30 14:53:40 +00:00
}
return internet.Connection(conn), nil
2016-06-14 20:54:08 +00:00
}
func init() {
2018-08-06 11:48:35 +00:00
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
2016-06-14 20:54:08 +00:00
}