@@ -3,25 +3,32 @@ package account
33import (
44 "sync"
55
6- "github.com/xtls/xray-core/common/errors "
6+ "github.com/xtls/xray-core/common/net "
77 "github.com/xtls/xray-core/common/protocol"
8+ "github.com/xtls/xray-core/common/uuid"
89
910 "google.golang.org/protobuf/proto"
1011)
1112
1213func (a * Account ) AsAccount () (protocol.Account , error ) {
14+ var VR net.Port
15+ if id , err := uuid .ParseString (a .Auth ); err == nil {
16+ VR = net .PortFromBytes (id [6 :8 ])
17+ }
1318 return & MemoryAccount {
1419 Auth : a .Auth ,
20+ VR : VR ,
1521 }, nil
1622}
1723
1824type MemoryAccount struct {
1925 Auth string
26+ VR net.Port
2027}
2128
22- func (a * MemoryAccount ) Equals (another protocol.Account ) bool {
23- if account , ok := another .(* MemoryAccount ); ok {
24- return a .Auth == account .Auth
29+ func (a * MemoryAccount ) Equals (other protocol.Account ) bool {
30+ if b , ok := other .(* MemoryAccount ); ok {
31+ return a .Auth == b .Auth
2532 }
2633 return false
2734}
@@ -33,97 +40,63 @@ func (a *MemoryAccount) ToProto() proto.Message {
3340}
3441
3542type Validator struct {
36- emails map [string ]struct {}
37- users map [string ]* protocol.MemoryUser
38-
39- mutex sync.Mutex
43+ users sync.Map
4044}
4145
4246func NewValidator () * Validator {
43- return & Validator {
44- emails : make (map [string ]struct {}),
45- users : make (map [string ]* protocol.MemoryUser ),
46- }
47+ return & Validator {}
4748}
4849
49- func (v * Validator ) Add (u * protocol.MemoryUser ) error {
50- v .mutex .Lock ()
51- defer v .mutex .Unlock ()
52-
53- if u .Email != "" {
54- if _ , ok := v .emails [u .Email ]; ok {
55- return errors .New ("User " , u .Email , " already exists." )
56- }
57- v .emails [u .Email ] = struct {}{}
58- }
59- v .users [u .Account .(* MemoryAccount ).Auth ] = u
60-
50+ func (v * Validator ) Add (user * protocol.MemoryUser ) error {
51+ v .users .Store (user .Account .(* MemoryAccount ).Auth , user )
6152 return nil
6253}
6354
64- func (v * Validator ) Del (email string ) error {
65- if email == "" {
66- return errors . New ( "Email must not be empty." )
55+ func (v * Validator ) DelByEmail (email string ) error {
56+ if user := v . GetByEmail ( email ); user != nil {
57+ v . users . Delete ( user . Account .( * MemoryAccount ). Auth )
6758 }
68-
69- v .mutex .Lock ()
70- defer v .mutex .Unlock ()
71-
72- if _ , ok := v .emails [email ]; ! ok {
73- return errors .New ("User " , email , " not found." )
74- }
75- delete (v .emails , email )
76- for key , user := range v .users {
77- if user .Email == email {
78- delete (v .users , key )
79- break
80- }
81- }
82-
8359 return nil
8460}
8561
8662func (v * Validator ) Get (auth string ) * protocol.MemoryUser {
87- v .mutex .Lock ()
88- defer v .mutex .Unlock ()
89-
90- return v .users [auth ]
91- }
92-
93- func (v * Validator ) GetByEmail (email string ) * protocol.MemoryUser {
94- if email == "" {
95- return nil
96- }
97-
98- v .mutex .Lock ()
99- defer v .mutex .Unlock ()
100-
101- if _ , ok := v .emails [email ]; ok {
102- for _ , user := range v .users {
103- if user .Email == email {
104- return user
105- }
106- }
63+ if value , ok := v .users .Load (auth ); ok {
64+ return value .(* protocol.MemoryUser )
10765 }
108-
10966 return nil
11067}
11168
112- func (v * Validator ) GetAll () []* protocol.MemoryUser {
113- v .mutex .Lock ()
114- defer v .mutex .Unlock ()
115-
116- users := make ([]* protocol.MemoryUser , 0 , len (v .users ))
117- for _ , user := range v .users {
118- users = append (users , user )
119- }
69+ func (v * Validator ) GetByEmail (email string ) (user * protocol.MemoryUser ) {
70+ v .users .Range (func (key , value any ) bool {
71+ if value .(* protocol.MemoryUser ).Email == email {
72+ user = value .(* protocol.MemoryUser )
73+ return false
74+ }
75+ return true
76+ })
77+ return
78+ }
12079
121- return users
80+ func (v * Validator ) GetAll () (users []* protocol.MemoryUser ) {
81+ v .users .Range (func (key , value any ) bool {
82+ users = append (users , value .(* protocol.MemoryUser ))
83+ return true
84+ })
85+ return
12286}
12387
124- func (v * Validator ) GetCount () int64 {
125- v .mutex .Lock ()
126- defer v .mutex .Unlock ()
88+ func (v * Validator ) GetCount () (count int64 ) {
89+ v .users .Range (func (key , value any ) bool {
90+ count ++
91+ return true
92+ })
93+ return
94+ }
12795
128- return int64 (len (v .users ))
96+ func (v * Validator ) NotEmpty () (not_empty bool ) {
97+ v .users .Range (func (key , value any ) bool {
98+ not_empty = true
99+ return false
100+ })
101+ return
129102}
0 commit comments