1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00

86 lines
2.4 KiB
Go
Raw Normal View History

2016-06-14 22:54:08 +02:00
package tcp
import (
2016-09-30 16:53:40 +02:00
"crypto/tls"
2016-12-04 09:10:47 +01:00
"net"
"v2ray.com/core/common/errors"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
2016-11-24 23:16:05 +01:00
"v2ray.com/core/transport/internet/internal"
2016-10-02 23:43:58 +02:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-06-14 22:54:08 +02:00
)
var (
2016-11-24 23:16:05 +01:00
globalCache = internal.NewConnectionPool()
2016-06-14 22:54:08 +02:00
)
2016-09-30 16:53:40 +02:00
func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
2016-11-22 08:23:03 +01:00
log.Info("Internet|TCP: Dailing TCP to ", dest)
2016-06-14 22:54:08 +02:00
if src == nil {
src = v2net.AnyIP
}
2016-10-02 23:43:58 +02:00
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
tcpSettings := networkSettings.(*Config)
2016-11-24 23:16:05 +01:00
id := internal.NewConnectionId(src, dest)
2016-06-14 22:54:08 +02:00
var conn net.Conn
2016-10-17 14:35:13 +02:00
if dest.Network == v2net.Network_TCP && tcpSettings.ConnectionReuse.IsEnabled() {
2016-06-14 22:54:08 +02:00
conn = globalCache.Get(id)
}
if conn == nil {
var err error
conn, err = internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
2016-11-01 00:41:46 +01: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 14:22:21 +02:00
}
2016-09-30 16:53:40 +02:00
}
2016-11-02 22:26:21 +01:00
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
2016-12-04 09:43:33 +01:00
return nil, errors.Base(err).Message("Interent|TCP: Failed to get header settings.")
2016-11-02 22:26:21 +01:00
}
auth, err := internet.CreateConnectionAuthenticator(tcpSettings.HeaderSettings.Type, headerConfig)
if err != nil {
2016-12-04 09:43:33 +01:00
return nil, errors.Base(err).Message("Internet|TCP: Failed to create header authenticator.")
2016-11-02 22:26:21 +01:00
}
conn = auth.Client(conn)
}
2016-09-30 16:53:40 +02:00
}
2016-10-02 23:43:58 +02:00
return NewConnection(id, conn, globalCache, tcpSettings), nil
2016-06-14 22:54:08 +02:00
}
2016-09-30 16:53:40 +02:00
func DialRaw(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
2016-11-22 08:23:03 +01:00
log.Info("Internet|TCP: Dailing Raw TCP to ", dest)
2016-06-14 22:54:08 +02:00
conn, err := internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
2016-09-30 16:53:40 +02:00
// TODO: handle dialer options
2016-06-14 22:54:08 +02:00
return &RawConnection{
TCPConn: *conn.(*net.TCPConn),
}, nil
}
func init() {
internet.TCPDialer = Dial
internet.RawTCPDialer = DialRaw
}