2016-06-14 16:54:08 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2016-06-14 18:30:11 -04:00
|
|
|
"net"
|
2016-07-12 12:54:16 -04:00
|
|
|
"sync/atomic"
|
2016-06-14 18:30:11 -04:00
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/dice"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
2016-07-12 12:54:16 -04:00
|
|
|
var (
|
|
|
|
globalConv = uint32(dice.Roll(65536))
|
|
|
|
)
|
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
|
2016-06-17 10:51:41 -04:00
|
|
|
udpDest := v2net.UDPDestination(dest.Address(), dest.Port())
|
2016-08-06 16:52:02 -04:00
|
|
|
log.Info("KCP|Dialer: Dialing KCP to ", udpDest)
|
2016-06-17 10:51:41 -04:00
|
|
|
conn, err := internet.DialToDest(src, udpDest)
|
2016-06-14 16:54:08 -04:00
|
|
|
if err != nil {
|
2016-08-06 16:52:02 -04:00
|
|
|
log.Error("KCP|Dialer: Failed to dial to dest: ", err)
|
2016-06-14 16:54:08 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-14 18:30:11 -04:00
|
|
|
|
2016-08-06 15:59:22 -04:00
|
|
|
cpip, err := effectiveConfig.GetAuthenticator()
|
|
|
|
if err != nil {
|
2016-08-06 16:52:02 -04:00
|
|
|
log.Error("KCP|Dialer: Failed to create authenticator: ", err)
|
2016-08-06 15:59:22 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-12 12:54:16 -04:00
|
|
|
conv := uint16(atomic.AddUint32(&globalConv, 1))
|
|
|
|
session := NewConnection(conv, conn, conn.LocalAddr().(*net.UDPAddr), conn.RemoteAddr().(*net.UDPAddr), cpip)
|
2016-06-17 10:51:41 -04:00
|
|
|
session.FetchInputFrom(conn)
|
|
|
|
|
|
|
|
return session, nil
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
internet.KCPDialer = DialKCP
|
|
|
|
}
|