utp.go 842 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package utp
  2. import (
  3. "math/rand"
  4. "v2ray.com/core/common/alloc"
  5. "v2ray.com/core/transport/internet"
  6. )
  7. type UTP struct {
  8. header byte
  9. extension byte
  10. connectionId uint16
  11. }
  12. func (this *UTP) Overhead() int {
  13. return 4
  14. }
  15. func (this *UTP) Open(payload *alloc.Buffer) bool {
  16. payload.SliceFrom(this.Overhead())
  17. return true
  18. }
  19. func (this *UTP) Seal(payload *alloc.Buffer) {
  20. payload.PrependUint16(this.connectionId)
  21. payload.PrependBytes(this.header, this.extension)
  22. }
  23. type UTPFactory struct{}
  24. func (this UTPFactory) Create(rawSettings interface{}) internet.Authenticator {
  25. return &UTP{
  26. header: 1,
  27. extension: 0,
  28. connectionId: uint16(rand.Intn(65536)),
  29. }
  30. }
  31. func init() {
  32. internet.RegisterAuthenticator("utp", UTPFactory{})
  33. internet.RegisterAuthenticatorConfig("utp", func() interface{} { return &Config{} })
  34. }