1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 02:46:33 -05:00
v2fly/transport/internet/tcp/dialer.go

86 lines
2.3 KiB
Go
Raw Normal View History

2016-06-14 16:54:08 -04:00
package tcp
import (
2016-11-02 17:26:21 -04:00
"errors"
2016-06-14 16:54:08 -04:00
"net"
2016-09-30 10:53:40 -04:00
"crypto/tls"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
2016-10-02 17:43:58 -04:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-06-14 16:54:08 -04:00
)
var (
globalCache = NewConnectionCache()
)
2016-09-30 10:53:40 -04:00
func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
2016-06-14 16:54:08 -04:00
log.Info("Dailing TCP to ", dest)
if src == nil {
src = v2net.AnyIP
}
2016-10-02 17:43:58 -04:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
tcpSettings := networkSettings.(*Config)
2016-06-14 16:54:08 -04:00
id := src.String() + "-" + dest.NetAddr()
var conn net.Conn
2016-10-17 08:35:13 -04:00
if dest.Network == v2net.Network_TCP && tcpSettings.ConnectionReuse.IsEnabled() {
2016-06-14 16:54:08 -04:00
conn = globalCache.Get(id)
}
if conn == nil {
var err error
conn, err = internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
2016-10-31 19:41:46 -04:00
if options.Stream != nil && options.Stream.HasSecuritySettings() {
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
log.Error("TCP: Failed to get security settings: ", err)
return nil, err
}
tlsConfig, ok := securitySettings.(*v2tls.Config)
if ok {
config := tlsConfig.GetTLSConfig()
if dest.Address.Family().IsDomain() {
config.ServerName = dest.Address.Domain()
}
conn = tls.Client(conn, config)
2016-10-16 08:22:21 -04:00
}
2016-09-30 10:53:40 -04:00
}
2016-11-02 17:26:21 -04:00
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, errors.New("TCP: Failed to get header settings: " + err.Error())
}
auth, err := internet.CreateConnectionAuthenticator(tcpSettings.HeaderSettings.Type, headerConfig)
if err != nil {
return nil, errors.New("TCP: Failed to create header authenticator: " + err.Error())
}
conn = auth.Client(conn)
}
2016-09-30 10:53:40 -04:00
}
2016-10-02 17:43:58 -04:00
return NewConnection(id, conn, globalCache, tcpSettings), nil
2016-06-14 16:54:08 -04:00
}
2016-09-30 10:53:40 -04:00
func DialRaw(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
2016-06-14 16:54:08 -04:00
log.Info("Dailing Raw TCP to ", dest)
conn, err := internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
2016-09-30 10:53:40 -04:00
// TODO: handle dialer options
2016-06-14 16:54:08 -04:00
return &RawConnection{
TCPConn: *conn.(*net.TCPConn),
}, nil
}
func init() {
internet.TCPDialer = Dial
internet.RawTCPDialer = DialRaw
}