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
683 B
Go
Raw Normal View History

2015-12-16 19:19:04 -05:00
package dialer
import (
"errors"
"net"
"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) {
if dest.Address().IsDomain() {
dialer := &net.Dialer{
Timeout: time.Second * 60,
DualStack: true,
2015-12-16 19:19:04 -05:00
}
network := "tcp"
if dest.IsUDP() {
network = "udp"
2015-12-16 19:19:04 -05:00
}
return dialer.Dial(network, dest.NetAddr())
2015-12-16 19:19:04 -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()),
})
}
}