utp.go 641 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package utp
  2. import (
  3. "context"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/dice"
  6. "v2ray.com/core/common/serial"
  7. )
  8. type UTP struct {
  9. header byte
  10. extension byte
  11. connectionId uint16
  12. }
  13. func (v *UTP) Size() int {
  14. return 4
  15. }
  16. func (v *UTP) Write(b []byte) (int, error) {
  17. serial.Uint16ToBytes(v.connectionId, b[:0])
  18. b[2] = v.header
  19. b[3] = v.extension
  20. return 4, nil
  21. }
  22. func NewUTP(ctx context.Context, config interface{}) (interface{}, error) {
  23. return &UTP{
  24. header: 1,
  25. extension: 0,
  26. connectionId: dice.RandomUint16(),
  27. }, nil
  28. }
  29. func init() {
  30. common.Must(common.RegisterConfig((*Config)(nil), NewUTP))
  31. }