srtp.go 810 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package srtp
  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 SRTP struct {
  10. header uint16
  11. number uint16
  12. }
  13. func (v *SRTP) Overhead() int {
  14. return 4
  15. }
  16. func (v *SRTP) Open(payload *alloc.Buffer) bool {
  17. payload.SliceFrom(v.Overhead())
  18. return true
  19. }
  20. func (v *SRTP) Seal(payload *alloc.Buffer) {
  21. v.number++
  22. payload.PrependFunc(2, serial.WriteUint16(v.number))
  23. payload.PrependFunc(2, serial.WriteUint16(v.header))
  24. }
  25. type SRTPFactory struct {
  26. }
  27. func (v SRTPFactory) Create(rawSettings interface{}) internet.Authenticator {
  28. return &SRTP{
  29. header: 0xB5E8,
  30. number: uint16(rand.Intn(65536)),
  31. }
  32. }
  33. func init() {
  34. internet.RegisterAuthenticator(loader.GetType(new(Config)), SRTPFactory{})
  35. }