v2ray-core/transport/hub/dialer.go

110 lines
2.4 KiB
Go
Raw Normal View History

2016-05-30 22:21:41 +00:00
package hub
import (
"errors"
"net"
"time"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-06-12 05:38:14 +00:00
"github.com/v2ray/v2ray-core/proxy"
2016-06-01 23:49:25 +00:00
"github.com/v2ray/v2ray-core/transport"
2016-05-30 22:21:41 +00:00
)
var (
ErrorInvalidHost = errors.New("Invalid Host.")
globalCache = NewConnectionCache()
)
2016-06-03 22:38:22 +00:00
func Dial(src v2net.Address, dest v2net.Destination) (*Connection, error) {
if src == nil {
src = v2net.AnyIP
}
id := src.String() + "-" + dest.NetAddr()
2016-06-01 23:49:25 +00:00
var conn net.Conn
2016-06-03 22:38:22 +00:00
if dest.IsTCP() && transport.IsConnectionReusable() {
conn = globalCache.Get(id)
2016-06-01 23:49:25 +00:00
}
2016-05-30 22:21:41 +00:00
if conn == nil {
var err error
2016-06-03 22:38:22 +00:00
conn, err = DialWithoutCache(src, dest)
2016-05-30 22:21:41 +00:00
if err != nil {
return nil, err
}
}
return &Connection{
2016-06-03 22:38:22 +00:00
dest: id,
2016-05-30 22:21:41 +00:00
conn: conn,
listener: globalCache,
}, nil
}
2016-06-03 22:38:22 +00:00
func DialWithoutCache(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: time.Second * 60,
DualStack: true,
2016-05-30 22:21:41 +00:00
}
2016-06-03 22:38:22 +00:00
if src != nil && src != v2net.AnyIP {
var addr net.Addr
if dest.IsTCP() {
addr = &net.TCPAddr{
IP: src.IP(),
Port: 0,
}
} else {
addr = &net.UDPAddr{
IP: src.IP(),
Port: 0,
}
}
dialer.LocalAddr = addr
2016-05-30 22:21:41 +00:00
}
2016-06-03 22:38:22 +00:00
return dialer.Dial(dest.Network().String(), dest.NetAddr())
2016-05-30 22:21:41 +00:00
}
2016-06-12 05:38:14 +00:00
2016-06-12 05:45:21 +00:00
func Dial3(src v2net.Address, dest v2net.Destination, proxyMeta *proxy.OutboundHandlerMeta) (*Connection, error) {
2016-06-12 05:38:14 +00:00
if proxyMeta.KcpSupported && transport.IsKcpEnabled() {
DialKCP3(src, dest, proxyMeta)
}
return Dial(src, dest)
}
2016-06-12 05:45:21 +00:00
func DialWithoutCache3(src v2net.Address, dest v2net.Destination, proxyMeta *proxy.OutboundHandlerMeta) (net.Conn, error) {
2016-06-12 05:38:14 +00:00
if proxyMeta.KcpSupported && transport.IsKcpEnabled() {
2016-06-12 07:15:47 +00:00
return DialKCPWithoutCache(src, dest)
2016-06-12 05:38:14 +00:00
}
return DialWithoutCache(src, dest)
}
2016-06-12 05:45:21 +00:00
func DialKCP3(src v2net.Address, dest v2net.Destination, proxyMeta *proxy.OutboundHandlerMeta) (*Connection, error) {
2016-06-12 05:38:14 +00:00
if src == nil {
src = v2net.AnyIP
}
id := src.String() + "-" + dest.NetAddr()
var conn net.Conn
if dest.IsTCP() && transport.IsConnectionReusable() {
conn = globalCache.Get(id)
}
if conn == nil {
var err error
conn, err = DialWithoutCache3(src, dest, proxyMeta)
if err != nil {
return nil, err
}
}
return &Connection{
dest: id,
conn: conn,
listener: globalCache,
}, nil
}
/*DialKCPWithoutCache Dial KCP connection
This Dialer will ignore src this is a restriction
due to github.com/xtaci/kcp-go.DialWithOptions
*/
func DialKCPWithoutCache(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
return DialKCP(dest)
}