utp.go 810 B

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