1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-18 23:36:23 -05:00
v2fly/transport/internet/tcp/dialer.go

58 lines
1.6 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package tcp
import (
"context"
2017-01-01 15:19:12 -05:00
2017-02-10 10:50:45 -05:00
"v2ray.com/core/app/log"
2017-01-03 09:16:48 -05:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/transport/internet"
2017-04-20 05:00:15 -04:00
"v2ray.com/core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
func getTCPSettingsFromContext(ctx context.Context) *Config {
rawTCPSettings := internet.TransportSettingsFromContext(ctx)
if rawTCPSettings == nil {
return nil
}
return rawTCPSettings.(*Config)
}
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
2017-04-09 07:30:46 -04:00
log.Trace(newError("dailing TCP to ", dest))
src := internet.DialerSourceFromContext(ctx)
conn, err := internet.DialSystem(ctx, src, dest)
if err != nil {
return nil, err
2016-06-14 16:54:08 -04:00
}
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
2017-04-20 05:00:15 -04:00
tlsConfig, ok := securitySettings.(*tls.Config)
if ok {
if dest.Address.Family().IsDomain() {
2017-10-26 05:43:02 -04:00
tlsConfig.OverrideServerNameIfEmpty(dest.Address.Domain())
2016-10-16 08:22:21 -04:00
}
2017-10-26 05:43:02 -04:00
config := tlsConfig.GetTLSConfig()
conn = tls.Client(conn, config)
2016-09-30 10:53:40 -04:00
}
}
tcpSettings := getTCPSettingsFromContext(ctx)
if tcpSettings != nil && tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
2017-04-09 07:30:46 -04:00
return nil, newError("failed to get header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
2017-04-09 07:30:46 -04:00
return nil, newError("failed to create header authenticator").Base(err).AtError()
2016-11-02 17:26:21 -04:00
}
conn = auth.Client(conn)
2016-09-30 10:53:40 -04:00
}
return internet.Connection(conn), nil
2016-06-14 16:54:08 -04:00
}
func init() {
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_TCP, Dial))
2016-06-14 16:54:08 -04:00
}