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,8 @@
package vless
import (
"google.golang.org/protobuf/proto"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/uuid"
@ -37,3 +39,11 @@ func (a *MemoryAccount) Equals(account protocol.Account) bool {
}
return a.ID.Equals(vlessAccount.ID)
}
func (a *MemoryAccount) ToProto() proto.Message {
return &Account{
Id: a.ID.String(),
Flow: a.Flow,
Encryption: a.Encryption,
}
}

View file

@ -172,6 +172,21 @@ func (h *Handler) RemoveUser(ctx context.Context, e string) error {
return h.validator.Del(e)
}
// GetUser implements proxy.UserManager.GetUser().
func (h *Handler) GetUser(ctx context.Context, email string) *protocol.MemoryUser {
return h.validator.GetByEmail(email)
}
// GetUsers implements proxy.UserManager.GetUsers().
func (h *Handler) GetUsers(ctx context.Context) []*protocol.MemoryUser {
return h.validator.GetAll()
}
// GetUsersCount implements proxy.UserManager.GetUsersCount().
func (h *Handler) GetUsersCount(context.Context) int64 {
return h.validator.GetCount()
}
// Network implements proxy.Inbound.Network().
func (*Handler) Network() []net.Network {
return []net.Network{net.Network_TCP, net.Network_UNIX}

View file

@ -13,6 +13,9 @@ type Validator interface {
Get(id uuid.UUID) *protocol.MemoryUser
Add(u *protocol.MemoryUser) error
Del(email string) error
GetByEmail(email string) *protocol.MemoryUser
GetAll() []*protocol.MemoryUser
GetCount() int64
}
// MemoryValidator stores valid VLESS users.
@ -57,3 +60,32 @@ func (v *MemoryValidator) Get(id uuid.UUID) *protocol.MemoryUser {
}
return nil
}
// Get a VLESS user with email, nil if user doesn't exist.
func (v *MemoryValidator) GetByEmail(email string) *protocol.MemoryUser {
u, _ := v.email.Load(email)
if u != nil {
return u.(*protocol.MemoryUser)
}
return nil
}
// Get all users
func (v *MemoryValidator) GetAll() []*protocol.MemoryUser {
var u = make([]*protocol.MemoryUser, 0, 100)
v.email.Range(func(key, value interface{}) bool {
u = append(u, value.(*protocol.MemoryUser))
return true
})
return u
}
// Get users count
func (v *MemoryValidator) GetCount() int64 {
var c int64 = 0
v.email.Range(func(key, value interface{}) bool {
c++
return true
})
return c
}