1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-29 23:37:54 -04:00
v2fly/transport/internet/kcp/dialer.go
2016-06-15 00:30:11 +02:00

53 lines
1006 B
Go

package kcp
import (
"errors"
"math/rand"
"net"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport/internet"
)
var (
ErrUnknownDestination = errors.New("Destination IP can't be resolved.")
)
func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
var ip net.IP
if dest.Address().IsDomain() {
ips, err := net.LookupIP(dest.Address().Domain())
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, ErrUnknownDestination
}
ip = ips[0]
} else {
ip = dest.Address().IP()
}
udpAddr := &net.UDPAddr{
IP: ip,
Port: int(dest.Port()),
}
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{})
if err != nil {
return nil, err
}
cpip, _ := NewNoneBlockCrypt(nil)
session := newUDPSession(rand.Uint32(), nil, udpConn, udpAddr, cpip)
kcvn := &KCPVconn{hc: session}
err = kcvn.ApplyConf()
if err != nil {
return nil, err
}
return kcvn, nil
}
func init() {
internet.KCPDialer = DialKCP
}