srtp.go 760 B

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