Method for controlling SHADOWSOCKS traffic detection in ubuntu

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.