dtls.go 951 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package tls
  2. import (
  3. "context"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/dice"
  6. )
  7. // DTLS writes header as DTLS. See https://tools.ietf.org/html/rfc6347
  8. type DTLS struct {
  9. epoch uint16
  10. sequence uint32
  11. }
  12. // Size implements PacketHeader.
  13. func (*DTLS) Size() int32 {
  14. return 1 + 2 + 2 + 3 + 2
  15. }
  16. // Write implements PacketHeader.
  17. func (d *DTLS) Write(b []byte) (int, error) {
  18. b[0] = 23 // application data
  19. b[1] = 254
  20. b[2] = 253
  21. b[3] = byte(d.epoch >> 8)
  22. b[4] = byte(d.epoch)
  23. b[5] = byte(d.sequence >> 16)
  24. b[6] = byte(d.sequence >> 8)
  25. b[7] = byte(d.sequence)
  26. d.sequence++
  27. l := dice.RollUint16()
  28. b[8] = byte(l >> 8)
  29. b[9] = byte(l)
  30. return 10, nil
  31. }
  32. // New creates a new UTP header for the given config.
  33. func New(ctx context.Context, config interface{}) (interface{}, error) {
  34. return &DTLS{
  35. epoch: dice.RollUint16(),
  36. sequence: 0,
  37. }, nil
  38. }
  39. func init() {
  40. common.Must(common.RegisterConfig((*PacketConfig)(nil), New))
  41. }