~ubuntu-branches/ubuntu/oneiric/bikeshed/oneiric

« back to all changes in this revision

Viewing changes to socks-prox

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2010-11-19 22:20:29 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20101119222029-oap0j3d0v0pd3dph
Tags: 1.7-0ubuntu1
* COPYING: add GPL license, LP: #663292
* Makefile, run-one, run-one.1: add the run-one utility
* run-one, run-one.1: use flock, mention in documentation
* debian/control: util-linux needed for flock
* Makefile, what-provides.1, whatprovides => what-provides:
  provide a what-provides utility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#
 
3
#    socks-prox - establish and encrypted connection for tunneling
 
4
#                 traffic through a socks proxy
 
5
#
 
6
#    Copyright (C) 2010 Dustin Kirkland <kirkland@ubuntu.com>
 
7
#
 
8
#    Authors:
 
9
#        Dustin Kirkland <kirkland@ubuntu.com>
 
10
#        Scott Moser <smoser@ubuntu.com>
 
11
#
 
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.
 
15
#
 
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.
 
20
#
 
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/>.
 
23
 
 
24
## Debugging info:
 
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' 
 
30
##
 
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.
 
34
 
 
35
error() {
 
36
        echo "ERROR: $@" 1>&2
 
37
        exit 1
 
38
}
 
39
 
 
40
info() {
 
41
        echo "INFO: $@"
 
42
}
 
43
 
 
44
status_info() {
 
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};;
 
53
                        -D:*) socks=${flag};;
 
54
                esac
 
55
                pflag=${flag}
 
56
        done
 
57
        [ -n "${uhost}" ] && _RET_USERHOST=${uhost}
 
58
        [ -n "${socks}" ] && _RET_SOCKS=${socks}
 
59
        if [ -n "${cpath}" -a -n "${uhost}" ]; then
 
60
                _RET_CONNECTED=0
 
61
                ssh -o "${cpath}" -o ControlMaster=no "${uhost}" \
 
62
                        /bin/true && _RET_CONNECTED=1
 
63
        fi
 
64
}
 
65
 
 
66
Usage() {
 
67
        cat <<EOF
 
68
${PROG}: [ options ] command
 
69
 
 
70
    manage a socks/ssh tunnel to an ec2 instance
 
71
 
 
72
    command is one of 'start', 'stop', or 'status'
 
73
    options:
 
74
       -h | --help      show this message
 
75
EOF
 
76
}
 
77
 
 
78
PROG=$(basename "$0")
 
79
EC2PRE=${EC2PRE:-ec2-}
 
80
RUSER="ubuntu"
 
81
UID=$(id -u)
 
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"
 
85
SOCKS=0
 
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)}')
 
89
done
 
90
SSH_OPTS="-f -N -C ${O_OPTS} -D ${SOCKS}"
 
91
 
 
92
[ $# -eq 0 ] && { Usage 1>&2; exit 1; }
 
93
[ "$1" = "--help" -o "$1" = "-h" ] && { Usage; exit 0; }
 
94
 
 
95
case $1 in
 
96
        start)
 
97
                status_info
 
98
                [ -n "${_RET_PID}" ] &&
 
99
                        error "${PROG} is already running as ${_RET_PID}. please stop first"
 
100
 
 
101
                if [ -z "$2" ]; then
 
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}
 
106
                else
 
107
                        uhost=${2}
 
108
                fi
 
109
                        
 
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'/"
 
120
                        
 
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"
 
127
        ;;
 
128
        stop)
 
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)..."
 
136
        ;;
 
137
        status)
 
138
                status_info
 
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}]"
 
143
                        exit 0
 
144
                else
 
145
                        echo "$PROG not connected."
 
146
                        info "Seems bad connection on pid [${_RET_PID}] to [${_RET_USERHOST}]"
 
147
                        exit 1
 
148
                fi
 
149
        ;;
 
150
        *)
 
151
                error "Unknown parameter"
 
152
        ;;
 
153
esac
 
154
 
 
155
exit 0
 
156
 
 
157
# vi: ts=4 noexpandtab