id.go 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package protocol
  2. import (
  3. "crypto/md5"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/uuid"
  6. )
  7. const (
  8. IDBytesLen = 16
  9. )
  10. // The ID of en entity, in the form of a UUID.
  11. type ID struct {
  12. uuid uuid.UUID
  13. cmdKey [IDBytesLen]byte
  14. }
  15. // Equals returns true if this ID equals to the other one.
  16. func (id *ID) Equals(another *ID) bool {
  17. return id.uuid.Equals(&(another.uuid))
  18. }
  19. func (id *ID) Bytes() []byte {
  20. return id.uuid.Bytes()
  21. }
  22. func (id *ID) String() string {
  23. return id.uuid.String()
  24. }
  25. func (id *ID) UUID() uuid.UUID {
  26. return id.uuid
  27. }
  28. func (id ID) CmdKey() []byte {
  29. return id.cmdKey[:]
  30. }
  31. // NewID returns an ID with given UUID.
  32. func NewID(uuid uuid.UUID) *ID {
  33. id := &ID{uuid: uuid}
  34. md5hash := md5.New()
  35. common.Must2(md5hash.Write(uuid.Bytes()))
  36. common.Must2(md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")))
  37. md5hash.Sum(id.cmdKey[:0])
  38. return id
  39. }