XTLS / Xray-core

Xray, Penetrates Everything. Also the best v2ray-core, with XTLS support. Fully compatible configuration.
https://t.me/projectXray
Mozilla Public License 2.0
23.17k stars 3.71k forks source link

how to observe traffic Down Up used by each clients of an inbound in vmess vless trojan ...protocols #1549

Closed lovebinance closed 3 months ago

lovebinance commented 1 year ago

title is enough to explain problem

Fangliding commented 1 year ago

@lovebinance 大年初一不过年了在这写issue

lovebinance commented 1 year ago

谢谢你解决这个问题

On Sat, Jan 21, 2023, 21:38 风扇滑翔翼 @.***> wrote:

@lovebinance https://github.com/lovebinance 大年初一不过年了在这写issue

— Reply to this email directly, view it on GitHub https://github.com/XTLS/Xray-core/issues/1549#issuecomment-1399302456, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5IKNAT33K56VKXRING2UJLWTQQZDANCNFSM6AAAAAAUCQQ5S4 . You are receiving this because you were mentioned.Message ID: @.***>

Fangliding commented 1 year ago

There are some api to get inbound traffic https://xtls.github.io/config/api.html#apiobject

lovebinance commented 1 year ago

thanks alot but is there solution to capture traffic Down Up used by each clients of an inbound in vmess vless trojan ...protocols

On Sat, Jan 21, 2023, 21:48 风扇滑翔翼 @.***> wrote:

There are some api to get inbound traffic https://xtls.github.io/config/api.html#apiobject

— Reply to this email directly, view it on GitHub https://github.com/XTLS/Xray-core/issues/1549#issuecomment-1399304547, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5IKNAWYPJBSUPAPJFUOTZDWTQSAJANCNFSM6AAAAAAUCQQ5S4 . You are receiving this because you were mentioned.Message ID: @.***>

lovebinance commented 1 year ago

@Fangliding what is xray api port to connecti it ?

zxbiao commented 1 year ago

is define in config.json , use "dokodemo-door" protocol in inbound. the example code port is 10085, you can change it by yourself.

zxbiao commented 1 year ago

also you can use this shell script to query traffic.

#!/bin/bash
##xray api shell script

_APISERVER=127.0.0.1:10085
_XRAY=/usr/local/bin/xray

apidata () {
    local ARGS=
    if [[ $1 == "reset" ]]; then
      ARGS="reset: true"
    fi
    $_XRAY api statsquery --server=$_APISERVER "${ARGS}" \
    | awk '{
        if (match($1, /"name":/)) {
            f=1; gsub(/^"|link"|,$/, "", $2);
            split($2, p,  ">>>");
            printf "%s:%s->%s\t", p[1],p[2],p[4];
        }
        else if (match($1, /"value":/) && f){
          f = 0;
          gsub(/"/, "", $2);
          printf "%.0f\n", $2;
        }
        else if (match($0, /}/) && f) { f = 0; print 0; }
    }'
}

