github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/proxy/vless/validator.go (about)

     1  // +build !confonly
     2  
     3  package vless
     4  
     5  import (
     6  	"strings"
     7  	"sync"
     8  
     9  	"v2ray.com/core/common/protocol"
    10  	"v2ray.com/core/common/uuid"
    11  )
    12  
    13  type Validator struct {
    14  	// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
    15  	email sync.Map
    16  	users sync.Map
    17  }
    18  
    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  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(u.(*protocol.MemoryUser).Account.(*MemoryAccount).ID.UUID())
    41  	return nil
    42  }
    43  
    44  func (v *Validator) Get(id uuid.UUID) *protocol.MemoryUser {
    45  	u, _ := v.users.Load(id)
    46  	if u != nil {
    47  		return u.(*protocol.MemoryUser)
    48  	}
    49  	return nil
    50  }