Last active
August 12, 2016 04:52
-
-
Save smj10j/a84af2034de07e79961f2c2c63be61d1 to your computer and use it in GitHub Desktop.
Opens a quick SOCKS5 tunnel over Tor, starts Tor, and configures OSX to use the 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 | |
# activate debugging | |
#set -x | |
# fail on any errors | |
set -e | |
INTERFACE=Wi-Fi | |
HOST=127.0.0.1 | |
PORT=9050 | |
################################## | |
##### DO NOT EDIT BELOW HERE ##### | |
################################## | |
TOR_ALREADY_RUNNING=0 | |
TOR_SERVICE_CHECK_OUTPUT=$(brew services list | (grep "tor\s*started" || echo "")) | |
[ -n "${TOR_SERVICE_CHECK_OUTPUT}" ] && TOR_ALREADY_RUNNING=1 | |
# Ask for the administrator password upfront | |
sudo -v | |
function disable_proxy() { | |
echo -n "$(tput setaf 136)" # orange | |
echo "Disabling SOCKS proxy..." | |
sudo networksetup -setsocksfirewallproxystate $INTERFACE off | |
echo -n "$(tput setaf 64)" #green | |
echo "SOCKS proxy disabled." | |
if [[ $TOR_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Stopping Tor..." | |
brew services stop tor | |
echo -n "$(tput setaf 64)" #green | |
echo "Stopped Tor." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
#echo "-- At Launch --" | |
#echo "${TOR_SERVICE_CHECK_OUTPUT}" | |
#echo "-- Now --" | |
#brew services list | egrep "tor\s+started" | |
echo "Tor was previously running so not stopping it." | |
fi | |
echo -n "$(tput sgr0)" # color reset | |
exit 0 | |
} | |
#This function is used to cleanly exit any script. It does this displaying a | |
# given error message, and exiting with an error code. | |
function error_exit { | |
echo -n "$(tput setaf 1)" # red | |
echo "$@" | |
echo -n "$(tput sgr0)" # color reset | |
disable_proxy | |
exit 1 | |
} | |
#Trap the killer signals so that we can exit with a good message. | |
trap "error_exit 'Received signal SIGHUP'" SIGHUP | |
trap "error_exit 'Received signal SIGINT'" SIGINT | |
trap "error_exit 'Received signal SIGTERM'" SIGTERM | |
trap "error_exit 'Received signal SIGKILL'" SIGKILL | |
trap "error_exit 'Received signal ERR'" ERR | |
#Alias the function so that it will print a message with the following format: | |
#prog-name(@line#): message | |
#We have to explicitly allow aliases, we do this because they make calling the | |
#function much easier (see example). | |
shopt -s expand_aliases | |
alias die='error_exit "Error ${0}(@`echo $(( $LINENO - 1 ))`):"' | |
# Let's roll | |
echo -n "$(tput setaf 136)" # orange | |
echo "Enabling SOCKS proxy..." | |
sudo networksetup -setsocksfirewallproxy $INTERFACE $HOST $PORT | |
sudo networksetup -setsocksfirewallproxystate $INTERFACE on | |
echo -n "$(tput setaf 64)" # green | |
echo "SOCKS proxy $HOST:$PORT enabled." | |
if [[ $TOR_ALREADY_RUNNING -eq 0 ]]; then | |
echo -n "$(tput setaf 136)" # orange | |
echo "Starting Tor..." | |
brew services start tor | |
echo -n "$(tput setaf 64)" # green | |
echo "Tor started." | |
else | |
echo -n "$(tput setaf 136)" # orange | |
echo "Existing Tor service is already running. We will not attempt to start it." | |
#echo "${TOR_SERVICE_CHECK_OUTPUT}" | |
fi | |
echo -n "$(tput setaf 64)" # green | |
echo "Tor is running." | |
echo -n "$(tput sgr0)" # color reset | |
# Keep-alive: update existing `sudo` time stamp until finished | |
while read -n1; do | |
sleep 1 | |
done 2>/dev/null # trap ctrl-c and call disable_proxy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment