XTLS / libXray

MIT License
64 stars 57 forks source link

LibXrayQueryStats() for usage traffic #6

Closed houmie closed 1 year ago

houmie commented 1 year ago

Hello,

What is the best way to get the usage (traffic)?

import UIKit
import NetworkExtension
import LibXray

class ViewController: UIViewController {

    var tunnelProviderManager: NETunnelProviderManager?
    var isTimerUsageRunning = false
    var timerUsage = Timer()

    override func viewDidLoad() {
        super.viewDidLoad()
        if self.isTimerUsageRunning == false {
            self.isTimerUsageRunning = true
            self.timerUsage = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [self] _ in
                LibXrayQueryStats("127.0.0.1:8080", TGConstant.homeDirectory.absoluteString + "test.json")
            })
        }
}

I try to get the stats written to test.json every second. Is this the correct approach?

ghost commented 1 year ago

You must know, this is not an iOS developer community. You should finish this by yourself. I will NOT reply every issue about iOS developing.

houmie commented 1 year ago

Apologies for the misunderstanding. I completely understand.

I have created a demo project just to learn the wrapper and everything else works great. I can start, stop and use configs. You did a fantastic job with this project. Just sometimes the description of the methods isn't clear to me like the LibXrayQueryStats().

In particular the strategy/restriction behind it in Go isn't clear. Is it ok to call the method every minute to get the statistics? Or is the method allowed to be called from the main target?

But no problem, I understand. Maybe it would make sense to enable the "Discussion" tab on your GitHub project, so that other users can help each other. That would be great, because I know you're busy.

Thanks again for this great project.

houmie commented 1 year ago

Hello friend @yiguous

I think the issue is in Go.

Checkout this line

I have added a more descriptive error message in your Go library and compiled it afterwards again:

if err != nil {
    fmt.Println("Directory Error: " + err.Error())
    return err.Error()
}

Then in Xcode when I execute the library: LibXrayQueryStats("127.0.0.1:10085", statFilePath)

I get this error message:

Directory Error: context deadline exceeded

This means it can't connect to 127.0.0.1:10085 within three seconds and it times out.

My config looks like this below. I know it's a bit hard to read. But you can see dokodemo-door is set to 127.0.01:10085. Everything else is also in place.

let dataString = "{\"api\": {\"services\": [\"HandlerService\",\"LoggerService\",\"StatsService\"],\"tag\": \"api\"},\"stats\": {},\"policy\": {\"levels\": {\"0\": {\"statsUserUplink\": true,\"statsUserDownlink\": true,\"handshake\":3,\"connIdle\":180}},\"system\": {\"statsInboundUplink\": true,\"statsInboundDownlink\": true,\"statsOutboundUplink\": true,\"statsOutboundDownlink\": true}},\"inbounds\":[{\"listen\": \"127.0.0.1\",\"port\": 10085,\"protocol\": \"dokodemo-door\",\"settings\": {\"address\": \"127.0.0.1\"},\"tag\": \"api\",\"sniffing\": null},{\"listen\":\"[::1]\",\"port\":8080,\"protocol\":\"socks\",\"settings\":{\"auth\":\"noauth\",\"udp\":true},\"sniffing\":{\"destOverride\":[\"http\",\"tls\"],\"enabled\":true,\"excludedDomains\":[],\"metadataOnly\":false,\"routeOnly\":false},\"tag\":\"socks-in\"}],\"outbounds\":[{\"protocol\":\"vless\",\"settings\":{\"vnext\":[{\"address\":\"xxx.xxx.xxx.xx\",\"port\":443,\"users\":[{\"encryption\":\"none\",\"flow\":\"xtls-rprx-vision\",\"id\":\"8652b34d-a236-4eca-8a08-b30ea4e63cba\"}]}]},\"streamSettings\":{\"network\":\"tcp\",\"realitySettings\":{\"fingerprint\":\"chrome\",\"publicKey\":\"eGZ4cJLCFo8gqCkFGuwxCRVx8FE3R2vh__uqCFZg6RM\",\"serverName\":\"www.google-analytics.com\",\"shortId\":\"1ad99ba959dec1a9\",\"show\":false,\"spiderX\":\"\"},\"security\":\"reality\",\"tcpSettings\":{\"header\":{\"type\":\"none\"}}},\"tag\":\"proxy\"},{\"protocol\":\"freedom\",\"tag\":\"direct\"},{\"protocol\":\"blackhole\",\"tag\":\"block\"}],\"routing\":{\"balancers\":[],\"domainMatcher\":\"hybrid\",\"domainStrategy\":\"IPIfNonMatch\",\"rules\":[{\"type\": \"field\",\"inboundTag\": [\"api\"],\"outboundTag\": \"api\"},{\"__enabled__\":true,\"__id__\":\"E88ACF9A-E85D-4C03-B2BF-43D0513613F3\",\"__name__\":\"Rule_E88ACF9A-E85D-4C03-B2BF-43D0513613F3\",\"domainMatcher\":\"hybrid\",\"network\":\"udp,tcp,\",\"outboundTag\":\"block\",\"protocol\":[\"bittorrent\"],\"type\":\"field\"}]}}"

Unless I'm overlooking something. Have you tested this function and it works for you?

ghost commented 1 year ago

This function works well. But I don't use it for production.

houmie commented 1 year ago

Ah that's great. May I ask which port did you use that worked for you? 127.0.0.1:??

For me, no matter what port I use the Go function prints context deadline exceeded. Thank you.

ghost commented 1 year ago

Ah that's great. May I ask which port did you use that worked for you? 127.0.0.1:??

For me, no matter what port I use the Go function prints context deadline exceeded. Thank you.

Yes. I used 127.0.0.1 . But I think it has no relationship with ip which you listen.

I have decided to remove this api. Whatever, it is useless for production.

houmie commented 1 year ago

Ah I understand. Thank you for letting me know.

I suppose it would be useful if the user could see how much traffic has been used so far. It's nice to see that on the client. But I haven't seen any Xray client to do that. So maybe it's not possible.

It works on the server though. xray api statsquery --server=127.0.0.1:10085 -pattern your@email.com

It returns the download and upload for a particular user with the given email address:

{
    "stat":  [
        {
            "name":  "user>>>your@email.com>>>traffic>>>downlink",
            "value":  "13013635"
        },
        {
            "name":  "user>>>your@email.com>>>traffic>>>uplink",
            "value":  "1553233"
        }
    ]
}

It would be so cool if this information could also be gathered on Client side. But sadly I haven't found a way.

Have a good day,

ghost commented 1 year ago

You can try V2Box, it has this feature.