Add traffic stats tutorial (#284)

* Add traffic stats tutorial

Co-authored-by: ben-august <36500518+ben-august@users.noreply.github.com>

* fix prettier

Co-authored-by: ben-august <36500518+ben-august@users.noreply.github.com>
pull/286/head
yuhan6665 2022-07-15 23:15:53 -04:00 committed by GitHub
parent e1e74c55be
commit 993a8dd177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 247 additions and 2 deletions

View File

@ -152,6 +152,7 @@ export function getDocumentLv2Sidebar(
path + "tproxy.md",
path + "iptables_gid.md",
path + "redirect.md",
path + "traffic_stats.md",
],
},
];

View File

@ -14,6 +14,10 @@
在 iptables/nftables 实现的透明代理中,一种新的规避 Xray 流量的方式。
[通过 Xray 将特定的流量指向特定出口,实现全局路由“分流”](./redirect.md) by <img src="https://avatars.githubusercontent.com/u/28607089?s=460" width="32" height="32" alt="a"/> [@Zzz3m](https://github.com/Zzz3m)
[通过 Xray 将特定的流量指向特定出口,实现全局路由“分流”](./redirect.md) by <img src="https://avatars.githubusercontent.com/u/28607089?s=32" width="32" height="32" alt="a"/> [@Zzz3m](https://github.com/Zzz3m)
将 Xray 玩出花:基于 fwmark 或 sendThrough 方式实现“分流”。
[Xray 流量统计](./traffic_stats.md) by <img src="https://avatars.githubusercontent.com/u/1588741?s=32" width="32" height="32" alt="a"/> [@yuhan6665](https://github.com/yuhan6665)
适配 Xray 的流量统计和脚本。

View File

@ -0,0 +1,118 @@
---
title: 流量统计
---
# 流量统计配置教程
请熟悉[流量统计 白话文教程](https://guide.v2fly.org/advanced/traffic.html),本文在其基础上适配了 Xray1.5.9+)。
## 查看流量信息
配置方法与 v2fly 一致。
查看流量信息是 xray 命令行的其中一个功能。配置内设置的 api dokodemo-door 端口,即为 `--server` 参数的端口。
```bash
xray api statsquery --server=127.0.0.1:10085 #查看所有流量
xray help api statsquery #statsquery 查询匹配的记录
xray help api stats #stats 查询一个记录
```
输出例子:
```json
{
"stat": [
{
"name": "inbound>>>vmess-quic>>>traffic>>>downlink",
"value": "1176"
},
{
"name": "user>>>love@example.com>>>traffic>>>downlink",
"value": "2040"
},
{
"name": "inbound>>>api>>>traffic>>>uplink",
"value": "14247"
},
{
"name": "user>>>love@example.com>>>traffic>>>uplink",
"value": "2520"
},
{
"name": "inbound>>>api>>>traffic>>>downlink",
"value": "87618"
},
{
"name": "outbound>>>direct>>>traffic>>>downlink",
"value": "0"
},
{
"name": "inbound>>>vmess-quic>>>traffic>>>uplink",
"value": "1691"
},
{
"name": "outbound>>>direct>>>traffic>>>uplink",
"value": "0"
}
]
}
```
## 流量信息的处理
把以下脚本保存到 `traffic.sh`,注意使用 `chmod 755 traffic.sh` 授予执行权限。注意调整修改 `_APISERVER` 一行的连接具体的端口参数。
```bash
#!/bin/bash
_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 "-----------------------------"
```

View File

@ -14,6 +14,10 @@
在 iptables/nftables 实现的透明代理中,一种新的规避 Xray 流量的方式。
[通过 Xray 将特定的流量指向特定出口,实现全局路由“分流”](./redirect.md) by <img src="https://avatars.githubusercontent.com/u/28607089?s=460" width="32" height="32" alt="a"/> [@Zzz3m](https://github.com/Zzz3m)
[通过 Xray 将特定的流量指向特定出口,实现全局路由“分流”](./redirect.md) by <img src="https://avatars.githubusercontent.com/u/28607089?s=32" width="32" height="32" alt="a"/> [@Zzz3m](https://github.com/Zzz3m)
将 Xray 玩出花:基于 fwmark 或 sendThrough 方式实现“分流”。
[Xray 流量统计](./traffic_stats.md) by <img src="https://avatars.githubusercontent.com/u/1588741?s=32" width="32" height="32" alt="a"/> [@yuhan6665](https://github.com/yuhan6665)
适配 Xray 的流量统计和脚本。

View File

@ -0,0 +1,118 @@
---
title: 流量统计
---
# 流量统计配置教程
请熟悉[流量统计 白话文教程](https://guide.v2fly.org/advanced/traffic.html),本文在其基础上适配了 Xray1.5.9+)。
## 查看流量信息
配置方法与 v2fly 一致。
查看流量信息是 xray 命令行的其中一个功能。配置内设置的 api dokodemo-door 端口,即为 `--server` 参数的端口。
```bash
xray api statsquery --server=127.0.0.1:10085 #查看所有流量
xray help api statsquery #statsquery 查询匹配的记录
xray help api stats #stats 查询一个记录
```
输出例子:
```json
{
"stat": [
{
"name": "inbound>>>vmess-quic>>>traffic>>>downlink",
"value": "1176"
},
{
"name": "user>>>love@example.com>>>traffic>>>downlink",
"value": "2040"
},
{
"name": "inbound>>>api>>>traffic>>>uplink",
"value": "14247"
},
{
"name": "user>>>love@example.com>>>traffic>>>uplink",
"value": "2520"
},
{
"name": "inbound>>>api>>>traffic>>>downlink",
"value": "87618"
},
{
"name": "outbound>>>direct>>>traffic>>>downlink",
"value": "0"
},
{
"name": "inbound>>>vmess-quic>>>traffic>>>uplink",
"value": "1691"
},
{
"name": "outbound>>>direct>>>traffic>>>uplink",
"value": "0"
}
]
}
```
## 流量信息的处理
把以下脚本保存到 `traffic.sh`,注意使用 `chmod 755 traffic.sh` 授予执行权限。注意调整修改 `_APISERVER` 一行的连接具体的端口参数。
```bash
#!/bin/bash
_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 "-----------------------------"
```