为V2Ray配置添加用户流量统计功能
2020-08-24     loonlog     29064     6
本文目录
前面的一篇文章介绍了为V2Ray建立多用户,一般可以建立一个端口下面的多个用户,也可以建立多个端口,多个用户,那我们既然创建了多用户,那就是想能不能管理一下这些用户呢,比如监控一下用户的流量,毕竟可以看看哪个家伙用的流量比较多,这家伙肯定看了很多小姐姐的视频,需要时不时的提醒他注意身体健康!
我也是刚刚跑通这个功能,之前想过弄,但是官网资料看了很多没有头绪,还是一个网友留言给了些提示,顺着思路又看了下GitHub上面的讨论,仿照着弄成功了,现在就来分享一下:
StatsService API获得数据统计功能,配置里面必须满足以下条件:
1. "stats":{}对象的存在,当此项存在时,开启统计信息;
2. "api"配置对象里面有`StatsService` ;
3. "policy"中的统计开关为true,除了各个用户的统计,还有全局统计 ;
4. clients里面要有email ,email用来区分用户;
5. 有个专用的`dokodemo-door`协议的入口,tag为api ;
6. routing里面要有inboundTag:api和 outboundTag:api的规则 。
查看流量统计的指令
查询全局流量
v2ctl api --server=127.0.0.1:10085 StatsService.QueryStats 'pattern: "" reset: false'
定向查询用户的上传和下载流量
v2ctl api --server=127.0.0.1:10085 StatsService.GetStats 'name: "user>>>abc@loonlog.com>>>traffic>>>downlink" reset: false' v2ctl api --server=127.0.0.1:10085 StatsService.GetStats 'name: "user>>>abc@loonlog.com>>>traffic>>>uplink" reset: false'
下面这代码是我们没有做流量统计的全部代码:
(悠闲的使用了一年半时间,就没有折腾;比较简单,比较基础,高级的东西,慢慢学习)
{
"inbounds": [{
"port": 10808,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "floonlog2-a099-4ba3-b06c-3loonlogcom7",
"level": 1,
"alterId": 64
}
]
}
},
{
"port": 10809,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "floonlog1-a099-4ba3-b06c-8loonlogcom7",
"level": 1,
"alterId": 64
}
]
}
}],
"outbounds": [{
"protocol": "freedom",
"settings": {}
},{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}],
"routing": {
"rules": [
{
"type": "field",
"ip": ["geoip:private"],
"outboundTag": "blocked"
}
]
}
}
下面这段代码是加入了流量统计的配置内容,请大家和上面的代码进行对比,结合文章前面介绍的配置必须满足的6条关键点,对比理解吧:
{
"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": "the1st",
"port": 10808,
"protocol": "vmess",
"settings": {
"clients": [
{
"email": "abc@loonlog.com",
"id": "floonlog2-a099-4ba3-b06c-3loonlogcom7",
"level": 1,
"alterId": 64
}
]
}
},
{
"tag": "the2st",
"port": 10809,
"protocol": "vmess",
"settings": {
"clients": [
{
"email": "123@loonlog.com",
"id": "floonlog1-a099-4ba3-b06c-8loonlogcom7",
"level": 1,
"alterId": 64
}
]
}
},
{
"listen": "127.0.0.1",
"port": 10085,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1"
},
"tag": "api"
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
},
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}
],
"routing": {
"settings": {
"rules": [
{
"inboundTag": [
"api"
],
"outboundTag": "api",
"type": "field"
}
]
},
"strategy": "rules"
}
}
完事,最好是输入指令cat /etc/v2ray/config.json 再看看对不对,检查一下,然后重启V2Ray服务,
systemctl restart v2ray
我们上面建立了一个传送门dokodemo-door协议的入口10085,别忘记开启防火墙端口,不然会报错:context deadline exceeded
firewall-cmd --zone=public --add-port=10085/tcp --permanent
重载防火墙配置
firewall-cmd --reload
当我们利用ssh登陆服务器,用指令查询统计信息时,遇到提示“-bash: v2ctl: command not found”,我们就吧指令前面的v2ctl添加上路径信息/usr/bin/v2ray/v2ctl
完整指令信息如下:
查询全局流量
/usr/bin/v2ray/v2ctl api --server=127.0.0.1:10085 StatsService.QueryStats 'pattern: "" reset: false'
定向查询用户的上传和下载流量
/usr/bin/v2ray/v2ctl api --server=127.0.0.1:10085 StatsService.GetStats 'name: "user>>>abc@loonlog.com>>>traffic>>>downlink" reset: false' /usr/bin/v2ray/v2ctl api --server=127.0.0.1:10085 StatsService.GetStats 'name: "user>>>abc@loonlog.com>>>traffic>>>uplink" reset: false'
大家要时刻关注下方的留言评论内容,因为有很多热心的网友!
参考信息:
1、https://gist.github.com/boypt/80d9ecaaa7f3c799c525e91f3c0b35d1
2、https://github.com/v2ray/discussion/issues/17
3、热心网友的留言
http://loonlog.com/2020/8/24/v2ray-traffic-statistics/
评论列表,共 6 条评论
回复
我来添加一个反馈,如果routing配置中存在"127.0.0.0/8"的blocked规则,也会导致报错context deadline exceeded
更多的导致context deadline exceeded的问题可以看github讨论区:https://gist.github.com/eycorsican/aa8cdc1d39c3fa355c499f89a15b9753
回复
另:这个问题卡了我一个小时……真是太傻了hhh
回复
回复
楼主你好,想请教下dokodemo-door配置的端口10085是默认都用这个吗?我看官方示例里用的是10085但是v2ctl api -h 里用的范例都是8080,请问下这个怎么理解,谢谢
回复
用10085,8080我认为比较特殊,尽量避开使用这个端口
回复
看我看我看我,我就是那名网友。哈哈哈