#!/bin/sh # # Pen proxy wrapper, periodic check for best connections available. # # Stichting Wireless Leiden # # Rick van der Zwet # BIND_ADDR=${1:-172.31.255.1} BIND_PORT=${2:-3128} # Internal parameters, don't touch unless you know what you are doing. TEST_URL="http://tinyproxy.stats/" TEST_INTERVAL=`expr 15 \* 60` PIDFILE='/var/run/pen.pid' PEN='/usr/local/bin/pen' PEN_FLAGS="-S 20 -b 30 -p ${PIDFILE} ${BIND_ADDR}:${BIND_PORT}" TAG=`basename $0` logit() { logger -t "$TAG" $* } get_proxy_list() { # Get (updated) proxy listing from configuration files. . /etc/rc.subr load_rc_config "pen_wrapper" make_list "$list_normal_proxies" " " } # Return speed value, higher is better test_proxy() { PROXY=$1 retstr=`HTTP_PROXY=http://$PROXY fetch -T 3 -o /dev/null ${TEST_URL} 2>&1` bps=`echo "${retstr}" | awk '/Bps/ {printf $4}'` echo ${bps:-"0"} } # Sort proxy list on highest bandwidth test_proxies() { result='' for host in $*; do bps=`test_proxy $host:3128` if [ "$bps" != "0" ]; then result="$result $bps:$host:3128" fi done echo $result | xargs -n1 | sort -t':' -k1 -n -r | cut -d: -f 2,3 | xargs } ## # Main loop LIVE_PROXY_LIST='' while true; do PROXY_LIST=`get_proxy_list` if [ -z "$PROXY_LIST" ]; then logit "Not starting: list_normal_proxies variable not configured" else NEW_PROXY_LIST=`test_proxies $PROXY_LIST` if [ "${LIVE_PROXY_LIST}" != "${NEW_PROXY_LIST}" ]; then logit "INFO: New listing to be configured '${NEW_PROXY_LIST}'" # Pen should only be started if alias exists ifconfig | grep -q ${BIND_ADDR} || { logit "Not starting: alias $BIND_ADDR not configured!" } && { [ -r ${PIDFILE} ] && kill `cat ${PIDFILE}` ${PEN} ${PEN_FLAGS} ${NEW_PROXY_LIST} LIVE_PROXY_LIST="${NEW_PROXY_LIST}" } fi fi sleep ${TEST_INTERVAL} done