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