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