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

@ -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
}