1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/transport/dialer/dialer.go
2015-12-17 01:19:04 +01:00

45 lines
778 B
Go

package dialer
import (
"errors"
"math/rand"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
ErrInvalidHost = errors.New("Invalid Host.")
)
func Dial(dest v2net.Destination) (net.Conn, error) {
var ip net.IP
if dest.Address().IsIPv4() || dest.Address().IsIPv6() {
ip = dest.Address().IP()
} else {
ips, err := net.LookupIP(dest.Address().Domain())
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, ErrInvalidHost
}
if len(ips) == 1 {
ip = ips[0]
} else {
ip = ips[rand.Intn(len(ips))]
}
}
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()),
})
}
}