2016-06-14 20:54:08 +00:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
2017-01-26 19:46:44 +00:00
|
|
|
"context"
|
2016-09-30 14:53:40 +00:00
|
|
|
"crypto/tls"
|
2016-12-04 08:10:47 +00:00
|
|
|
"net"
|
2017-01-01 20:19:12 +00:00
|
|
|
|
2017-01-03 14:16:48 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-04 08:10:47 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2017-02-01 20:35:40 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2016-08-20 18:55:45 +00:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2016-11-24 22:16:05 +00:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2016-10-02 21:43:58 +00:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-24 22:16:05 +00:00
|
|
|
globalCache = internal.NewConnectionPool()
|
2016-06-14 20:54:08 +00:00
|
|
|
)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
|
2016-11-22 07:23:03 +00:00
|
|
|
log.Info("Internet|TCP: Dailing TCP to ", dest)
|
2017-01-26 19:46:44 +00:00
|
|
|
src := internet.DialerSourceFromContext(ctx)
|
|
|
|
|
|
|
|
tcpSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
2016-10-02 21:43:58 +00:00
|
|
|
|
2017-01-01 21:12:44 +00:00
|
|
|
id := internal.NewConnectionID(src, dest)
|
2016-06-14 20:54:08 +00:00
|
|
|
var conn net.Conn
|
2017-01-01 20:19:12 +00:00
|
|
|
if dest.Network == v2net.Network_TCP && tcpSettings.IsConnectionReuse() {
|
2016-06-14 20:54:08 +00:00
|
|
|
conn = globalCache.Get(id)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
var err error
|
2017-01-03 14:16:48 +00:00
|
|
|
conn, err = internet.DialSystem(src, dest)
|
2016-06-14 20:54:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
|
2016-10-31 23:41:46 +00:00
|
|
|
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 12:22:21 +00:00
|
|
|
}
|
2016-09-30 14:53:40 +00:00
|
|
|
}
|
2016-11-02 21:26:21 +00:00
|
|
|
if tcpSettings.HeaderSettings != nil {
|
|
|
|
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
|
|
|
|
if err != nil {
|
2016-12-04 08:43:33 +00:00
|
|
|
return nil, errors.Base(err).Message("Interent|TCP: Failed to get header settings.")
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
2017-01-12 21:47:10 +00:00
|
|
|
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
|
2016-11-02 21:26:21 +00:00
|
|
|
if err != nil {
|
2016-12-04 08:43:33 +00:00
|
|
|
return nil, errors.Base(err).Message("Internet|TCP: Failed to create header authenticator.")
|
2016-11-02 21:26:21 +00:00
|
|
|
}
|
|
|
|
conn = auth.Client(conn)
|
|
|
|
}
|
2016-09-30 14:53:40 +00:00
|
|
|
}
|
2017-01-04 12:29:41 +00:00
|
|
|
return internal.NewConnection(id, conn, globalCache, internal.ReuseConnection(tcpSettings.IsConnectionReuse())), 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
|
|
|
}
|