utp.go 839 B

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