mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-12-12 07:58:50 +00:00
079d0bd8a9
* Refactor log * Add new log methods * Fix logger test * Change all logging code * Clean up pathObj * Rebase to latest main * Remove invoking method name after the dot
57 lines
1.9 KiB
Go
57 lines
1.9 KiB
Go
package tcp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
"github.com/xtls/xray-core/transport/internet/reality"
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
)
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
|
errors.LogInfo(ctx, "dialing TCP to ", dest)
|
|
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))
|
|
if fingerprint := tls.GetFingerprint(config.Fingerprint); fingerprint != nil {
|
|
conn = tls.UClient(conn, tlsConfig, fingerprint)
|
|
if err := conn.(*tls.UConn).HandshakeContext(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
conn = tls.Client(conn, tlsConfig)
|
|
}
|
|
} else if config := reality.ConfigFromStreamSettings(streamSettings); config != nil {
|
|
if conn, err = reality.UClient(conn, config, ctx, dest); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
tcpSettings := streamSettings.ProtocolSettings.(*Config)
|
|
if tcpSettings.HeaderSettings != nil {
|
|
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
|
|
if err != nil {
|
|
return nil, errors.New("failed to get header settings").Base(err).AtError()
|
|
}
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
|
if err != nil {
|
|
return nil, errors.New("failed to create header authenticator").Base(err).AtError()
|
|
}
|
|
conn = auth.Client(conn)
|
|
}
|
|
return stat.Connection(conn), nil
|
|
}
|
|
|
|
func init() {
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
}
|