github.com/v2fly/v2ray-core/v4@v4.45.2/proxy/vless/validator.go (about)

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