3
# socks-prox - establish and encrypted connection for tunneling
4
# traffic through a socks proxy
6
# Copyright (C) 2010 Dustin Kirkland <kirkland@ubuntu.com>
9
# Dustin Kirkland <kirkland@ubuntu.com>
10
# Scott Moser <smoser@ubuntu.com>
12
# This program is free software: you can redistribute it and/or modify
13
# it under the terms of the GNU General Public License as published by
14
# the Free Software Foundation, either version 3 of the License.
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
# You should have received a copy of the GNU General Public License
22
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
## The following "works for me" to use http://ip2location.com to check
26
## if the proxy is up and functional:
27
## $ curl --silent --socks4 localhost:${PORT} http://www.ip2location.com/ |
28
## sed -n -e 's,.*Livedemo1_lblISP[^>]*>\([^<]*\)<.*,\1,p' \
29
## -e 's,.*Livedemo1_lblIpAddress[^>]*>\([^<]*\)<.*,\1,p'
31
## Where 'PORT' is the port as shown by 'status'
32
## If you remove the '--socks4', curl will connect directly, and you should
33
## see other information.
45
_RET_PID="" _RET_USERHOST="" _RET_CONNECTED="" _RET_SOCKS=""
46
_RET_PID=$(pgrep -f "${SSH_TUNNEL_NAME}" -U "${UID}") || return 0
47
local flags="" flag="" uhost="" cpath="" pflag="" socks=""
48
flags=$(ps -o args= ${_RET_PID})
49
for flag in $flags; do
50
case "${pflag}:${flag}" in
51
${socks:-xxxxx}:*) uhost="${flag}";;
52
*:ControlPath=*) cpath=${flag};;
57
[ -n "${uhost}" ] && _RET_USERHOST=${uhost}
58
[ -n "${socks}" ] && _RET_SOCKS=${socks}
59
if [ -n "${cpath}" -a -n "${uhost}" ]; then
61
ssh -o "${cpath}" -o ControlMaster=no "${uhost}" \
62
/bin/true && _RET_CONNECTED=1
68
${PROG}: [ options ] command
70
manage a socks/ssh tunnel to an ec2 instance
72
command is one of 'start', 'stop', or 'status'
74
-h | --help show this message
79
EC2PRE=${EC2PRE:-ec2-}
82
SSH_TUNNEL_NAME="${PROG}-ssh"
83
CONTROL_PATH="$HOME/.${PROG}.${SSH_TUNNEL_NAME}"
84
O_OPTS="-o ControlMaster=yes -o ControlPath=$HOME/.${SSH_TUNNEL_NAME}.ssh"
86
while [ "$SOCKS" -lt 10000 ] || (netstat -an | grep -qs "$SOCKS"); do
87
# Randomize the port selection
88
SOCKS=$(echo | awk '{srand(); printf "%d\n", (rand()*10000+10000)}')
90
SSH_OPTS="-f -N -C ${O_OPTS} -D ${SOCKS}"
92
[ $# -eq 0 ] && { Usage 1>&2; exit 1; }
93
[ "$1" = "--help" -o "$1" = "-h" ] && { Usage; exit 0; }
98
[ -n "${_RET_PID}" ] &&
99
error "${PROG} is already running as ${_RET_PID}. please stop first"
102
info "Looking for running cloud instances..."
103
hostname=$(${EC2PRE}describe-instances | awk '-F\t' '$6 == "running" { print $4; exit(0); }')
104
[ -z "$hostname" ] && error "No running instances found -- try starting a cloud instance"
105
uhost=${RUSER}@${hostname}
110
info "Selecting instance [$hostname]..."
111
info "Configuring gnome..."
112
gconftool-2 -t string -s /system/proxy/mode "auto" \
113
-t string -s /system/proxy/socks_host "localhost" \
114
-t string -s /system/proxy/socks_port "$SOCKS"
115
# BUG: This is a nasty dirty hack, but a bug in chromium prevents it
116
# from re-reading socks proxy information from gconf.
117
find "$HOME/.config/chromium" -name "*.pac" -type f -print0 2>/dev/null |
118
xargs -0 --no-run-if-empty \
119
sed -i "s/'SOCKS5 localhost:.*'/'SOCKS5 localhost:$SOCKS'/"
121
info "Establishing tunnel to [$hostname] on port [$SOCKS]..."
122
info "You may need to restart your browser(s)..."
123
SSH_TUNNEL_NAME="${SSH_TUNNEL_NAME}" \
124
bash -c 'exec -a ${SSH_TUNNEL_NAME} -- "$@"' arg0 \
125
ssh ${O_OPTS} -f -N -C -D "${SOCKS}" "${uhost}"
126
[ $? -eq 0 ] || error "connection ssh failed"
129
info "De-configuring gnome..."
130
gconftool-2 -t string -s /system/proxy/mode "none"
131
info "Terminating tunnel..."
132
# BUG: Ideally, we'd save the pid of the ssh process and kill that here.
133
# For now, kill any/all tunnels to AWS.
134
pkill -f "${SSH_TUNNEL_NAME}" -U "${UID}"
135
info "You may need to restart your browser(s)..."
139
[ -n "${_RET_PID}" ] || { echo "$PROG is not running"; exit 1; }
140
if [ "${_RET_CONNECTED}" = "1" ]; then
141
echo "$PROG connected."
142
info "pid=[${_RET_PID}] host=[${_RET_USERHOST}] port=[${_RET_SOCKS}]"
145
echo "$PROG not connected."
146
info "Seems bad connection on pid [${_RET_PID}] to [${_RET_USERHOST}]"
151
error "Unknown parameter"
157
# vi: ts=4 noexpandtab