mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-06-15 21:07:15 +03: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:
@ -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)
|
||||
|
Reference in New Issue
Block a user