Revize 935fc6ad
Přidáno uživatelem Michal Kliment před více než 11 roky(ů)
freenetis-qos-sync.sh | ||
---|---|---|
#!/bin/bash
|
||
################################################################################
|
||
# #
|
||
# This script serves for QoS synchronization of IS FreenetIS #
|
||
# #
|
||
# Author Michal Kliment 2012 #
|
||
# Email kliment@freenetis.org #
|
||
# #
|
||
# name freenetis-qos-sync.sh #
|
||
# version 0.9.0 #
|
||
# #
|
||
################################################################################
|
||
|
||
#Load variables from config file
|
||
CONFIG=/etc/freenetis/freenetis-qos.conf
|
||
|
||
PATH_QOS_MEMBERS=/tmp/qos_members
|
||
PATH_QOS_IP_ADDRESSES=/tmp/qos_ip_addresses
|
||
PATH_QOS_IPSETS=/tmp/qos_ipsets
|
||
|
||
IPTABLES=/sbin/iptables
|
||
|
||
LOG_PREFIX=`date "+%Y-%m-%d %H:%M"`" QoS: "
|
||
|
||
ROOT="1:"
|
||
|
||
#Load variables
|
||
if [ -f ${CONFIG} ]; then
|
||
. $CONFIG;
|
||
else
|
||
echo "Config file is missing at path $CONFIG."
|
||
echo "Terminating..."
|
||
exit 0
|
||
fi
|
||
|
||
stop ()
|
||
{
|
||
# for each current ipsets, list is stored in file
|
||
cat $PATH_QOS_IPSETS | while read line
|
||
do
|
||
ID=`echo $line | awk '{print $1}'`
|
||
IPSET=`echo $line | awk '{print $2}'`
|
||
|
||
# flush ipset
|
||
ipset -F $IPSET
|
||
echo $LOG_PREFIX"Emptied ipset $IPSET";
|
||
|
||
# remove its iptables rules
|
||
#$IPTABLES -t mangle -D POSTROUTING -o $OUTPUT_INTERFACE -m set --set $IPSET src -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -D POSTROUTING -m set --set $IPSET src -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -D POSTROUTING -m set --set $IPSET src -j RETURN
|
||
echo $LOG_PREFIX"Deleted iptables rule for assignment upload tc class $ROOT$ID to ipset $IPSET"
|
||
|
||
#$IPTABLES -t mangle -D POSTROUTING -o $INPUT_INTERFACE -m set --set $IPSET dst -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -D POSTROUTING -m set --set $IPSET dst -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -D POSTROUTING -m set --set $IPSET dst -j RETURN
|
||
echo $LOG_PREFIX"Deleted iptables rule for assignment download tc class $ROOT$ID to ipset $IPSET"
|
||
|
||
# remove ipset
|
||
ipset -X $IPSET
|
||
echo $LOG_PREFIX"Removed ipset $IPSET"
|
||
done
|
||
|
||
# clear file with ipset list
|
||
cat /dev/null > $PATH_QOS_IPSETS
|
||
|
||
echo $LOG_PREFIX"Deleting old tc classes"
|
||
|
||
# deletes all old qdiscs, its remove all children classes, qdisc, etc.
|
||
tc qdisc del dev $OUTPUT_INTERFACE root 2> /dev/null
|
||
tc qdisc del dev $INPUT_INTERFACE root 2> /dev/null
|
||
}
|
||
|
||
start ()
|
||
{
|
||
echo $LOG_PREFIX"Downloading data"
|
||
|
||
wget -q -O $PATH_QOS_MEMBERS $SET_URL_QOS_MEMBERS --no-check-certificate
|
||
wget -q -O $PATH_QOS_IP_ADDRESSES $SET_URL_QOS_IP_ADDRESSES --no-check-certificate
|
||
|
||
# creates default qdiscs (first for upload, second for download)
|
||
tc qdisc add dev $OUTPUT_INTERFACE root handle $ROOT htb default 2
|
||
echo $LOG_PREFIX"Added root tc qdisc for upload"
|
||
|
||
tc qdisc add dev $INPUT_INTERFACE root handle $ROOT htb default 2
|
||
echo $LOG_PREFIX"Added root tc qdisc for download"
|
||
|
||
# line number counter
|
||
LNR=1
|
||
|
||
cat $PATH_QOS_MEMBERS | while read line
|
||
do
|
||
ID=`echo $line | awk '{print $1}'`
|
||
|
||
UPLOAD_CEIL=`echo $line | awk '{print $2}'`
|
||
DOWNLOAD_CEIL=`echo $line | awk '{print $3}'`
|
||
|
||
UPLOAD_RATE=`echo $line | awk '{print $4}'`
|
||
DOWNLOAD_RATE=`echo $line | awk '{print $5}'`
|
||
|
||
PRIORITY=`echo $line | awk '{print $6}'`
|
||
|
||
PROTOCOL=`echo $line | awk '{print $7}'`
|
||
|
||
PARENT=`echo $line | awk '{print $8}'`
|
||
|
||
IPSET=`echo $line | awk '{print $9}'`
|
||
|
||
if [ "$UPLOAD_CEIL" != "0M" ]; then
|
||
UPLOAD_CEIL=" ceil "$UPLOAD_CEIL"bit"
|
||
else
|
||
UPLOAD_CEIL=""
|
||
fi
|
||
|
||
if [ "$UPLOAD_RATE" != "0M" ]; then
|
||
UPLOAD_RATE=" rate "$UPLOAD_RATE"bit"
|
||
else
|
||
UPLOAD_RATE=""
|
||
fi
|
||
|
||
if [ "$DOWNLOAD_CEIL" != "0M" ]; then
|
||
DOWNLOAD_CEIL=" ceil "$DOWNLOAD_CEIL"bit"
|
||
else
|
||
DOWNLOAD_CEIL=""
|
||
fi
|
||
|
||
if [ "$DOWNLOAD_RATE" != "0M" ]; then
|
||
DOWNLOAD_RATE=" rate "$DOWNLOAD_RATE"bit"
|
||
else
|
||
DOWNLOAD_RATE=""
|
||
fi
|
||
|
||
# creates classes (first for upload, second for download)
|
||
tc class add dev $OUTPUT_INTERFACE parent $ROOT$PARENT classid $ROOT$ID htb $UPLOAD_RATE $UPLOAD_CEIL
|
||
echo $LOG_PREFIX"Created tc class $ROOT$ID for upload"
|
||
|
||
tc class add dev $INPUT_INTERFACE parent $ROOT$PARENT classid $ROOT$ID htb $DOWNLOAD_RATE $DOWNLOAD_CEIL
|
||
echo $LOG_PREFIX"Created tc class $ROOT$ID for download"
|
||
|
||
if [ "$LNR" -gt 1 ]; then
|
||
|
||
tc qdisc add dev $OUTPUT_INTERFACE parent $ROOT$ID handle $ID: sfq
|
||
echo $LOG_PREFIX"Created tc qdisc for upload tc class $ROOT$ID"
|
||
|
||
tc qdisc add dev $INPUT_INTERFACE parent $ROOT$ID handle $ID: sfq
|
||
echo $LOG_PREFIX"Created tc qdisc for download tc class $ROOT$ID"
|
||
|
||
tc filter add dev $OUTPUT_INTERFACE parent $ID: prio $PRIORITY handle $ID protocol $PROTOCOL flow hash keys nfct-src divisor 1024
|
||
echo $LOG_PREFIX"Created filter for upload tc class $ROOT$ID with priority $PRIORITY and protocol $PROTOCOL"
|
||
|
||
tc filter add dev $INPUT_INTERFACE parent $ID: prio $PRIORITY handle $ID protocol $PROTOCOL flow hash keys dst divisor 1024
|
||
echo $LOG_PREFIX"Created filter for download tc class $ROOT$ID with priority $PRIORITY and protocol $PROTOCOL"
|
||
|
||
fi
|
||
|
||
if [ "$IPSET" != "" ]; then
|
||
|
||
ipset -N $IPSET iphash --hashsize 10000 --probes 8 --resize 50
|
||
echo $LOG_PREFIX"Created ipset $IPSET for tc class $ROOT$ID"
|
||
|
||
#$IPTABLES -t mangle -A POSTROUTING -o $OUTPUT_INTERFACE -m set --set $IPSET src -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -A POSTROUTING -m set --set $IPSET src -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -A POSTROUTING -m set --set $IPSET src -j RETURN
|
||
echo $LOG_PREFIX"Added iptables rule for assignment upload tc class $ROOT$ID to ipset $IPSET"
|
||
|
||
#$IPTABLES -t mangle -A POSTROUTING -o $INPUT_INTERFACE -m set --set $IPSET dst -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -A POSTROUTING -m set --set $IPSET dst -j CLASSIFY --set-class $ROOT$ID
|
||
$IPTABLES -t mangle -A POSTROUTING -m set --set $IPSET dst -j RETURN
|
||
echo $LOG_PREFIX"Added iptables rule for assignment download tc class $ROOT$ID to ipset $IPSET"
|
||
|
||
awk '{ if ($1=='$ID') print $2 }' $PATH_QOS_IP_ADDRESSES | while read IP_ADDRESS
|
||
do
|
||
ipset -A $IPSET $IP_ADDRESS
|
||
echo $LOG_PREFIX"Added ip address $IP_ADDRESS to ipset $IPSET"
|
||
done
|
||
|
||
echo "$ID $IPSET" >> $PATH_QOS_IPSETS
|
||
fi
|
||
|
||
LNR=$(($LNR+1))
|
||
done
|
||
|
||
echo $LOG_PREFIX"Sleeping"
|
||
sleep $DELAY
|
||
}
|
||
|
||
update()
|
||
{
|
||
stop
|
||
start
|
||
}
|
||
|
||
case "$1" in
|
||
update)
|
||
while (true);
|
||
do
|
||
update
|
||
done
|
||
;;
|
||
stop)
|
||
stop
|
||
;;
|
||
esac
|
freenetis-qos.conf | ||
---|---|---|
################################################################################
|
||
# #
|
||
# This script serves for IS FreenetIS (redirection and QoS) #
|
||
# #
|
||
# Author Michal Kliment 2012 #
|
||
# Email kliment@freenetis.org #
|
||
# #
|
||
# name freenetis-qos.conf #
|
||
# version 0.9.0 #
|
||
# #
|
||
################################################################################
|
||
|
||
################################################################################
|
||
# G E N E R A L S E T T I N G S #
|
||
################################################################################
|
||
|
||
# Base PATH_FN to running FreenetIS instance
|
||
PATH_FN=http://localhost/freenetis
|
||
|
||
# Log file, change to /dev/null to disable logging
|
||
LOG_FILE=/var/log/freenetis-qos.log
|
||
|
||
# Input interface on which redirection rules and QoS download are applicated on
|
||
INPUT_INTERFACE=eth0
|
||
|
||
# Output interface on which QoS upload is applicated on
|
||
OUTPUT_INTERFACE=eth0
|
||
|
||
# Delay in seconds between next update cycle
|
||
DELAY=60
|
||
|
||
# URL of pages which we need to download from FreenetIS. [DO NOT CHANGE THIS VALUES!!]
|
||
SET_URL_QOS_MEMBERS=$PATH_FN/index.php/en/web_interface/members_qos_ceil_rate
|
||
SET_URL_QOS_IP_ADDRESSES=$PATH_FN/index.php/en/web_interface/ip_addresses_qos_ceil_rate
|
freenetis-qos.init.sh | ||
---|---|---|
#! /bin/bash
|
||
|
||
### BEGIN INIT INFO
|
||
# Provides: freenetis-qos
|
||
# Required-Start: $remote_fs
|
||
# Required-Stop: $remote_fs
|
||
# Should-Start: $network $syslog
|
||
# Should-Stop: $network $syslog
|
||
# Default-Start: 2 3 4 5
|
||
# Default-Stop: 0 1 6
|
||
# Short-Description: Start and stop freenetis QoS daemon
|
||
# Description: FreenetIS initialization QoS synchronization script.
|
||
### END INIT INFO
|
||
|
||
################################################################################
|
||
# #
|
||
# This script serves for initialization of QoS of IS FreenetIS #
|
||
# #
|
||
# Author Michal Kliment 2012 #
|
||
# Email kliment@freenetis.org #
|
||
# #
|
||
# Name freenetis-qos.init.sh #
|
||
# Version 0.9.0 #
|
||
# #
|
||
################################################################################
|
||
|
||
#Local variable contains path to iptables - mandatory
|
||
IPTABLES=/sbin/iptables
|
||
|
||
#Load variables from config file
|
||
CONFIG=/etc/freenetis/freenetis-qos.conf
|
||
|
||
# Path to QoS synchronization file
|
||
QOS_SYNCFILE=/usr/sbin/freenetis-qos-sync
|
||
|
||
#Path to QoS pid file
|
||
QOS_PIDFILE=/var/run/freenetis-qos-sync.pid
|
||
|
||
#Load variables
|
||
if [ -f ${CONFIG} ]; then
|
||
. $CONFIG;
|
||
else
|
||
echo "Config file is missing at path $CONFIG."
|
||
echo "Terminating..."
|
||
exit 0
|
||
fi
|
||
|
||
start_qos ()
|
||
{
|
||
cat /dev/null > "$LOG_FILE"
|
||
|
||
if [ -f ${QOS_PIDFILE} ]; then
|
||
echo "Already started"
|
||
return 0
|
||
fi
|
||
|
||
echo -n "Starting FreenetIS QoS deamon: "
|
||
nohup $QOS_SYNCFILE update >> "$LOG_FILE" 2>&1 &
|
||
|
||
#Parse PID a save to file
|
||
ps aux | grep $QOS_SYNCFILE | grep -v grep | awk '{print $2}' > $QOS_PIDFILE
|
||
|
||
# test if daemon is started
|
||
if [ `ps aux | grep $QOS_SYNCFILE | grep -v grep | wc -l` -gt 0 ];
|
||
then
|
||
echo "OK"
|
||
else
|
||
echo "FAILED!"
|
||
fi
|
||
|
||
return 0
|
||
}
|
||
|
||
stop_qos ()
|
||
{
|
||
if [ ! -f ${QOS_PIDFILE} ]; then
|
||
echo "Already stopped"
|
||
return 0
|
||
fi
|
||
|
||
#Killing of process by sigterm
|
||
echo -n "Stopping FreenetIS QoS deamon: "
|
||
kill -9 `cat $QOS_PIDFILE`
|
||
|
||
rm -f $QOS_PIDFILE
|
||
|
||
$QOS_SYNCFILE stop >> "$LOG_FILE" 2>&1
|
||
|
||
# test if daemon is stopped
|
||
if [ `ps aux | grep $QOS_SYNCFILE | grep -v grep | wc -l` -eq 0 ];
|
||
then
|
||
echo "OK"
|
||
else
|
||
echo "FAILED!";
|
||
fi
|
||
|
||
return 0
|
||
}
|
||
|
||
status_qos ()
|
||
{
|
||
if [ -f ${QOS_PIDFILE} ]; then
|
||
echo "Freenetis QoS is running with PID `cat $QOS_PIDFILE`"
|
||
return 0
|
||
else
|
||
echo "Freenetis QoS is not running"
|
||
return 0
|
||
fi
|
||
}
|
||
|
||
usage_qos ()
|
||
{
|
||
echo "usage : `echo $0` (start|stop|restart|status|help)"
|
||
}
|
||
|
||
help_qos ()
|
||
{
|
||
echo " start - initialization of firewall rules and settings for QoS"
|
||
echo " stop - clears firewall rules and settings for QoS"
|
||
echo " restart - restarts firewall rules and settings for QoS"
|
||
echo " status - returns actual status of QoS"
|
||
echo " help - prints help for QoS"
|
||
}
|
||
|
||
# Is parameter #1 zero length?
|
||
if [ -z "$1" ]; then
|
||
usage_qos
|
||
exit 0
|
||
fi;
|
||
|
||
case "$1" in
|
||
|
||
start)
|
||
start_qos
|
||
exit 0
|
||
;;
|
||
|
||
restart)
|
||
stop_qos
|
||
start_qos
|
||
exit 0
|
||
;;
|
||
|
||
stop)
|
||
stop_qos
|
||
exit 0
|
||
;;
|
||
|
||
status)
|
||
status_qos
|
||
exit 0
|
||
;;
|
||
|
||
help)
|
||
usage_qos
|
||
help_qos
|
||
exit 0
|
||
;;
|
||
|
||
*)
|
||
usage_qos
|
||
exit 0
|
||
;;
|
||
|
||
esac
|
||
|
||
exit 0
|
Také k dispozici: Unified diff
Added files from Freenetis repo.