1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/transport/dialer/dialer.go

41 lines
760 B
Go
Raw Normal View History

2015-12-16 19:19:04 -05:00
package dialer
import (
"errors"
"net"
2016-01-21 16:45:44 -05:00
"github.com/v2ray/v2ray-core/common/dice"
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) {
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 {
2016-01-30 06:32:38 -05:00
return nil, ErrorInvalidHost
2015-12-16 19:19:04 -05:00
}
2016-01-21 16:45:44 -05:00
ip = ips[dice.Roll(len(ips))]
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()),
})
}
}