知识点:shell脚本,TC应用,iptables应用
#!/bin/sh #copyright by zhoutao0712 echo -e '\n export UIP="192.168.1."\n export NET="192.168.1.0/24"\n export UP=40\n export DOWN=170\n export UPLOADR=2\n export UPLOADC=12\n export ONLINE=$(cat /proc/net/ipt_hashlimit/game_burst |wc -l) export DOWNLOADR=15\n export DOWNLOADC=$(($DOWN*((30+ONLINE))/((20+8*ONLINE))))\n' >/tmp/regist_env.sh cat /proc/net/ipt_hashlimit/newc >/tmp/old cat /tmp/old |wc -l >/tmp/N_old . /tmp/regist_env.sh insmod imq insmod ipt_IMQ insmod ipt_length.o insmod ipt_hashlimit.o ifconfig imq1 up ifconfig imq0 up iptables -t mangle -N QOSDOWN iptables -t mangle -N QOSUP iptables -t mangle -I FORWARD -s $NET -j QOSUP iptables -t mangle -I FORWARD -d $NET -j QOSDOWN iptables -t mangle -A QOSDOWN -j IMQ --todev 0 iptables -t mangle -A QOSUP -j IMQ --todev 1 //DNS不做限制 iptables -t mangle -I QOSDOWN -p udp --sport 53 -j RETURN iptables -t mangle -I QOSUP -p udp --dport 53 -j RETURN iptables -t mangle -N GAME_BURST //判定大于128的数据包为游戏包,并放入GAME_BURST链 iptables -t mangle -A QOSDOWN -m length --length 128: -j GAME_BURST //游戏包以目的地址为准进行流量××× iptables -t mangle -A GAME_BURST -m hashlimit --hashlimit 8/sec --hashlimit-burst 100 --hashlimit-mode dstip \ --hashlimit-name game_burst --hashlimit-htable-gcinterval 5000 --hashlimit-htable-expire 150000 -j RETURN iptables -t mangle -A GAME_BURST -m recent --rdest --name game_burst --set //对http https 数据标记为255 iptables -t mangle -A QOSDOWN -p tcp -m mport --sports 80,443 -j BCOUNT iptables -t mangle -A QOSDOWN -p tcp -m mport --sports 80,443 -m bcount --range :102400 -j MARK --set-mark-return 255 iptables -t mangle -A QOSUP -p tcp -m mport --dports 80,443 -j BCOUNT iptables -t mangle -A QOSUP -p tcp -m mport --dports 80,443 -m bcount --range :102400 -j MARK --set-mark-return 255 //标记游戏数据包为254 iptables -t mangle -A QOSDOWN -m recent --rdest --name game_burst ! --rcheck --seconds 8 -j MARK --set-mark-return 254 iptables -t mangle -A QOSDOWN -j MARK --ipaddr 1 iptables -t mangle -A QOSUP -j MARK --ipaddr 0 tc qdisc del dev imq0 root tc qdisc del dev imq1 root echo -e '\n tc qdisc add dev imq0 root handle 1: htb\n tc qdisc add dev imq1 root handle 1: htb\n tc qdisc add dev imq0 root handle 1: htb\n tc qdisc add dev imq1 root handle 1: htb\n tc class add dev imq1 parent 1: classid 1:1 htb rate $((UP))kbps\n tc class add dev imq0 parent 1: classid 1:1 htb rate $((DOWN))kbps\n //对页面访问进行适当限制 tc class add dev imq0 parent 1:1 classid 1:255 htb rate $((DOWN*2/10))kbps ceil $((DOWN*8/10))kbps quantum 6000 prio 3\n tc filter add dev imq0 parent 1:0 protocol ip prio 5 handle 255 fw flowid 1:255\n tc class add dev imq1 parent 1:1 classid 1:255 htb rate $((UP*2/10))kbps ceil $((UP*5/10))kbps quantum 2000 prio 3\n tc filter add dev imq1 parent 1:0 protocol ip prio 5 handle 255 fw flowid 1:255\n //保障游戏的最低带宽 tc class add dev imq0 parent 1:1 classid 1:254 htb rate $((DOWN*6/10))kbps prio 0\n tc filter add dev imq0 parent 1:0 protocol ip prio 2 handle 254 fw flowid 1:254\n tc qdisc add dev imq0 parent 1:254 handle 254: bfifo limit 10kb\n' >/tmp/pref_qos . /tmp/pref_qos iptables -t mangle -N CONNLMT iptables -t mangle -N NEWLMT iptables -t mangle -I FORWARD -m state --state NEW -s $NET -p ! icmp -j CONNLMT //对内网发起的连接给予限制 iptables -t mangle -A CONNLMT -j NEWLMT //×××流量,对其他数据包进行限速 iptables -t mangle -A NEWLMT -m hashlimit --hashlimit-name newconn --hashlimit 15/sec \ --hashlimit-mode srcip --hashlimit-htable-gcinterval 5000 --hashlimit-htable-expire 20000 -j RETURN iptables -t mangle -A NEWLMT -j DROP //排除DNS HTTP等包的限制 iptables -t mangle -A CONNLMT -p tcp -m mport --dports 20:23,25,80,110,443 -j RETURN iptables -t mangle -A CONNLMT -p udp -m mport --dports 53,4000:4050 -j RETURN //限制连接数量 iptables -t mangle -A CONNLMT -p tcp -m connlimit --connlimit-above 150 -j DROP iptables -t mangle -A CONNLMT -p ! tcp -m connlimit --connlimit-above 120 -j DROP echo -e '\n i=1\n while [ $i -le $N_del ]\n do\n ip_i=$(sed -n ${i}p /tmp/del_list)\n handle_i=$(echo $ip_i|cut -d'.' -f4)\n tc filter del dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n tc class del dev imq0 parent 1:1 classid 1:$handle_i\n tc filter del dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n tc class del dev imq1 parent 1:1 classid 1:$handle_i\n i=`expr $i + 1`\n done\n' >/tmp/del_qos echo -e '\n i=1\n while [ $i -le $N_add ]\n do\n ip_i=$(sed -n ${i}p /tmp/add_list)\n handle_i=$(echo $ip_i|cut -d'.' -f4)\n //单IP下载限速 tc class add dev imq0 parent 1:1 classid 1:$handle_i htb rate $((DOWNLOADR))kbps ceil $((DOWNLOADC))kbps quantum 2000 prio 5\n tc qdisc add dev imq0 parent 1:$handle_i handle $handle_i sfq perturb 15\n //过滤器对每个IP进行分类 tc filter add dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n //同上 tc class add dev imq1 parent 1:1 classid 1:$handle_i htb rate $((UPLOADR))kbps ceil $((UPLOADC))kbps quantum 1500 prio 5\n tc qdisc add dev imq1 parent 1:$handle_i handle $handle_i bfifo limit 6kb\n tc filter add dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n i=`expr $i + 1`\n done\n' >/tmp/add_qos echo -e '\n i=1\n while [ $i -le $N_new ]\n do\n ip_i=$(sed -n ${i}p /tmp/new)\n handle_i=$(echo $ip_i|cut -d'.' -f4)\n tc class replace dev imq0 parent 1:1 classid 1:$handle_i htb rate $((DOWNLOADR))kbps ceil $((DOWNLOADC))kbps quantum 2000 prio 5\n tc qdisc replace dev imq0 parent 1:$handle_i handle $handle_i sfq perturb 15\n tc filter replace dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n tc class replace dev imq1 parent 1:1 classid 1:$handle_i htb rate $((UPLOADR))kbps ceil $((UPLOADC))kbps quantum 1500 prio 5\n tc qdisc replace dev imq1 parent 1:$handle_i handle $handle_i bfifo limit 6kb\n tc filter replace dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n i=`expr $i + 1`\n done\n' >/tmp/replace_qos
代码出处:http://bbs.dualwan.cn/thread-20122-1-1.html
openwrt 改良版:
转载于:https://blog.51cto.com/wangxuhello/1395377