Last active
December 24, 2015 04:59
-
-
Save emiller/6747331 to your computer and use it in GitHub Desktop.
proxy (socks ssh proxy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# proxy -- simple socks proxy for those open wifi networks. | |
# | |
# Usage is simple: | |
# | |
# proxy <up|down|watch|show|ssh> [alt remote port] [alt remote host] | |
# | |
# `proxy up` will create a socks proxy listening locally on 8000 by | |
# default, through a rhost server via ssh over port 53 (dns/domain). | |
# When successfully connected, it will configure networking to use | |
# the socks proxy when attempting to browse HTTP/HTTPS and disable it | |
# when the proxy disconnects. | |
# | |
# Tweak these defaults to match your setup | |
BIND=8000 | |
HOST= | |
PORT=53 | |
ICON_UP="network-wireless-encrypted.png" | |
ICON_DOWN="connect_no.png" | |
ICON_BASE="/usr/share/icons/Tango/32x32/status" | |
# The stuff below should mostly be fine as is | |
bport=$BIND | |
rport=${2:-`(ps ax | grep -v grep | egrep -q "ssh.*D.$bport.*p.*LocalCommand") && (ps ax | grep -v grep | egrep "ssh.*D.$bport.*p.*LocalCommand" | sed 's/^.*ssh.*-p.\([0-9]*\).-.*-.*\$/\1/') || echo $PORT`} | |
rhost=${3:-$HOST} | |
fname=`readlink -f $0` | |
logfile="/tmp/.proxy.$rhost.$rport.log" | |
pattern="ssh.*D.$bport.*p.$rport.*o.LocalCommand.*$rhost" | |
statcmd="pkill -0 -f $pattern" | |
[[ "$rhost" == "" && "$1" != "show" ]] && shift $# | |
# Display a bubble alert | |
function notice_please() { | |
icon="$ICON_BASE/$1" | |
subject=$2 | |
msg=$3 | |
level=${4:-low} | |
notify-send -a plskthx -u $level -i $icon "$subject" "$msg" | |
echo | |
echo "$subject: $msg" | |
} | |
# Toggle socks proxy on/off in system configuration | |
function socks_toggle() { | |
dconf write /system/proxy/socks/host "'localhost'" | |
dconf write /system/proxy/socks/port $bport | |
if [ "$1" = "up" ]; then | |
dconf write /system/proxy/http/enabled true | |
dconf write /system/proxy/mode "'manual'" | |
else | |
dconf write /system/proxy/mode "'none'" | |
dconf write /system/proxy/http/enabled false | |
fi | |
} | |
case $1 in | |
up) | |
$statcmd && proxy down $rport | |
{ | |
ssh -vvvv -N \ | |
-D $bport \ | |
-p $rport \ | |
-o PermitLocalCommand=yes \ | |
-o ExitOnForwardFailure=yes \ | |
-o ServerAliveInterval=5 \ | |
-o ServerAliveCountMax=2 \ | |
-o TCPKeepAlive=yes \ | |
-o LocalCommand="$fname show $rport $rhost" \ | |
$rhost > $logfile 2>&1 & | |
socks_toggle up | |
wait | |
socks_toggle down | |
proxy show $rport $rhost | |
} & | |
;; | |
down) | |
socks_toggle down | |
pkill -f "$pattern" | |
;; | |
watch) | |
test -f $logfile && cat $logfile && tail -n 0 -f $logfile || echo "no proxy to watch" | |
;; | |
ssh) | |
ssh -p $rport $rhost | |
;; | |
show) | |
$statcmd && { | |
notice_please $ICON_UP "proxy($bport) -> ssh($rport)" "connected to $rhost" | |
} || { | |
notice_please $ICON_DOWN "proxy($bport) -> ssh($rport)" "disconnected from $rhost" | |
} | |
;; | |
*) | |
echo | |
echo "usage:" | |
echo | |
echo " `basename $0` <up|down|watch|show|ssh> [alt remote port] [alt remote host]" | |
echo | |
echo "default:" | |
echo | |
echo " socks port: $BIND" | |
echo " remote port: $PORT" | |
echo " remote host: $HOST" | |
echo | |
if [ "$HOST" == "" ]; then | |
echo " default 'remote host' (HOST) is not set" | |
echo " to fix this, edit HOST variable in $fname" | |
fi | |
echo | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment