Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add user online stats #3637

Merged
merged 9 commits into from
Nov 3, 2024
Merged

API: Add user online stats #3637

merged 9 commits into from
Nov 3, 2024

Conversation

hossinasaadi
Copy link
Contributor

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) 0b8ad84
  2. client should have an email.
  3. get user online from api :
./xray api statsonline -server=127.0.0.1:8080 -email "user1@test.com"
{
   "stat":  {
       "name":  "user>>>user1@test.com>>>online",
       "value":  "3"
   }
}

this implementation based on #2277 (comment) discussion and may needs some review to improve and suggestions.
@mmmray @yuhan6665

@mmmray
Copy link
Collaborator

mmmray commented Aug 4, 2024

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.

app/stats/online_map.go Outdated Show resolved Hide resolved
features/stats/stats.go Show resolved Hide resolved
app/stats/online_map.go Outdated Show resolved Hide resolved
hossinasaadi and others added 2 commits August 5, 2024 09:37
Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>
@APT-ZERO
Copy link

APT-ZERO commented Aug 5, 2024

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)

@yuhan6665
Copy link
Member

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

@hossinasaadi
Copy link
Contributor Author

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.

@yuhan6665 yuhan6665 changed the title Add user online stats API: Add user online stats Aug 6, 2024
@RPRX
Copy link
Member

RPRX commented Aug 7, 2024

#3644 (comment)

@willstore69
Copy link

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.

@yuhan6665
Copy link
Member

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

@yuhan6665 yuhan6665 merged commit 2c72864 into XTLS:main Nov 3, 2024
36 checks passed
@Fangliding Fangliding mentioned this pull request Nov 8, 2024
4 tasks
@M03ED
Copy link

M03ED commented Dec 13, 2024

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

@iambabyninja
Copy link
Member

iambabyninja commented Dec 31, 2024

@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

@yuhan6665
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants