utp.go 880 B

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