API & Commands: Add GetUsersStatsRequest(); Improve api statsonlineiplist (#5776)

https://github.com/XTLS/Xray-core/pull/5776#issuecomment-4230007504
This commit is contained in:
Yury Kastov
2026-04-11 22:09:24 +03:00
committed by GitHub
parent 32937846c5
commit a91a88c7b2
10 changed files with 580 additions and 116 deletions
+35 -19
View File
@@ -11,19 +11,19 @@ import (
// Manager is an implementation of stats.Manager.
type Manager struct {
access sync.RWMutex
counters map[string]*Counter
onlineMap map[string]*OnlineMap
channels map[string]*Channel
running bool
access sync.RWMutex
counters map[string]*Counter
onlineMaps map[string]*OnlineMap
channels map[string]*Channel
running bool
}
// NewManager creates an instance of Statistics Manager.
func NewManager(ctx context.Context, config *Config) (*Manager, error) {
m := &Manager{
counters: make(map[string]*Counter),
onlineMap: make(map[string]*OnlineMap),
channels: make(map[string]*Channel),
counters: make(map[string]*Counter),
onlineMaps: make(map[string]*OnlineMap),
channels: make(map[string]*Channel),
}
return m, nil
@@ -88,12 +88,12 @@ func (m *Manager) RegisterOnlineMap(name string) (stats.OnlineMap, error) {
m.access.Lock()
defer m.access.Unlock()
if _, found := m.onlineMap[name]; found {
return nil, errors.New("onlineMap ", name, " already registered.")
if _, found := m.onlineMaps[name]; found {
return nil, errors.New("OnlineMap ", name, " already registered.")
}
errors.LogDebug(context.Background(), "create new onlineMap ", name)
errors.LogDebug(context.Background(), "create new OnlineMap ", name)
om := NewOnlineMap()
m.onlineMap[name] = om
m.onlineMaps[name] = om
return om, nil
}
@@ -102,9 +102,9 @@ func (m *Manager) UnregisterOnlineMap(name string) error {
m.access.Lock()
defer m.access.Unlock()
if _, found := m.onlineMap[name]; found {
errors.LogDebug(context.Background(), "remove onlineMap ", name)
delete(m.onlineMap, name)
if _, found := m.onlineMaps[name]; found {
errors.LogDebug(context.Background(), "remove OnlineMap ", name)
delete(m.onlineMaps, name)
}
return nil
}
@@ -114,12 +114,24 @@ func (m *Manager) GetOnlineMap(name string) stats.OnlineMap {
m.access.RLock()
defer m.access.RUnlock()
if om, found := m.onlineMap[name]; found {
if om, found := m.onlineMaps[name]; found {
return om
}
return nil
}
// VisitOnlineMaps calls visitor function on all managed online maps.
// The visitor runs under a read lock; it must not call RegisterOnlineMap or UnregisterOnlineMap (would deadlock).
func (m *Manager) VisitOnlineMaps(visitor func(string, stats.OnlineMap) bool) {
m.access.RLock()
defer m.access.RUnlock()
for name, om := range m.onlineMaps {
if !visitor(name, om) {
break
}
}
}
// RegisterChannel implements stats.Manager.
func (m *Manager) RegisterChannel(name string) (stats.Channel, error) {
m.access.Lock()
@@ -166,9 +178,9 @@ func (m *Manager) GetAllOnlineUsers() []string {
m.access.RLock()
defer m.access.RUnlock()
usersOnline := make([]string, 0, len(m.onlineMap))
for user, onlineMap := range m.onlineMap {
if onlineMap.Count() > 0 {
usersOnline := make([]string, 0, len(m.onlineMaps))
for user, om := range m.onlineMaps {
if om.Count() > 0 {
usersOnline = append(usersOnline, user)
}
}
@@ -198,6 +210,10 @@ func (m *Manager) Close() error {
m.access.Lock()
defer m.access.Unlock()
m.running = false
for name := range m.onlineMaps {
errors.LogDebug(context.Background(), "remove OnlineMap ", name)
delete(m.onlineMaps, name)
}
errs := []error{}
for name, channel := range m.channels {
errors.LogDebug(context.Background(), "remove channel ", name)