API: Add new Get Inbound User (#3644)

* Add GetInboundUser in proto

* Add get user logic for all existing inbounds

* Add inbounduser command

* Add option to get all users

* Fix shadowsocks2022 config

* Fix init users in shadowsocks2022

* Fix copy

* Add inbound user count command

This api costs much less than get inbound user, could be useful in some case

* Update from latest main
This commit is contained in:
yuhan6665 2024-11-03 00:25:23 -04:00 committed by GitHub
parent b7aacd3245
commit 85a1c33709
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 982 additions and 293 deletions

View file

@ -1,6 +1,7 @@
package vmess
import (
"google.golang.org/protobuf/proto"
"strings"
"github.com/xtls/xray-core/common/errors"
@ -28,6 +29,21 @@ func (a *MemoryAccount) Equals(account protocol.Account) bool {
return a.ID.Equals(vmessAccount.ID)
}
func (a *MemoryAccount) ToProto() proto.Message {
var test = ""
if a.AuthenticatedLengthExperiment {
test = "AuthenticatedLength|"
}
if a.NoTerminationSignal {
test = test + "NoTerminationSignal"
}
return &Account{
Id: a.ID.String(),
TestsEnabled: test,
SecuritySettings: &protocol.SecurityConfig{Type: a.Security},
}
}
// AsAccount implements protocol.Account.
func (a *Account) AsAccount() (protocol.Account, error) {
id, err := uuid.ParseString(a.Id)

View file

@ -56,7 +56,7 @@ func (v *userByEmail) Add(u *protocol.MemoryUser) bool {
return v.addNoLock(u)
}
func (v *userByEmail) Get(email string) (*protocol.MemoryUser, bool) {
func (v *userByEmail) GetOrGenerate(email string) (*protocol.MemoryUser, bool) {
email = strings.ToLower(email)
v.Lock()
@ -80,6 +80,13 @@ func (v *userByEmail) Get(email string) (*protocol.MemoryUser, bool) {
return user, found
}
func (v *userByEmail) Get(email string) *protocol.MemoryUser {
email = strings.ToLower(email)
v.Lock()
defer v.Unlock()
return v.cache[email]
}
func (v *userByEmail) Remove(email string) bool {
email = strings.ToLower(email)
@ -141,14 +148,26 @@ func (*Handler) Network() []net.Network {
return []net.Network{net.Network_TCP, net.Network_UNIX}
}
func (h *Handler) GetUser(email string) *protocol.MemoryUser {
user, existing := h.usersByEmail.Get(email)
func (h *Handler) GetOrGenerateUser(email string) *protocol.MemoryUser {
user, existing := h.usersByEmail.GetOrGenerate(email)
if !existing {
h.clients.Add(user)
}
return user
}
func (h *Handler) GetUser(ctx context.Context, email string) *protocol.MemoryUser {
return h.usersByEmail.Get(email)
}
func (h *Handler) GetUsers(ctx context.Context) []*protocol.MemoryUser {
return h.clients.GetUsers()
}
func (h *Handler) GetUsersCount(context.Context) int64 {
return h.clients.GetCount()
}
func (h *Handler) AddUser(ctx context.Context, user *protocol.MemoryUser) error {
if len(user.Email) > 0 && !h.usersByEmail.Add(user) {
return errors.New("User ", user.Email, " already exists.")
@ -321,7 +340,7 @@ func (h *Handler) generateCommand(ctx context.Context, request *protocol.Request
}
errors.LogDebug(ctx, "pick detour handler for port ", port, " for ", availableMin, " minutes.")
user := inboundHandler.GetUser(request.User.Email)
user := inboundHandler.GetOrGenerateUser(request.User.Email)
if user == nil {
return nil
}

View file

@ -56,6 +56,20 @@ func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
return nil
}
func (v *TimedUserValidator) GetUsers() []*protocol.MemoryUser {
v.Lock()
defer v.Unlock()
dst := make([]*protocol.MemoryUser, len(v.users))
copy(dst, v.users)
return dst
}
func (v *TimedUserValidator) GetCount() int64 {
v.Lock()
defer v.Unlock()
return int64(len(v.users))
}
func (v *TimedUserValidator) GetAEAD(userHash []byte) (*protocol.MemoryUser, bool, error) {
v.RLock()
defer v.RUnlock()