You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/dialer/dialer.go

41 lines
760 B

9 years ago
package dialer
import (
"errors"
"net"
9 years ago
"github.com/v2ray/v2ray-core/common/dice"
9 years ago
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
9 years ago
ErrorInvalidHost = errors.New("Invalid Host.")
9 years ago
)
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 {
9 years ago
return nil, ErrorInvalidHost
9 years ago
}
9 years ago
ip = ips[dice.Roll(len(ips))]
9 years ago
}
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()),
})
}
}