Changeset 10484 in hybrid for branches/releng-9.0/nanobsd


Ignore:
Timestamp:
Apr 13, 2012, 8:00:32 PM (13 years ago)
Author:
rick
Message:

New pen wrapper, result of scripting an script 3 years later.

Now using dynamic listings and way more clever sorting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/releng-9.0/nanobsd/files/usr/local/bin/pen_wrapper

    r10136 r10484  
    11#!/bin/sh
    2 # Pen proxy wrapper, periodic check for best connections
     2#
     3# Pen proxy wrapper, periodic check for best connections available.
     4#
    35# Stichting Wireless Leiden
     6#
    47# Rick van der Zwet <rick@wirelessleiden.nl>
     8#
    59
    6 BIND_ADDR=${1-172.31.255.1}
    7 BIND_PORT=${2-3128}
    8 DEBUG=0
    9 
    10 #XXX: Really static list, some dynamic alternative prefered
    11 PROXY_LIST="${3-172.17.8.68:3128 172.17.143.4:3128 172.20.128.98:3128 172.16.2.254:3128 172.19.168.66:3128}"
     10BIND_ADDR=${1:-172.31.255.1:3128}
    1211
    1312
    14 TEST_URL="http://www.ams-ix.net/"
    15 TEST_INTERVAL=`expr 30 \* 60` # Back-off period in seconds, re-testing period
     13# Internal parameters, don't touch unless you know what you are doing.
     14TEST_URL="http://tinyproxy.stats/"
     15TEST_INTERVAL=`expr 15 \* 60`
     16PIDFILE='/var/run/pen.pid'
     17PEN='/usr/local/bin/pen'
     18PEN_FLAGS="-S 20 -b 30 -p ${PIDFILE} ${BIND_ADDR}:${BIND_PORT}"
     19
     20TAG=`basename $0`
     21logit() {
     22  logger -t "$TAG" $*
     23}
     24
     25update_proxy_list() {
     26  # Get (updated) proxy listing from configuration files.
     27  . /etc/rc.subr
     28  echo "$list_normal_proxies"
     29}
    1630
    1731
    18 # Don't touch, unless you know what you are doing
    19 PIDFILE='/var/run/pen.pid'
    20 PEN='/usr/local/bin/pen'
    21 PEN_FLAGS="-b 30 -r -p ${PIDFILE} -o prio ${BIND_ADDR}:${BIND_PORT}"
    22 
    23 LOGFILE='/var/log/pen_wrapper.log'
    24 
    25 log()
    26 {
    27         _datestamp=`date "+%Y-%m-%d %H:%M:%S"`
    28         _msg="[${_datestamp}] $*"
    29         if [ ${DEBUG} -eq 0 ]; then
    30                 echo "${_msg}" >> ${LOGFILE}
    31         else
    32                 echo "${_msg}"
    33         fi
     32# Return speed value, higher is better
     33test_proxy() {
     34  PROXY=$1
     35  retstr=`HTTP_PROXY=http://$PROXY fetch -T 3 -o /dev/null ${TEST_URL} 2>&1` || return 0
     36  BPS=`echo "${retstr}" | awk '/Bps/ {printf $4}'`
     37  return $BPS
    3438}
    3539
    36 d_log() {
    37         if [ ${DEBUG} -ne 0 ]; then
    38                 log $*
    39         fi
    40 }
     40# Sort proxy list on highest bandwidth
     41sort_proxies() {
     42  result=''
     43  for host in $*; do
     44    bps=`test_proxy $host:3128`
     45    result="$bps $host:3128"
     46  done
    4147
    42 test_proxy()
    43 {
    44         # Set proxy
    45         PROXY=$1
    46         PORT=$2
    47         URL=$3
    48         export HTTP_PROXY="${PROXY}:${PORT}"
    49 
    50         # Attempted fetch
    51         retstr=`fetch -T 3 -o /dev/null ${URL} 2>&1`
    52         retval=$?
    53 
    54         # Store to list if successfull
    55         if [ "${retval}" -eq  0 ]; then
    56                 BPS=`echo "${retstr}" | awk '/Bps/ {printf $4}'`
    57                 echo "${BPS} ${PROXY}" >> ${TMPFILE}
    58         fi
    59         return $retval
    60 }
    61 
    62 sort_proxies()
    63 {
    64         # Result holder
    65         TMPFILE=`mktemp -t pen_wrapper`
    66 
    67         for _host in ${PROXY_LIST}; do
    68                 PROXY=`echo $_host | cut -d ":" -f1`
    69                 PORT=`echo $_host | cut -d ":" -f2`
    70                 _msg="Fetching '${TEST_URL}' via '${PROXY}:${PORT}' ..."
    71                 test_proxy ${PROXY} ${PORT} ${TEST_URL} && d_log ${_msg} "OK" || d_log ${_msg} "FAILED"
    72         done
    73 
    74         _proxylist=`sort -nr ${TMPFILE} | awk '{print $2}' | tr '\n' ' '`
    75         _cfg="0:0:1:1"
    76         if [ -n "${_proxylist}" ]; then
    77                 _prio="0"
    78                 _proxy_arg=""
    79                 for _proxy in ${_proxylist}; do
    80                         _prio=`expr ${_prio} + 1`
    81                         _proxy_arg="${_proxy_arg} ${_proxy}:${PORT}:${_cfg}:${_prio}"
    82                 done
    83         fi
    84         # Clear out junk
    85         rm -f ${TMPFILE}
    86         NEW_PROXY_LIST="${_proxy_arg}"
     48  echo $result | xargs -n2 | sort -r | awk '{print $2}' | xargs
    8749}
    8850
     
    9254LIVE_PROXY_LIST=''
    9355while true; do
    94         sort_proxies
    95         if [ "${LIVE_PROXY_LIST}" != "${NEW_PROXY_LIST}" ]; then
    96                 log "INFO: New listing to be configured '${NEW_PROXY_LIST}'"
    97                 d_log "Live: ${LIVE_PROXY_LIST}"
    98                 d_log "New : ${NEW_PROXY_LIST}"
    99                 # Pen should only be started if alias exists
    100                 ifconfig | grep -q ${BIND_ADDR}
    101                 if [ $? -eq 0 ]; then
    102                         if [ -r ${PIDFILE} ]; then
    103                                 kill `cat ${PIDFILE}`
    104                         fi
    105                         ${PEN} ${PEN_FLAGS} ${NEW_PROXY_LIST}
    106                         LIVE_PROXY_LIST="${NEW_PROXY_LIST}"
    107                 fi
    108         fi
    109         sleep ${TEST_INTERVAL}
     56  PROXY_LIST=`update_proxy_list`
     57  if [ -z "$PROXY_LIST" ]; then
     58    logit "Not starting: list_normal_proxies variable not configured"
     59  else
     60    NEW_PROXY_LIST=`sort_proxies $PROXY_LIST`
     61    if [ "${LIVE_PROXY_LIST}" != "${NEW_PROXY_LIST}" ]; then
     62      logit "INFO: New listing to be configured '${NEW_PROXY_LIST}'"
     63
     64      # Pen should only be started if alias exists
     65      ifconfig | grep -q ${BIND_ADDR} || {
     66        logit "Not starting: alias $BIND_ADDR not configured!"
     67      } && {
     68        [ -r ${PIDFILE} ] && kill `cat ${PIDFILE}`
     69        ${PEN} ${PEN_FLAGS} ${NEW_PROXY_LIST}
     70        LIVE_PROXY_LIST="${NEW_PROXY_LIST}"
     71      }
     72    fi
     73  fi
     74
     75  sleep ${TEST_INTERVAL}
    11076done
Note: See TracChangeset for help on using the changeset viewer.