It is very simple to implement through iptables, such as your local shadowsocks address 192.168.0.4 and Port 20091
Port 20091 export traffic statistics command: sudo iptables-I OUTPUT-s 192.168.0.4-p tcp-sport 20091
Then, check the traffic statistics regularly to view the traffic statistics:
Sudo iptables-n-v-L-t filter
Obtain the specific traffic value: sudo iptables-n-v-L-t filter | grep-I 'spt: 000000' | awk-f''' {print $2 }'
Then write a crontab script to read the traffic statistics every minute. If the traffic exceeds the predefined value, the shadowsocks service is stopped.
Of course, you may open multiple shadowsocks services and need to save their process id. If the traffic exceeds the limit, kill this id.
Shadowsocks process startup script:
| The code is as follows: |
Copy code |
# Start the service and record the process id to the file PORT = 20091 PID_FILE = ~ /Shadow _ $ PORT. pid # Enable corresponding port traffic statistics Sudo iptables-I OUTPUT-s 192.168.0.4-p tcp-sport $ PORT Ss-server-c shadowsocks. json-p $ PORT> logshadow. log 2> & 1 & Echo "$! "> $ PID_FILE |
========================================================== ====================
Traffic monitoring script:
| The code is as follows: |
Copy code |
# When crontab detects traffic exceeding the limit, the service is stopped. For example, the daily traffic limit is 100 MB. PORT = 20091 PID_FILE = ~ /Shadow _ $ PORT. pid MAX = 100 Value_string = 'sudo iptables-n-v-L-t filter | grep-I 'spt: 000000' | awk-f'' {print $2 }'' Grep 'M' $ value_string If [$? = 0]; then Value = 'echo $ value_string | tr-d 'M'' If [$ value-gt $ MAX]; then Kill-9 'cat $ PID_FILE' Rm $ PID_FILE Fi Fi |