Skip to content

Instantly share code, notes, and snippets.

@callistabee
Last active June 24, 2018 15:03
Show Gist options
  • Save callistabee/27529fde86592d995c2693aba4c55997 to your computer and use it in GitHub Desktop.
Save callistabee/27529fde86592d995c2693aba4c55997 to your computer and use it in GitHub Desktop.
osx: launch and use socks proxy
#!/bin/bash
host=$1
port=$2
function usage() {
echo "usage: $(basename $0) host [port=2046]"
exit 1
}
if [ $(id -u) -ne 0 ]; then
echo "$(basename $0): this script must be run as root"
usage
fi
if [ -z $host ]; then
usage
fi
if [ -z $port ]; then
port=2046
fi
ssh -F $HOME/.ssh/config -TD $port $host &
sshpid=$!
echo "ssh running on PID $sshpid"
networksetup -setsocksfirewallproxy "Wi-Fi" localhost $port
networksetup -setsocksfirewallproxystate "Wi-Fi" on
function sigint() {
echo "caught SIGINT, terminating"
kill $sshpid
networksetup -setsocksfirewallproxystate "Wi-Fi" off
exit 0
}
trap sigint SIGINT
wait $sshpid
echo "ssh died, terminating"
osascript -e \
"display \"warning: proxy went offline\" with title \"$(basename $0)\""
networksetup -setsocksfirewallproxystate "Wi-Fi" off
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment