user.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package vmess
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "github.com/gofrs/uuid/v5"
  6. )
  7. // ID cmdKey length
  8. const (
  9. IDBytesLen = 16
  10. )
  11. // The ID of en entity, in the form of a UUID.
  12. type ID struct {
  13. UUID *uuid.UUID
  14. CmdKey []byte
  15. }
  16. // newID returns an ID with given UUID.
  17. func newID(uuid *uuid.UUID) *ID {
  18. id := &ID{UUID: uuid, CmdKey: make([]byte, IDBytesLen)}
  19. md5hash := md5.New()
  20. md5hash.Write(uuid.Bytes())
  21. md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
  22. md5hash.Sum(id.CmdKey[:0])
  23. return id
  24. }
  25. func nextID(u *uuid.UUID) *uuid.UUID {
  26. md5hash := md5.New()
  27. md5hash.Write(u.Bytes())
  28. md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
  29. var newid uuid.UUID
  30. for {
  31. md5hash.Sum(newid[:0])
  32. if !bytes.Equal(newid.Bytes(), u.Bytes()) {
  33. return &newid
  34. }
  35. md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
  36. }
  37. }
  38. func newAlterIDs(primary *ID, alterIDCount uint16) []*ID {
  39. alterIDs := make([]*ID, alterIDCount)
  40. prevID := primary.UUID
  41. for idx := range alterIDs {
  42. newid := nextID(prevID)
  43. alterIDs[idx] = &ID{UUID: newid, CmdKey: primary.CmdKey[:]}
  44. prevID = newid
  45. }
  46. alterIDs = append(alterIDs, primary)
  47. return alterIDs
  48. }