Xray-core icon indicating copy to clipboard operation
Xray-core copied to clipboard

API: Add user online stats

Open hossinasaadi opened this issue 1 year ago • 6 comments

implement custom counter for ips connected to each client by email

  1. add statsUserOnline to config.json add statsUserOnline bool to policy
      "levels": {
        "0": {
          "statsUserDownlink": true,
          "statsUserUplink": true,
**        "statsUserOnline": true

        },  
  1. each ip will remain in list for 20 sec (may we can add custom value to config) https://github.com/XTLS/Xray-core/commit/0b8ad84c2ba213f79d2552de896da8b79d68fa3c
  2. client should have an email.
  3. get user online from api :
./xray api statsonline -server=127.0.0.1:8080 -email "[email protected]"
{
   "stat":  {
       "name":  "user>>>[email protected]>>>online",
       "value":  "3"
   }
}

this implementation based on https://github.com/XTLS/Xray-core/pull/2277#issuecomment-2254770107 discussion and may needs some review to improve and suggestions. @mmmray @yuhan6665

hossinasaadi avatar Aug 04 '24 14:08 hossinasaadi

hello @amir-devman please review if this API is sufficient for your usecase as well. the implementation looks fine to me at a glance, as it is disabled by default performance doesn't seem critical.

mmmray avatar Aug 04 '24 21:08 mmmray

each ip will remain in list for 20 sec (may we can add custom value to config)

That is a good option You can also add option to NOT mark a user as Online, if it's not connected for more than N seconds (if user just did a Real-Delay test, it's not online)

APT-ZERO avatar Aug 05 '24 09:08 APT-ZERO

Looks good to me! Thanks for the effort to move to API solution. I will close #2277 for now

yuhan6665 avatar Aug 06 '24 02:08 yuhan6665

each ip will remain in list for 20 sec (may we can add custom value to config)

That is a good option You can also add option to NOT mark a user as Online, if it's not connected for more than N seconds (if user just did a Real-Delay test, it's not online)

that's a good idea, checking what i can do.

hossinasaadi avatar Aug 06 '24 06:08 hossinasaadi

https://github.com/XTLS/Xray-core/pull/3644#issuecomment-2272712048

RPRX avatar Aug 07 '24 06:08 RPRX

Cannot Checking Number User Online Stats While Using Xray Fallback Xtls Vision 443

=== When using the Xray API to check online user stats, the command xray api statsonline -server=127.0.0.1:10087 -email "segs" is returning the following output:

{ "stat": { "name": "user>>>segs>>>online" } }

The output indicates that the user is online but does not show the expected value for the number ip login.

willstore69 avatar Aug 15 '24 08:08 willstore69

@hossinasaadi Can you help to resolve conflict? I think it is time to merge

yuhan6665 avatar Nov 03 '24 04:11 yuhan6665

is it possible to get all users online stats with single request ?

M03ED avatar Dec 13 '24 14:12 M03ED

@hossinasaadi

在某些场景下,使用示例中的基础配置时,会周期性地出现以下结果:

panic: runtime error: 
invalid memory address or nil pointer dereference 
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x406396] 

goroutine 1206045 [running]: 
github.com/xtls/xray-core/app/stats.(*OnlineMap).AddIP(0xc002690000, {0xc000669980, 0xd}) 
    github.com/xtls/xray-core/app/stats/online_map.go:43 +0x65 

github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).getLink(0xc0017ea360, {0x1563828, 0xc0084a8d80}) 
    github.com/xtls/xray-core/app/dispatcher/default.go:190 +0x53f 

github.com/xtls/xray-core/app/dispatcher.(*DefaultDispatcher).Dispatch(
    0xc0017ea360, {0x1563828, 0xc0084a8d80}, {0x1563978, 0xc00066994c}, 0x1466, 0x2?) 
    github.com/xtls/xray-core/app/dispatcher/default.go:266 +0x325 

github.com/xtls/xray-core/common/mux.(*Server).Dispatch(
    0x1563860?, {0x1563828?, 0xc0084a8d80?}, {0x1563978?, 0xc00066994c?}, 0x995?, 0xc??}) 
    github.com/xtls/xray-core/common/mux/server.go:41 +0xc6 

github.com/xtls/xray-core/proxy/vless/inbound.(*Handler).Process(
    0xc001580590, {0x1563828, 0xc0084a8b70}, 0x1544108?, {0x156a718, 0xc0084a8b10}, {0x1565c40, 0x0000336c0}) 
    github.com/xtls/xray-core/proxy/vless/inbound/inbound.go:523 +0x1d59 

github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).callback(
    0xc000b94500, {0x156a980, 0xc006a1b40}) 
    github.com/xtls/xray-core/app/proxyman/inbound/worker.go:109 +0x5e7 

created by github.com/xtls/xray-core/app/proxyman/inbound.(*tcpWorker).Start.func1 
    in goroutine 1205978 
    github.com/xtls/xray-core/app/proxyman/inbound/worker.go:123 +0x7a

iambabyninja avatar Dec 31 '24 10:12 iambabyninja

@iambabyninja can you try if this fix the issue? https://github.com/XTLS/Xray-core/actions/runs/12577069437

yuhan6665 avatar Jan 02 '25 03:01 yuhan6665