2015-12-16 19:19:04 -05:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2016-02-18 06:28:45 -05:00
|
|
|
"time"
|
2015-12-16 19:19:04 -05:00
|
|
|
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-01-30 06:32:38 -05:00
|
|
|
ErrorInvalidHost = errors.New("Invalid Host.")
|
2015-12-16 19:19:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func Dial(dest v2net.Destination) (net.Conn, error) {
|
2016-02-18 06:28:45 -05:00
|
|
|
if dest.Address().IsDomain() {
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: time.Second * 60,
|
|
|
|
DualStack: true,
|
2015-12-16 19:19:04 -05:00
|
|
|
}
|
2016-02-18 06:28:45 -05:00
|
|
|
network := "tcp"
|
|
|
|
if dest.IsUDP() {
|
|
|
|
network = "udp"
|
2015-12-16 19:19:04 -05:00
|
|
|
}
|
2016-02-18 06:28:45 -05:00
|
|
|
return dialer.Dial(network, dest.NetAddr())
|
2015-12-16 19:19:04 -05:00
|
|
|
}
|
2016-02-18 06:28:45 -05:00
|
|
|
|
|
|
|
ip := dest.Address().IP()
|
2015-12-16 19:19:04 -05:00
|
|
|
if dest.IsTCP() {
|
|
|
|
return net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: ip,
|
|
|
|
Port: int(dest.Port()),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return net.DialUDP("udp", nil, &net.UDPAddr{
|
|
|
|
IP: ip,
|
|
|
|
Port: int(dest.Port()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|