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 |
|
---|
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}"
|
---|
12 |
|
---|
13 |
|
---|
14 | TEST_URL="http://www.ams-ix.net/"
|
---|
15 | TEST_INTERVAL=`expr 30 \* 60` # Back-off period in seconds, re-testing period
|
---|
16 |
|
---|
17 |
|
---|
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
|
---|
34 | }
|
---|
35 |
|
---|
36 | d_log() {
|
---|
37 | if [ ${DEBUG} -ne 0 ]; then
|
---|
38 | log $*
|
---|
39 | fi
|
---|
40 | }
|
---|
41 |
|
---|
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}"
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | ##
|
---|
91 | # Main loop
|
---|
92 | LIVE_PROXY_LIST=''
|
---|
93 | while 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}
|
---|
110 | done
|
---|