1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-20 20:24:15 -04:00
v2fly/transport/internet/tcp/dialer.go
2016-06-14 22:54:08 +02:00

50 lines
1.0 KiB
Go

package tcp
import (
"net"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/internet"
)
var (
globalCache = NewConnectionCache()
)
func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
log.Info("Dailing TCP to ", dest)
if src == nil {
src = v2net.AnyIP
}
id := src.String() + "-" + dest.NetAddr()
var conn net.Conn
if dest.IsTCP() && effectiveConfig.ConnectionReuse {
conn = globalCache.Get(id)
}
if conn == nil {
var err error
conn, err = internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
}
return NewConnection(id, conn, globalCache), nil
}
func DialRaw(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
log.Info("Dailing Raw TCP to ", dest)
conn, err := internet.DialToDest(src, dest)
if err != nil {
return nil, err
}
return &RawConnection{
TCPConn: *conn.(*net.TCPConn),
}, nil
}
func init() {
internet.TCPDialer = Dial
internet.RawTCPDialer = DialRaw
}