srtp.go 679 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package srtp
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/dice"
  7. )
  8. type SRTP struct {
  9. header uint16
  10. number uint16
  11. }
  12. func (*SRTP) Size() int32 {
  13. return 4
  14. }
  15. // Write implements io.Writer.
  16. func (s *SRTP) Write(b []byte) (int, error) {
  17. s.number++
  18. binary.BigEndian.PutUint16(b, s.number)
  19. binary.BigEndian.PutUint16(b[2:], s.number)
  20. return 4, nil
  21. }
  22. // New returns a new SRTP instance based on the given config.
  23. func New(ctx context.Context, config interface{}) (interface{}, error) {
  24. return &SRTP{
  25. header: 0xB5E8,
  26. number: dice.RollUint16(),
  27. }, nil
  28. }
  29. func init() {
  30. common.Must(common.RegisterConfig((*Config)(nil), New))
  31. }