validator.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build !confonly
  2. package vless
  3. import (
  4. "strings"
  5. "sync"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/uuid"
  8. )
  9. type Validator struct {
  10. // Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
  11. email sync.Map
  12. users sync.Map
  13. }
  14. func (v *Validator) Add(u *protocol.MemoryUser) error {
  15. if u.Email != "" {
  16. _, loaded := v.email.LoadOrStore(strings.ToLower(u.Email), u)
  17. if loaded {
  18. return newError("User ", u.Email, " already exists.")
  19. }
  20. }
  21. v.users.Store(u.Account.(*MemoryAccount).ID.UUID(), u)
  22. return nil
  23. }
  24. func (v *Validator) Del(e string) error {
  25. if e == "" {
  26. return newError("Email must not be empty.")
  27. }
  28. le := strings.ToLower(e)
  29. u, _ := v.email.Load(le)
  30. if u == nil {
  31. return newError("User ", e, " not found.")
  32. }
  33. v.email.Delete(le)
  34. v.users.Delete(u.(*protocol.MemoryUser).Account.(*MemoryAccount).ID.UUID())
  35. return nil
  36. }
  37. func (v *Validator) Get(id uuid.UUID) *protocol.MemoryUser {
  38. u, _ := v.users.Load(id)
  39. if u != nil {
  40. return u.(*protocol.MemoryUser)
  41. }
  42. return nil
  43. }