API: Add user online stats (#3637)

* add statsUserOnline bool to policy

* add OnlineMap struct to stats

* apply UserOnline functionality to dispatcher

* add statsonline api command

* fix comments

* Update app/stats/online_map.go

Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>

* improve AddIP

* regenerate pb

---------

Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>
This commit is contained in:
Hossin Asaadi 2024-11-03 17:44:15 +04:00 committed by GitHub
parent e3276df725
commit 2c72864935
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 575 additions and 177 deletions

View file

@ -20,6 +20,18 @@ type Counter interface {
Add(int64) int64
}
// OnlineMap is the interface for stats.
//
// xray:api:stable
type OnlineMap interface {
// Count is the current value of the OnlineMap.
Count() int
// AddIP adds a ip to the current OnlineMap.
AddIP(string)
// List is the current OnlineMap ip list.
List() []string
}
// Channel is the interface for stats channel.
//
// xray:api:stable
@ -70,6 +82,13 @@ type Manager interface {
// GetCounter returns a counter by its identifier.
GetCounter(string) Counter
// RegisterOnlineMap registers a new onlinemap to the manager. The identifier string must not be empty, and unique among other onlinemaps.
RegisterOnlineMap(string) (OnlineMap, error)
// UnregisterOnlineMap unregisters a onlinemap from the manager by its identifier.
UnregisterOnlineMap(string) error
// GetOnlineMap returns a onlinemap by its identifier.
GetOnlineMap(string) OnlineMap
// RegisterChannel registers a new channel to the manager. The identifier string must not be empty, and unique among other channels.
RegisterChannel(string) (Channel, error)
// UnregisterChannel unregisters a channel from the manager by its identifier.
@ -88,6 +107,16 @@ func GetOrRegisterCounter(m Manager, name string) (Counter, error) {
return m.RegisterCounter(name)
}
// GetOrRegisterOnlineMap tries to get the OnlineMap first. If not exist, it then tries to create a new onlinemap.
func GetOrRegisterOnlineMap(m Manager, name string) (OnlineMap, error) {
onlineMap := m.GetOnlineMap(name)
if onlineMap != nil {
return onlineMap, nil
}
return m.RegisterOnlineMap(name)
}
// GetOrRegisterChannel tries to get the StatChannel first. If not exist, it then tries to create a new channel.
func GetOrRegisterChannel(m Manager, name string) (Channel, error) {
channel := m.GetChannel(name)
@ -128,6 +157,21 @@ func (NoopManager) GetCounter(string) Counter {
return nil
}
// RegisterOnlineMap implements Manager.
func (NoopManager) RegisterOnlineMap(string) (OnlineMap, error) {
return nil, errors.New("not implemented")
}
// UnregisterOnlineMap implements Manager.
func (NoopManager) UnregisterOnlineMap(string) error {
return nil
}
// GetOnlineMap implements Manager.
func (NoopManager) GetOnlineMap(string) OnlineMap {
return nil
}
// RegisterChannel implements Manager.
func (NoopManager) RegisterChannel(string) (Channel, error) {
return nil, errors.New("not implemented")