print_sum() {
    local DATA="$1"
    local PREFIX="$2"
    local SORTED=$(echo "$DATA" | grep "^${PREFIX}" | sort -r)
    local SUM=$(echo "$SORTED" | awk '
        /->up/{us+=$2}
        /->down/{ds+=$2}
        END{
            printf "SUM->up:\t%.0f\nSUM->down:\t%.0f\nSUM->TOTAL:\t%.0f\n", us, ds, us+ds;
        }')
    echo -e "${SORTED}\n${SUM}" \
    | numfmt --field=2 --suffix=B --to=iec \
    | column -t
}

DATA=$(apidata $1)
echo "------------Inbound----------"
print_sum "$DATA" "inbound"
echo "-----------------------------"
echo "------------Outbound---------"
print_sum "$DATA" "outbound"
echo "-----------------------------"
echo
echo "-------------User------------"
print_sum "$DATA" "user"
echo "-----------------------------"
lovebinance commented 1 year ago

according to document:

Currently available statistics are as follows: User data user>>>[email]>>>traffic>>>uplink Upstream traffic of a specific user, in bytes. user>>>[email]>>>traffic>>>downlink The downlink traffic of a specific user, in bytes.

but i can not find user data by this query

/usr/local/x-ui/bin/xray-linux-amd64 api statsquery --server=127.0.0.1:62789 reset: true { "stat": [ { "name": "inbound>>>api>>>traffic>>>uplink", "value": 217 }, { "name": "inbound>>>api>>>traffic>>>downlink", "value": 328 }, { "name": "inbound>>>inbound-4425>>>traffic>>>uplink", "value": 43656 }, { "name": "inbound>>>inbound-4425>>>traffic>>>downlink", "value": 6440 } ] }

so how can find my users/clients traffic in an inbound (inbound-4425 here) i dont see user related stat

zxbiao commented 1 year ago

step1:you must define the traffic level,

according to document:

Currently available statistics are as follows: User data user>>>[email]>>>traffic>>>uplink Upstream traffic of a specific user, in bytes. user>>>[email]>>>traffic>>>downlink The downlink traffic of a specific user, in bytes.

but i can not find user data by this query

/usr/local/x-ui/bin/xray-linux-amd64 api statsquery --server=127.0.0.1:62789 reset: true { "stat": [ { "name": "inbound>>>api>>>traffic>>>uplink", "value": 217 }, { "name": "inbound>>>api>>>traffic>>>downlink", "value": 328 }, { "name": "inbound>>>inbound-4425>>>traffic>>>uplink", "value": 43656 }, { "name": "inbound>>>inbound-4425>>>traffic>>>downlink", "value": 6440 } ] }

so how can find my users/clients traffic in an inbound (inbound-4425 here) i dont see user related stat

step 1 , you must enable "stats" and define user traffic level(policy). Like this code, some comments are in Chinese, you can translate into your language for memo.

  //enable traffic
  "stats": {},
  //本地策略
  "policy":
  {
    //创建策略等级
    "levels":
    {
      //策略等级“0”,此数字对应用户等级。
      "0":
      {
        //连接建立时的握手时间限制。单位为秒。默认值为 4。
        "handshake": 4,
        //连接空闲的时间限制。单位为秒。默认值为 300。
        "connIdle": 300,
        //当连接下行线路关闭后的时间限制。单位为秒。默认值为 2。
        "uplinkOnly": 2,
        //当连接上行线路关闭后的时间限制。单位为秒。默认值为 5。当客户端(如浏览器)关闭上行连接时,入站代理会在等待 downlinkOnly 时间后中断连接。
        "downlinkOnly": 5,
        //是否开启当前等级的所有用户的上行流量统计。值:true | false
        "statsUserUplink": true,
        //是否开启当前等级的所有用户的下行行流量统计。值:true | false
        "statsUserDownlink": true,
        //每个连接的内部缓存大小。单位为 kB。当值为 0 时,内部缓存被禁用。
        "bufferSize": 4
      }
    },
    //系统级别策略
    "system":
    {
      //开启所有入站代理的上行流量统计
      "statsInboundUplink": true,
      //开启所有入站代理的下行流量统计。
      "statsInboundDownlink": true,
      //开启所有出站代理的上行流量统计。
      "statsOutboundUplink": true,
      //开启所有出站代理的下行流量统计。。
      "statsOutboundDownlink": true
    }
  },

step 2 , set the user level. like this

"clients":[
          {
            "id": "2f5b7090-xxxx-xxxx-xxxx-826a6f579835",
            "level": 0,
            "email": "jie",
            "flow": "xtls-rprx-vision"

          },
          {
            "id": "eb4c347a-xxxx-xxxx-xxxx-2b95c1a2ec47", 
            "level": 0,
            "email": "bang",
            "flow": "xtls-rprx-vision"
          },
          {
            "id": "e513f249-xxxx-xxxx-xxxx-3c7bae3afc26",
            "level": 0,
            "email": "zxbiao",
            "flow": "xtls-rprx-vision"
          }
]

The query results are as follows

# bash /home/Xray-traffic.sh
------------Inbound----------
inbound:websocket-in->up    1.4KB
inbound:websocket-in->down  1.3KB
inbound:vless-in->up        90MB
inbound:vless-in->down      6.8GB
inbound:trojan-in->up       0B
inbound:trojan-in->down     0B
inbound:ss2022-in->up       47B
inbound:ss2022-in->down     0B
inbound:gRPC-in->up         0B
inbound:gRPC-in->down       0B
inbound:api->up             387B
inbound:api->down           1.4KB
SUM->up:                    90MB
SUM->down:                  6.8GB
SUM->TOTAL:                 6.9GB
-----------------------------
------------Outbound----------
outbound:direct->up           71MB
outbound:direct->down         6.7GB
outbound:cnblock->up          0B
outbound:cnblock->down        0B
outbound:block->up            0B
outbound:block->down          0B
outbound:adblock->up          0B
outbound:adblock->down        0B
SUM->up:                      71MB
SUM->down:                    6.7GB
SUM->TOTAL:                   6.8GB
-----------------------------

-------------User------------
user:jie->up                    32MB
user:jie->down                  4.4GB
user:zxbiao->up                 37MB
user:zxbiao->down            1.8GB
user:bang->up                   4.4MB
user:bang->down                 616MB
SUM->up:                        72MB
SUM->down:                      6.7GB
SUM->TOTAL:                     6.8GB
-----------------------------
lovebinance commented 1 year ago

i did it but not work for me i copied but user not added

/usr/local/bin/xray-linux-amd64 api statsquery --server=127.0.0.1:62789 { "stat": [ { "name": "inbound>>>api>>>traffic>>>uplink", "value": "340" }, { "name": "inbound>>>api>>>traffic>>>downlink", "value": "403" }, { "name": "inbound>>>inbound-4425>>>traffic>>>uplink", "value": "0" }, { "name": "inbound>>>inbound-4425>>>traffic>>>downlink", "value": "0" }, { "name": "outbound>>>blocked>>>traffic>>>uplink", "value": "0" }, { "name": "outbound>>>blocked>>>traffic>>>downlink", "value": "0" } ] }

MrSaeid007 commented 1 year ago

i did it but not work for me i copied but user not added

/usr/local/bin/xray-linux-amd64 api statsquery --server=127.0.0.1:62789 { "stat": [ { "name": "inbound>>>api>>>traffic>>>uplink", "value": "340" }, { "name": "inbound>>>api>>>traffic>>>downlink", "value": "403" }, { "name": "inbound>>>inbound-4425>>>traffic>>>uplink", "value": "0" }, { "name": "inbound>>>inbound-4425>>>traffic>>>downlink", "value": "0" }, { "name": "outbound>>>blocked>>>traffic>>>uplink", "value": "0" }, { "name": "outbound>>>blocked>>>traffic>>>downlink", "value": "0" } ] }

Is that possible to block ad in xray config template? If possible what is the config

MehdiPoustchi commented 6 months ago

Hey. I have the following config which is what you said BUT the xray api statsquery shows ZERO usage for USER1 in REALITY mode (Even though this user downloaded more than 1GB of data). If I create a VLESS-WS-TCP then it works fine!

this issue only happens in REALITY

{
  "stats": {},
  "api": {
    "tag": "api",
    "services": [
      "StatsService"
    ]
  },
  "policy": {
    "levels": {
      "0": {
        "statsUserUplink": true,
        "statsUserDownlink": true
      }
    },
    "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": {}
    },
    {
      "listen": "0.0.0.0",
      "port": "443",
      "protocol": "vless",
      "settings": {
        "decryption": "none",
        "clients": [
          {
            "id": "bxxxxxa5-xxx-xxx-8972-xxxx",
            "level": 0,
            "email": "user1",
            "flow": "xtls-rprx-vision"
          }
        ],
        "fallbacks": []
      },
      "streamSettings": {
        "network": "tcp",
        "security": "reality",
        "realitySettings": {
          "show": false,
          "dest": "www.yahoo.com:443",
          "xver": 0,
          "serverNames": [
            "www.discord.com",
            "discord.com",
          ],
          "privateKey": "XXXXXX",
          "publicKey": "XXXXX",
          "minClientVer": "",
          "maxClientVer": "",
          "shortIds": [
            "",
          ],
          "spiderX": "/test"
        }
      },
      "sniffing": {
        "enabled": true,
        "destOverride": [
          "http",
          "tls",
          "quic"
        ]
      }
    }
  ],
  "routing": {
    "domainStrategy": "AsIs",
    "rules": [
      {
        "type": "field",
        "outboundTag": "api",
        "inboundTag": [
          "api"
        ]
      },
      {
        "type": "field",
        "outboundTag": "blocked",
        "protocol": [
          "bittorrent"
        ]
      }
    ]
  },
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {},
      "tag": "direct"
    }
  ],
  "reverse": {},
  "transport": {}
}