mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +00:00
Allow to inject custom validator in VLESS controller (#3453)
* Make Validator an interface * Move validator creation away from VLESS inbound controller
This commit is contained in:
parent
3a8c5f38e8
commit
c259e4e4a6
4 changed files with 33 additions and 23 deletions
|
@ -9,15 +9,21 @@ import (
|
|||
"github.com/xtls/xray-core/common/uuid"
|
||||
)
|
||||
|
||||
// Validator stores valid VLESS users.
|
||||
type Validator struct {
|
||||
type Validator interface {
|
||||
Get(id uuid.UUID) *protocol.MemoryUser
|
||||
Add(u *protocol.MemoryUser) error
|
||||
Del(email string) error
|
||||
}
|
||||
|
||||
// MemoryValidator stores valid VLESS users.
|
||||
type MemoryValidator struct {
|
||||
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
||||
email sync.Map
|
||||
users sync.Map
|
||||
}
|
||||
|
||||
// Add a VLESS user, Email must be empty or unique.
|
||||
func (v *Validator) Add(u *protocol.MemoryUser) error {
|
||||
func (v *MemoryValidator) Add(u *protocol.MemoryUser) error {
|
||||
if u.Email != "" {
|
||||
_, loaded := v.email.LoadOrStore(strings.ToLower(u.Email), u)
|
||||
if loaded {
|
||||
|
@ -29,7 +35,7 @@ func (v *Validator) Add(u *protocol.MemoryUser) error {
|
|||
}
|
||||
|
||||
// Del a VLESS user with a non-empty Email.
|
||||
func (v *Validator) Del(e string) error {
|
||||
func (v *MemoryValidator) Del(e string) error {
|
||||
if e == "" {
|
||||
return errors.New("Email must not be empty.")
|
||||
}
|
||||
|
@ -44,7 +50,7 @@ func (v *Validator) Del(e string) error {
|
|||
}
|
||||
|
||||
// Get a VLESS user with UUID, nil if user doesn't exist.
|
||||
func (v *Validator) Get(id uuid.UUID) *protocol.MemoryUser {
|
||||
func (v *MemoryValidator) Get(id uuid.UUID) *protocol.MemoryUser {
|
||||
u, _ := v.users.Load(id)
|
||||
if u != nil {
|
||||
return u.(*protocol.MemoryUser)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue