github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/vless/validator.go (about) 1 package vless 2 3 import ( 4 "strings" 5 "sync" 6 7 "github.com/v2fly/v2ray-core/v5/common/protocol" 8 "github.com/v2fly/v2ray-core/v5/common/uuid" 9 ) 10 11 // Validator stores valid VLESS users. 12 type Validator struct { 13 // Considering email's usage here, map + sync.Mutex/RWMutex may have better performance. 14 email sync.Map 15 users sync.Map 16 } 17 18 // Add a VLESS user, Email must be empty or unique. 19 func (v *Validator) Add(u *protocol.MemoryUser) error { 20 if u.Email != "" { 21 _, loaded := v.email.LoadOrStore(strings.ToLower(u.Email), u) 22 if loaded { 23 return newError("User ", u.Email, " already exists.") 24 } 25 } 26 v.users.Store(u.Account.(*MemoryAccount).ID.UUID(), u) 27 return nil 28 } 29 30 // Del a VLESS user with a non-empty Email. 31 func (v *Validator) Del(e string) error { 32 if e == "" { 33 return newError("Email must not be empty.") 34 } 35 le := strings.ToLower(e) 36 u, _ := v.email.Load(le) 37 if u == nil { 38 return newError("User ", e, " not found.") 39 } 40 v.email.Delete(le) 41 v.users.Delete(u.(*protocol.MemoryUser).Account.(*MemoryAccount).ID.UUID()) 42 return nil 43 } 44 45 // Get a VLESS user with UUID, nil if user doesn't exist. 46 func (v *Validator) Get(id uuid.UUID) *protocol.MemoryUser { 47 u, _ := v.users.Load(id) 48 if u != nil { 49 return u.(*protocol.MemoryUser) 50 } 51 return nil 52 }