cleanup
[ub0rlay.git] / net-proxy / routify / files / routify-0
blob18dc5bac344bdf77880e5cf8bccacf18ad6669b7
1 #! /bin/sh
3 # Wrapper script for use of the tsocks(8) transparent socksification library
4 # See the tsocks(1) manpages.
6 # Copyright (c) 2004, 2006 Peter Palfrader
7 # Modified by Jacob Appelbaum <jacob@appelbaum.net> April 16th 2006
8 # Modified by Felix Bechstein <f@ub0r.de> Dec 23th 2007
11 # Define and ensure we have tsocks
12 # XXX: what if we don't have which?
13 TSOCKS="`which tsocks`"
14 if [ ! -x "$TSOCKS" ]
15 then
16 echo "$0: Can't find tsocks in PATH. Perhaps you haven't installed it?" >&2
17 exit 1
20 # Define TMPDIR
21 TMPDIR=/tmp/
23 # Check for any argument list
24 if [ "$#" = 0 ]
25 then
26 echo "Usage: $0 <port/dev> <command> [<options>...]" >&2
27 exit 1
30 if [ "$#" = 1 ] && ( [ "$1" = "-h" ] || [ "$1" = "--help" ] )
31 then
32 echo "Usage: $0 <port/dev> <command> [<options>...]"
33 exit 0
36 TSOCKS_PORT=$1
37 shift
39 if [ "$#" = 0 ]
40 then
41 echo "Usage: $0 <port/dev> <command> [<options>...]" >&2
42 exit 1
44 if [ "$#" = 1 ] && ( [ "$1" = "-h" ] || [ "$1" = "--help" ] )
45 then
46 echo "Usage: $0 <port/dev> <command> [<options>...]"
47 exit 0
50 # generate config file for port/dev $TSOCKS_PORT
51 if [ -r "${TMPDIR}routify.${TSOCKS_PORT}" ] ; then
52 #conf file already generated. we should use that conf
53 TSOCKS_CONF_FILE="${TMPDIR}routify.${TSOCKS_PORT}"
56 if echo test | nc localhost ${TSOCKS_PORT} > /dev/null 2> /dev/null ; then
57 if [ -z "$TSOCKS_CONF_FILE" ] ; then
58 echo "server = 127.0.0.1" > ${TMPDIR}routify.${TSOCKS_PORT}
59 echo "server_port = ${TSOCKS_PORT}" >> ${TMPDIR}routify.${TSOCKS_PORT}
61 else
62 /sbin/ifconfig | grep -o "^[a-zA-Z][^ ]*" | grep -v ^lo$ > "${TMPDIR}devlist"
63 if echo ${TSOCKS_PORT} | grep -f "${TMPDIR}devlist" > /dev/null ; then
64 TSOCKS_DEV=${TSOCKS_PORT}
65 # found device as arg
66 # but no proxy started for this device
67 if [ -z "$TSOCKS_CONF_FILE" ] ; then
68 TSOCKS_PORT=10080
69 while true ; do
70 if echo test | nc localhost ${TSOCKS_PORT} > /dev/null 2> /dev/null ; then
71 TSOCKS_PORT=$(expr ${TSOCKS_PORT} + 1) # search for free port
72 else
73 if fgrep "server_port = $TSOCKS_PORT" ${TMPDIR}routify.* > /dev/null 2> /dev/null ; then
74 TSOCKS_PORT=$(expr ${TSOCKS_PORT} + 1) # search for free port
75 else
76 break
79 done
80 # generate conf file
81 echo "server = 127.0.0.1" > ${TMPDIR}routify.${TSOCKS_DEV}
82 echo "server_port = ${TSOCKS_PORT}" >> ${TMPDIR}routify.${TSOCKS_DEV}
83 else
84 # read port from conf file
85 TSOCKS_PORT=$(grep server_port ${TMPDIR}routify.${TSOCKS_PORT} | tr -d 'a-zA-Z =#;_')
87 # start nylon on $TSOCKS_DEV to listen on $TSOCKS_PORT
88 echo test | nc localhost ${TSOCKS_PORT} 2> /dev/null > /dev/null || \
89 nylon -p $TSOCKS_PORT -I $TSOCKS_DEV -P ${TMPDIR}routify.${TSOCKS_DEV}.nylon
90 TSOCKS_CONF_FILE="${TMPDIR}routify.${TSOCKS_DEV}"
91 else # proxy down
92 echo "socket-proxy is down!"
93 rm "${TMPDIR}devlist"
94 exit 1
96 rm "${TMPDIR}devlist"
101 # Define our tsocks config file
102 [ -z "$TSOCKS_CONF_FILE" ] && TSOCKS_CONF_FILE="${TMPDIR}routify.${TSOCKS_PORT}"
103 export TSOCKS_CONF_FILE
105 # Check that we've got a tsocks config file
106 if [ -r "$TSOCKS_CONF_FILE" ]
107 then
108 exec tsocks "$@"
109 echo "$0: Failed to exec tsocks $@" >&2
110 exit 1
111 else
112 echo "$0: Missing tsocks configuration file \"$TSOCKS_CONF_FILE\"." >&2
113 exit 1