dialer.go 805 B

12345678910111213141516171819202122232425262728293031323334
  1. package kcp
  2. import (
  3. "errors"
  4. "math/rand"
  5. "net"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. "github.com/v2ray/v2ray-core/transport/internet"
  9. )
  10. var (
  11. ErrUnknownDestination = errors.New("Destination IP can't be resolved.")
  12. )
  13. func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
  14. log.Info("Dialling KCP to ", dest)
  15. udpDest := v2net.UDPDestination(dest.Address(), dest.Port())
  16. conn, err := internet.DialToDest(src, udpDest)
  17. if err != nil {
  18. return nil, err
  19. }
  20. cpip := NewSimpleAuthenticator()
  21. session := NewConnection(rand.Uint32(), conn, conn.LocalAddr().(*net.UDPAddr), conn.RemoteAddr().(*net.UDPAddr), cpip)
  22. session.FetchInputFrom(conn)
  23. return session, nil
  24. }
  25. func init() {
  26. internet.KCPDialer = DialKCP
  27. }