Skip to content

Commit

Permalink
add: iptables traffic ⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Dec 16, 2018
1 parent 2e0f2c4 commit 473a29e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ V2ray多用户管理脚本,向导式管理[新增|删除|修改]传输协议

## 功能
- 一键 启动 / 停止 / 重启 V2ray 服务端
- 流量统计
- 流量统计(v2ray && iptables)
- 命令行模式管理v2ray
- 支持多用户, 多端口管理
- 开启关闭动态端口
Expand Down Expand Up @@ -127,6 +127,9 @@ source <(curl -sL https://git.io/fNgqx) --remove
**不支持Centos 6**

## 更新日志
[**2018.12.16**](https://github.com/Jrohy/multi-v2ray/tree/v2.6.0)
增加iptables流量统计

[**2018.12.3**](https://github.com/Jrohy/multi-v2ray/tree/v2.5.1)
更新策略更改, 只用最新的Release版本更新
脚本可指定版本更新(回退),支持指令操作
Expand Down
2 changes: 2 additions & 0 deletions global_setting/calcul_traffic.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
PORT=$1

[[ $# == 0 ]] && exit 1

INPUT_TRAFFIC=$(iptables -nvL INPUT -x|grep $PORT|awk '{sum += $2};END {printf("%.0f\n",sum)}')

OUTPUT_TRAFFIC=$(iptables -nvL OUTPUT -x|grep $PORT|awk '{sum += $2};END {printf("%.0f\n",sum)}')
Expand Down
16 changes: 16 additions & 0 deletions global_setting/clean_traffic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
PORT=$1

[[ $# == 0 ]] && exit 1

clean_traffic(){
local TYPE=$1
RESULT=$(iptables -nvL $TYPE --line-numbers|grep -w "$PORT"|awk '{print $1}')
echo "$RESULT" | while read LINE
do
[[ ${LINE} ]] && iptables -Z $TYPE $LINE
done
}

clean_traffic INPUT
clean_traffic OUTPUT
32 changes: 32 additions & 0 deletions global_setting/iptables_ctr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os

from loader import Loader
from utils import color_str, Color, is_number, calcul_iptables_traffic

loader = Loader()

profile = loader.profile

group_list = profile.group_list

while True:
print("{} 端口流量统计".format(color_str(Color.BLUE, "Iptables")))
print("1.查看流量统计\n")
print("2.重置流量统计\n")
print("tip: 默认全部v2端口都开启iptables的流量统计, 重启vps会全部重置!!!\n")

choice = input("请输入数字选择功能:")
if choice == "1":
for group in group_list:
print(calcul_iptables_traffic(group.port))

elif choice == "2":
port = input("请输入要重置流量的端口:")
if port and is_number(port):
os.system("bash /usr/local/multi-v2ray/global_setting/clean_traffic.sh {}".format(str(port)))
else:
print(color_str(Color.RED, "输入有误!"))
else:
break
6 changes: 5 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ def calcul_iptables_traffic(port):
traffic_result = os.popen("bash /usr/local/multi-v2ray/global_setting/calcul_traffic.sh {}".format(str(port))).readlines()
if traffic_result:
traffic_list = traffic_result[0].split()
return [bytes_2_human_readable(traffic_list[0]), bytes_2_human_readable(traffic_list[1]), bytes_2_human_readable(traffic_list[2])]
upload_traffic = bytes_2_human_readable(traffic_list[0])
download_traffic = bytes_2_human_readable(traffic_list[1])
total_traffic = bytes_2_human_readable(traffic_list[2])
return "{0}: upload:{1} download:{2} total:{3}".format(str(port),
color_str(Color.BLUE, upload_traffic), color_str(Color.BLUE, download_traffic), color_str(Color.BLUE, total_traffic))

def clean_iptables(port):
clean_cmd = "iptables -D {0} {1}"
Expand Down
25 changes: 15 additions & 10 deletions v2ray
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

SHELL_V2RAY="v2.5.4"
SHELL_V2RAY="v2.6.0"

#######color code########
RED="31m" # Error message
Expand Down Expand Up @@ -368,16 +368,17 @@ fi

if [[ ${CHOICE} == 5 ]]; then
echo -e ""
echo -e "1.流量统计\n"
echo -e "2.禁止bittorrent\n"
echo -e "3.定时更新V2ray\n"
echo -e "4.清理v2ray日志\n"
echo -e "5.脚本升级\n"
echo -e "1.流量统计(v2ray)\n"
echo -e "2.流量统计(iptables)\n"
echo -e "3.禁止bittorrent\n"
echo -e "4.定时更新V2ray\n"
echo -e "5.清理v2ray日志\n"
echo -e "6.脚本升级\n"
echo -e "请输入数字选择功能"

while :; do echo
read -n1 -p "请选择: " EXTRA_CHOICE
if [[ ! $EXTRA_CHOICE =~ ^[1-5]$ ]]; then
if [[ ! $EXTRA_CHOICE =~ ^[1-6]$ ]]; then
if [[ -z ${EXTRA_CHOICE} ]];then
bash /usr/local/bin/v2ray
exit 0
Expand All @@ -394,20 +395,24 @@ if [[ ${CHOICE} == 5 ]]; then
bash /usr/local/bin/v2ray

elif [[ ${EXTRA_CHOICE} == 2 ]]; then
python3 $APP_PATH/global_setting/iptables_ctr.py
bash /usr/local/bin/v2ray

elif [[ ${EXTRA_CHOICE} == 3 ]]; then
python3 $APP_PATH/global_setting/ban_bt.py
service v2ray restart
echo ""
bash /usr/local/bin/v2ray

elif [[ ${EXTRA_CHOICE} == 3 ]]; then
elif [[ ${EXTRA_CHOICE} == 4 ]]; then
bash $APP_PATH/global_setting/update_timer.sh
bash /usr/local/bin/v2ray

elif [[ ${EXTRA_CHOICE} == 4 ]]; then
elif [[ ${EXTRA_CHOICE} == 5 ]]; then
cleanLog
bash /usr/local/bin/v2ray

elif [[ ${EXTRA_CHOICE} == 5 ]]; then
elif [[ ${EXTRA_CHOICE} == 6 ]]; then
echo -e "脚本正在以保留配置文件形式升级, 若失败请自行手动全新安装..\n"
source <(curl -sL https://git.io/fNgqx) -k
echo -e "\n脚本升级成功!!\n"
Expand Down

0 comments on commit 473a29e

Please sign in to comment.