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
+37 -2
View File
@@ -7,10 +7,11 @@ import (
var cmdOnlineStatsIpList = &base.Command{
CustomFlags: true,
UsageLine: "{{.Exec}} api statsonlineiplist [--server=127.0.0.1:8080] [-email '']",
UsageLine: "{{.Exec}} api statsonlineiplist [--server=127.0.0.1:8080] [-email '' | -all [-include-traffic] [-reset]]",
Short: "Retrieve a user's online IP addresses and access times",
Long: `
Retrieve the online IP addresses and corresponding access timestamps for a user from Xray.
Use -all to retrieve all online users with their IPs and timestamps.
Arguments:
@@ -23,9 +24,20 @@ Arguments:
-email
The user's email address.
-all
Retrieve all online users with their IPs and timestamps.
-include-traffic
Include traffic statistics when using -all.
-reset
Reset traffic counters after fetching. Only with -all and -include-traffic.
Example:
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -email "xray@love.com"
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -all
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080 -all -include-traffic
`,
Run: executeOnlineStatsIpList,
}
@@ -33,12 +45,35 @@ Example:
func executeOnlineStatsIpList(cmd *base.Command, args []string) {
setSharedFlags(cmd)
email := cmd.Flag.String("email", "", "")
all := cmd.Flag.Bool("all", false, "")
includeTraffic := cmd.Flag.Bool("include-traffic", false, "")
reset := cmd.Flag.Bool("reset", false, "")
cmd.Flag.Parse(args)
statName := "user>>>" + *email + ">>>online"
if *all && *email != "" {
base.Fatalf("-all and -email are mutually exclusive")
}
if !*all && *email == "" {
base.Fatalf("either -all or -email must be specified")
}
conn, ctx, close := dialAPIServer()
defer close()
client := statsService.NewStatsServiceClient(conn)
if *all {
r := &statsService.GetUsersStatsRequest{
IncludeTraffic: *includeTraffic,
Reset_: *reset,
}
resp, err := client.GetUsersStats(ctx, r)
if err != nil {
base.Fatalf("failed to get stats: %s", err)
}
showJSONResponse(resp)
return
}
statName := "user>>>" + *email + ">>>online"
r := &statsService.GetStatsRequest{
Name: statName,
Reset_: false,