Skip to content

Instantly share code, notes, and snippets.

@OmgImAlexis
Forked from eclarke/toggleproxy.sh
Last active August 29, 2015 14:24
Show Gist options
  • Save OmgImAlexis/f7fdedc9bf19f3369c6d to your computer and use it in GitHub Desktop.
Save OmgImAlexis/f7fdedc9bf19f3369c6d to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# ^^^^^^^ That tells your shell what shell should be used to execute this script.
#
# As a rule you nearly always write in borne shell (plain sh)
# regardless of what shell you use for your terminals.
#
# Hence this should always be the first line in every script you write.
#
#
# To run this script from the CLI you only need to do:
#
# ./scriptname.sh
#
# You should never need to exec it.
#
# if it's not executable, make it so:
#
# chmod +x scriptname.sh
#
#
# I've commented out those lines just so I could test the structure syntax is
# correct. Uncomment them and let me know if they work.
#
function socks
{
if [ "$1" = "on" ]; then
echo "Getting dressed"
# checks to see if there's an existing SSH tunnel and if not, it starts one
if [[ -z $(lsof -n -i :8080 | grep 'ssh' | awk '{print $2;}') ]]; then
echo "Putting my socks on"
sudo networksetup -setsocksfirewallproxy "Ethernet" localhost 8080
sudo networksetup -setsocksfirewallproxy "Wi-Fi" localhost 8080
ssh -C2qTnNf -D 8080 mia
echo "The socks are on!"
else
echo "We have socks on, I'm not putting on another pair!"
fi
elif [ $1 = "off" ]; then
if [[ -z $(lsof -n -i :8080 | grep 'ssh' | awk '{print $2;}') ]]; then
echo "I'm not wearing any socks?"
else
echo "Taking my socks off"
sudo networksetup -setsocksfirewallproxystate "Ethernet" off
sudo networksetup -setsocksfirewallproxystate "Wi-Fi" off
kill -9 $(lsof -n -i :8080 | grep 'ssh' | awk '{print $2;}')
echo "Clearnet is open!"
fi
elif [ $1 = "status" ]; then
if [[ -z $(lsof -n -i :8080 | grep 'ssh' | awk '{print $2;}') ]]; then
echo "My socks are off!"
else
echo "My socks are on!"
fi
else
echo "Huh?"
fi
}
#
# If the number of arguments are less than 1 let the idiot know!
#
if [ $# -lt 1 ]; then
echo "Error: I can only put socks on or off, wtf?"
exit 1
fi
# calls must come after function definitions
socks $@;
@OmgImAlexis
Copy link
Author

Socks now does error checks before doing anything and has a status command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment