mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-16 20:27:04 +03:00
Some checks are pending
Build docker image / build-image (push) Waiting to run
Build and Release / prepare (push) Waiting to run
Build and Release / build (386, freebsd, , ) (push) Blocked by required conditions
Build and Release / build (386, linux, , ) (push) Blocked by required conditions
Build and Release / build (386, openbsd, , ) (push) Blocked by required conditions
Build and Release / build (386, windows, , ) (push) Blocked by required conditions
Build and Release / build (386, windows, 1.21.4, win7-32) (push) Blocked by required conditions
Build and Release / build (amd64, darwin, , ) (push) Blocked by required conditions
Build and Release / build (amd64, freebsd, , ) (push) Blocked by required conditions
Build and Release / build (amd64, linux, , ) (push) Blocked by required conditions
Build and Release / build (amd64, openbsd, , ) (push) Blocked by required conditions
Build and Release / build (amd64, windows, , ) (push) Blocked by required conditions
Build and Release / build (amd64, windows, 1.21.4, win7-64) (push) Blocked by required conditions
Build and Release / build (arm, 5, linux) (push) Blocked by required conditions
Build and Release / build (arm, 6, linux) (push) Blocked by required conditions
Build and Release / build (arm, 7, freebsd) (push) Blocked by required conditions
Build and Release / build (arm, 7, linux) (push) Blocked by required conditions
Build and Release / build (arm, 7, openbsd) (push) Blocked by required conditions
Build and Release / build (arm, 7, windows) (push) Blocked by required conditions
Build and Release / build (arm64, android) (push) Blocked by required conditions
Build and Release / build (arm64, darwin) (push) Blocked by required conditions
Build and Release / build (arm64, freebsd) (push) Blocked by required conditions
Build and Release / build (arm64, linux) (push) Blocked by required conditions
Build and Release / build (arm64, openbsd) (push) Blocked by required conditions
Build and Release / build (arm64, windows) (push) Blocked by required conditions
Build and Release / build (loong64, linux) (push) Blocked by required conditions
Build and Release / build (mips, linux) (push) Blocked by required conditions
Build and Release / build (mips64, linux) (push) Blocked by required conditions
Build and Release / build (mips64le, linux) (push) Blocked by required conditions
Build and Release / build (mipsle, linux) (push) Blocked by required conditions
Build and Release / build (ppc64, linux) (push) Blocked by required conditions
Build and Release / build (ppc64le, linux) (push) Blocked by required conditions
Build and Release / build (riscv64, linux) (push) Blocked by required conditions
Build and Release / build (s390x, linux) (push) Blocked by required conditions
Test / test (macos-latest) (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
Test / test (windows-latest) (push) Waiting to run
* 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>
144 lines
3.3 KiB
Go
144 lines
3.3 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/app/stats"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/strmatcher"
|
|
"github.com/xtls/xray-core/core"
|
|
feature_stats "github.com/xtls/xray-core/features/stats"
|
|
grpc "google.golang.org/grpc"
|
|
)
|
|
|
|
// statsServer is an implementation of StatsService.
|
|
type statsServer struct {
|
|
stats feature_stats.Manager
|
|
startTime time.Time
|
|
}
|
|
|
|
func NewStatsServer(manager feature_stats.Manager) StatsServiceServer {
|
|
return &statsServer{
|
|
stats: manager,
|
|
startTime: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
|
|
c := s.stats.GetCounter(request.Name)
|
|
if c == nil {
|
|
return nil, errors.New(request.Name, " not found.")
|
|
}
|
|
var value int64
|
|
if request.Reset_ {
|
|
value = c.Set(0)
|
|
} else {
|
|
value = c.Value()
|
|
}
|
|
return &GetStatsResponse{
|
|
Stat: &Stat{
|
|
Name: request.Name,
|
|
Value: value,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *statsServer) GetStatsOnline(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
|
|
c := s.stats.GetOnlineMap(request.Name)
|
|
if c == nil {
|
|
return nil, errors.New(request.Name, " not found.")
|
|
}
|
|
value := int64(c.Count())
|
|
return &GetStatsResponse{
|
|
Stat: &Stat{
|
|
Name: request.Name,
|
|
Value: value,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
|
|
matcher, err := strmatcher.Substr.New(request.Pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := &QueryStatsResponse{}
|
|
|
|
manager, ok := s.stats.(*stats.Manager)
|
|
if !ok {
|
|
return nil, errors.New("QueryStats only works its own stats.Manager.")
|
|
}
|
|
|
|
manager.VisitCounters(func(name string, c feature_stats.Counter) bool {
|
|
if matcher.Match(name) {
|
|
var value int64
|
|
if request.Reset_ {
|
|
value = c.Set(0)
|
|
} else {
|
|
value = c.Value()
|
|
}
|
|
response.Stat = append(response.Stat, &Stat{
|
|
Name: name,
|
|
Value: value,
|
|
})
|
|
}
|
|
return true
|
|
})
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (s *statsServer) GetSysStats(ctx context.Context, request *SysStatsRequest) (*SysStatsResponse, error) {
|
|
var rtm runtime.MemStats
|
|
runtime.ReadMemStats(&rtm)
|
|
|
|
uptime := time.Since(s.startTime)
|
|
|
|
response := &SysStatsResponse{
|
|
Uptime: uint32(uptime.Seconds()),
|
|
NumGoroutine: uint32(runtime.NumGoroutine()),
|
|
Alloc: rtm.Alloc,
|
|
TotalAlloc: rtm.TotalAlloc,
|
|
Sys: rtm.Sys,
|
|
Mallocs: rtm.Mallocs,
|
|
Frees: rtm.Frees,
|
|
LiveObjects: rtm.Mallocs - rtm.Frees,
|
|
NumGC: rtm.NumGC,
|
|
PauseTotalNs: rtm.PauseTotalNs,
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (s *statsServer) mustEmbedUnimplementedStatsServiceServer() {}
|
|
|
|
type service struct {
|
|
statsManager feature_stats.Manager
|
|
}
|
|
|
|
func (s *service) Register(server *grpc.Server) {
|
|
ss := NewStatsServer(s.statsManager)
|
|
RegisterStatsServiceServer(server, ss)
|
|
|
|
// For compatibility purposes
|
|
vCoreDesc := StatsService_ServiceDesc
|
|
vCoreDesc.ServiceName = "v2ray.core.app.stats.command.StatsService"
|
|
server.RegisterService(&vCoreDesc, ss)
|
|
}
|
|
|
|
func init() {
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
|
s := new(service)
|
|
|
|
core.RequireFeatures(ctx, func(sm feature_stats.Manager) {
|
|
s.statsManager = sm
|
|
})
|
|
|
|
return s, nil
|
|
}))
|
|
}
|