dialer.go 850 B

1234567891011121314151617181920212223242526272829303132333435
  1. package kcp
  2. import (
  3. "net"
  4. "sync/atomic"
  5. "github.com/v2ray/v2ray-core/common/dice"
  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. globalConv = uint32(dice.Roll(65536))
  12. )
  13. func DialKCP(src v2net.Address, dest v2net.Destination) (internet.Connection, error) {
  14. udpDest := v2net.UDPDestination(dest.Address(), dest.Port())
  15. log.Info("Dialling KCP to ", udpDest)
  16. conn, err := internet.DialToDest(src, udpDest)
  17. if err != nil {
  18. return nil, err
  19. }
  20. cpip := NewSimpleAuthenticator()
  21. conv := uint16(atomic.AddUint32(&globalConv, 1))
  22. session := NewConnection(conv, conn, conn.LocalAddr().(*net.UDPAddr), conn.RemoteAddr().(*net.UDPAddr), cpip)
  23. session.FetchInputFrom(conn)
  24. return session, nil
  25. }
  26. func init() {
  27. internet.KCPDialer = DialKCP
  28. }