From a5da3c8fe021b038860317b6a05a31c37a7dc3f2 Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Tue, 25 Nov 2025 15:05:12 +0100 Subject: [PATCH] add traffic shaping script --- trafficshaping.sh | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100755 trafficshaping.sh diff --git a/trafficshaping.sh b/trafficshaping.sh new file mode 100755 index 0000000..3c275ae --- /dev/null +++ b/trafficshaping.sh @@ -0,0 +1,104 @@ +#!/bin/sh + +LOG=/data/ulvpn/$(basename $0 .sh).log +[ -t 1 -o "$DAEMON" ] || { export DAEMON=yes; $0 $1 &>$LOG; exit 0; } + +# Name of the traffic control command. +#TC=/sbin/tc +#TC=/data/data/com.termux/files/usr/bin/tc +TC=/bin/tc + +# The network interface we're planning on limiting bandwidth. + +IF=tun0 # Network card interface + +# Download limit (in mega bits) +DNLD=90kbit # DOWNLOAD Limit +#DNLD=2mbit # DOWNLOAD Limit + +# Upload limit (in mega bits) +#UPLD=2.5mbit # UPLOAD Limit +UPLD=90kbit # UPLOAD Limit + +# IP address range of the machine we are controlling +IP=10.2.1.1 # Host IP + +# Filter options for limiting the intended interface. +U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32" + +start() { + + # Hierarchical Token Bucket (HTB) to shape bandwidth + + $TC qdisc add dev $IF root handle 1: htb default 30 #Creates the root schedlar + $TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD #Creates a child schedlar to shape download + $TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD #Creates a child schedlar to shape upload + $U32 match ip dst $IP/16 flowid 1:1 #Filter to match the interface, limit download speed + $U32 match ip src $IP/16 flowid 1:2 #Filter to match the interface, limit upload speed +} + + +stop() { + + # Stop the bandwidth shaping. + $TC qdisc del dev $IF root + +} + +restart() { + + # Self-explanatory. + stop + sleep 1 + start + +} + +show() { + + # Display status of traffic control status. + $TC -s qdisc ls dev $IF + +} + +case "$1" in + + start) + + ip addr show dev $IF + echo -n "Starting bandwidth shaping: " + start + echo "done" + $TC qdisc + ;; + + stop) + + echo -n "Stopping bandwidth shaping: " + stop + echo "done" + ;; + + restart) + + echo -n "Restarting bandwidth shaping: " + restart + echo "done" + ;; + + show) + + echo "Bandwidth shaping status for $IF:" + show + echo "" + ;; + + *) + + pwd=$(pwd) + echo "Usage: $(basename $0) {start|stop|restart|show}" + ;; + +esac + +exit 0 -- 2.30.2