source: hybrid/trunk/nanobsd/files/usr/local/bin/pen_wrapper@ 10123

Last change on this file since 10123 was 10123, checked in by richardvm, 14 years ago

creating trunk based on 9.0

  • Property svn:executable set to *
  • Property svn:mergeinfo set to
File size: 2.7 KB
Line 
1#!/bin/sh
2# Pen proxy wrapper, periodic check for best connections
3# Stichting Wireless Leiden
4# Rick van der Zwet <rick@wirelessleiden.nl>
5
6BIND_ADDR=${1-172.31.255.1}
7BIND_PORT=${2-3128}
8DEBUG=0
9
10#XXX: Really static list, some dynamic alternative prefered
11PROXY_LIST="${3-172.17.8.68:3128 \
12 172.17.143.4:3128 \
13 172.20.128.98:3128 \
14 172.16.2.254:3128 \
15 172.19.168.66:3128 \
16 172.16.3.146:3128 \
17 172.17.16.66:3128 \
18 172.17.0.1:3128 \
19 172.16.4.54:3128 \
20 172.22.0.66:3128 \
21 172.23.25.66:3128 \
22 172.17.169.66:3128}"
23
24
25TEST_URL="http://www.ams-ix.net/"
26TEST_INTERVAL=`expr 30 \* 60` # Back-off period in seconds, re-testing period
27
28
29# Don't touch, unless you know what you are doing
30PIDFILE='/var/run/pen.pid'
31PEN='/usr/local/bin/pen'
32PEN_FLAGS="-b 30 -r -p ${PIDFILE} -o prio ${BIND_ADDR}:${BIND_PORT}"
33
34LOGFILE='/var/log/pen_wrapper.log'
35
36log()
37{
38 _datestamp=`date "+%Y-%m-%d %H:%M:%S"`
39 _msg="[${_datestamp}] $*"
40 if [ ${DEBUG} -eq 0 ]; then
41 echo "${_msg}" >> ${LOGFILE}
42 else
43 echo "${_msg}"
44 fi
45}
46
47d_log() {
48 if [ ${DEBUG} -ne 0 ]; then
49 log $*
50 fi
51}
52
53test_proxy()
54{
55 # Set proxy
56 PROXY=$1
57 PORT=$2
58 URL=$3
59 export HTTP_PROXY="${PROXY}:${PORT}"
60
61 # Attempted fetch
62 retstr=`fetch -T 3 -o /dev/null ${URL} 2>&1`
63 retval=$?
64
65 # Store to list if successfull
66 if [ "${retval}" -eq 0 ]; then
67 BPS=`echo "${retstr}" | awk '/Bps/ {printf $4}'`
68 echo "${BPS} ${PROXY}" >> ${TMPFILE}
69 fi
70 return $retval
71}
72
73sort_proxies()
74{
75 # Result holder
76 TMPFILE=`mktemp -t pen_wrapper`
77
78 for _host in ${PROXY_LIST}; do
79 PROXY=`echo $_host | cut -d ":" -f1`
80 PORT=`echo $_host | cut -d ":" -f2`
81 _msg="Fetching '${TEST_URL}' via '${PROXY}:${PORT}' ..."
82 test_proxy ${PROXY} ${PORT} ${TEST_URL} && d_log ${_msg} "OK" || d_log ${_msg} "FAILED"
83 done
84
85 _proxylist=`sort -nr ${TMPFILE} | awk '{print $2}' | tr '\n' ' '`
86 _cfg="0:0:1:1"
87 if [ -n "${_proxylist}" ]; then
88 _prio="0"
89 _proxy_arg=""
90 for _proxy in ${_proxylist}; do
91 _prio=`expr ${_prio} + 1`
92 _proxy_arg="${_proxy_arg} ${_proxy}:${PORT}:${_cfg}:${_prio}"
93 done
94 fi
95 # Clear out junk
96 rm -f ${TMPFILE}
97 NEW_PROXY_LIST="${_proxy_arg}"
98}
99
100
101##
102# Main loop
103LIVE_PROXY_LIST=''
104while true; do
105 sort_proxies
106 if [ "${LIVE_PROXY_LIST}" != "${NEW_PROXY_LIST}" ]; then
107 log "INFO: New listing to be configured '${NEW_PROXY_LIST}'"
108 d_log "Live: ${LIVE_PROXY_LIST}"
109 d_log "New : ${NEW_PROXY_LIST}"
110 # Pen should only be started if alias exists
111 ifconfig | grep -q ${BIND_ADDR}
112 if [ $? -eq 0 ]; then
113 if [ -r ${PIDFILE} ]; then
114 kill `cat ${PIDFILE}`
115 fi
116 ${PEN} ${PEN_FLAGS} ${NEW_PROXY_LIST}
117 LIVE_PROXY_LIST="${NEW_PROXY_LIST}"
118 fi
119 fi
120 sleep ${TEST_INTERVAL}
121done
Note: See TracBrowser for help on using the repository browser.