Skip to content

Instantly share code, notes, and snippets.

@boypt
Created February 22, 2019 14:27
Show Gist options
  • Save boypt/80d9ecaaa7f3c799c525e91f3c0b35d1 to your computer and use it in GitHub Desktop.
Save boypt/80d9ecaaa7f3c799c525e91f3c0b35d1 to your computer and use it in GitHub Desktop.
v2ray-core 流量统计 StatsService 使用要点
StatsService API获得数据统计功能,配置里面必须满足以下条件
1. `"stats":{}`对象的存在
2. `"api"`配置对象里面有`StatsService`
3. `"policy"`中的统计开关为true,除了各个用户的统计,还有全局统计
4. clients里面要有email
5. 有个专用的`dokodemo-door`协议的入口,tag为api
6. routing里面有inboundTag:api -> outboundTag:api的规则
另外:
- 统计的email/tag是当前的v2ray进程实例的数据,比如在服务器上统计,客户端写的email对服务器没有意义;如果在客户端统计,输出的就是客户端本身的收发数据。
- 统计API里面除了GetStats,新版本还加入了QueryStats方法,可以列出进程内的所有统计数据,包括各个tag/email的用户。
- `v2ctl api -h`有查询例子
```
v2ctl api --server=127.0.0.1:8080 StatsService.QueryStats 'pattern: "" reset: false'
v2ctl api --server=127.0.0.1:8080 StatsService.GetStats 'name: "inbound>>>statin>>>traffic>>>downlink" reset: false'
```
最后贴一份我的测试配置
```
{
"stats": {},
"log": {
"loglevel": "debug"
},
"api": {
"tag": "api",
"services": [
"HandlerService",
"LoggerService",
"StatsService"
]
},
"policy": {
"levels": {
"0": {
"statsUserUplink": true,
"statsUserDownlink": true
},
"1": {
"statsUserUplink": true,
"statsUserDownlink": true
}
},
"system": {
"statsInboundUplink": true,
"statsInboundDownlink": true
}
},
"inbounds": [
{
"tag": "tcp",
"port": 3307,
"protocol": "vmess",
"settings": {
"clients": [
{
"email": "a@mail",
"id": "e731f153-4f31-49d3-9e8f-ff8f396135ef",
"level": 1,
"alterId": 64
},
{
"email": "b@mail",
"id": "e731f153-4f31-49d3-9e8f-ff8f396135ee",
"level": 1,
"alterId": 64
}
]
}
},
{
"tag": "tls",
"port": 8443,
"protocol": "vmess",
"settings": {
"clients": [
{
"email": "c@mail",
"id": "e731f153-4f31-49d3-9e8f-ff8f396135ef",
"level": 1,
"alterId": 64
},
{
"email": "d@mail",
"id": "e731f153-4f31-49d3-9e8f-ff8f396135ee",
"level": 1,
"alterId": 64
}
]
},
"streamSettings": {
"network": "tcp",
"security": "tls",
"tlsSettings": {
"certificates": [
{
"certificateFile": "xxx.crt",
"keyFile": "xxx.key"
}
]
}
}
},
{
"listen": "127.0.0.1",
"port": 10085,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1"
},
"tag": "api"
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
}
],
"routing": {
"settings": {
"rules": [
{
"inboundTag": [
"api"
],
"outboundTag": "api",
"type": "field"
}
]
},
"strategy": "rules"
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment