2020-11-25 14:01:53 +03:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2021-12-15 03:28:47 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/xtls"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
2021-09-20 15:11:21 +03:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
2020-11-25 14:01:53 +03:00
|
|
|
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
|
|
tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
|
2023-01-21 07:36:08 +03:00
|
|
|
if fingerprint, ok := tls.GetFingerprint(ctx, config.Fingerprint); ok {
|
2021-04-01 12:15:18 +03:00
|
|
|
conn = tls.UClient(conn, tlsConfig, fingerprint)
|
|
|
|
if err := conn.(*tls.UConn).Handshake(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-29 13:08:29 +03:00
|
|
|
} else {
|
|
|
|
conn = tls.Client(conn, tlsConfig)
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
} else if config := xtls.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
|
|
xtlsConfig := config.GetXTLSConfig(xtls.WithDestination(dest))
|
|
|
|
conn = xtls.Client(conn, xtlsConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpSettings := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
if tcpSettings.HeaderSettings != nil {
|
|
|
|
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to get header settings").Base(err).AtError()
|
|
|
|
}
|
|
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create header authenticator").Base(err).AtError()
|
|
|
|
}
|
|
|
|
conn = auth.Client(conn)
|
|
|
|
}
|
2021-09-20 15:11:21 +03:00
|
|
|
return stat.Connection(conn), nil
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
|
|
}
|