Using an SSH tunnel and SOCKS proxy to avoid paywalls

Part of my life as somebody who regularly reads academic papers is dealing with the paywalls that most of them are locked behind. I hate them with the passion of a thousand fiery suns when I encounter them; fortunately I don’t often have to since I spend most of my life on campus. But for those times when (god forbid) I have to access imprisoned documents from the luxury of my own home, I can either stare balefully at the paywall screen, fight with the library’s often-misconfigured proxy service, or […drum roll…] pretend I’m on the University’s network by tunneling in over SSH!

This method is for Mac OS X 10.9 “Mavericks”: we’ll be directing HTTP traffic through 8080 instead of 80 using a SOCKS proxy and SSH tunnel into a server within the network you want to appear to be coming from.

First, open System Preferences > Network > Advanced > Proxies. Select the SOCKS proxy option and put “localhost” for the host and 8080 for the port. It doesn’t matter if you turn it on at this point, just make sure you click “save” and “apply” to save these options.

Next, copy these two functions* into your .bashrc file or wherever else you keep your repository of neat bash functions, making sure to change the ssh login on line 7:

function toggleproxy {
# checks to see if SOCKS proxy is enabled
if [[ $(networksetup -getsocksfirewallproxy Wi-Fi | grep '^Enabled') == "Enabled: No" ]]; then
networksetup -setsocksfirewallproxystate Wi-Fi on
echo "SOCKS on!"
# checks to see if there's an existing SSH tunnel and if not, it starts one
if [[ -z $(ps aux | grep '[0-9] ssh -D 8080') ]]; then
echo -ne "Don't see a ssh tunnel on 8080 active, starting one now..."
ssh -D 8080 -f -C -q -N USERNAME@HOSTNAME.EDU # Change this from the defaults!
[[ $? == 0 ]] && echo " success!" || echo " failed 😦"
fi
else
networksetup -setsocksfirewallproxystate Wi-Fi off
# only show this message if there's an active SSH tunnel
if [[ -n $(ps aux | grep '[0-9] ssh -D 8080') ]]; then
echo "SOCKS off! You may want to kill your existing SSH tunnels with 'killtunnel'."
else
echo "SOCKS off!"
fi
fi
}
function maketunnel {
ssh -D 8080 -f -C -q -N USERNAME@HOSTNAME.EDU # Change this from the defaults!
}
## kills all SSH connections with port forwarding to 8080
function killtunnel {
for x in `ps -u $USER | grep -P '[0-9] ssh -D 8080' | awk '{print $2}'`; do
kill $x
done
}
view raw toggleproxy.sh hosted with ❤ by GitHub

Once this is saved and loaded into your profile, just run  

 toggleproxy 

from your command line to automatically switch the SOCKS proxy on or off. If you’re turning it on, the function will attempt to open a new SSH tunnel if it doesn’t detect one already present.

I didn’t have it automatically kill your tunnels because maybe you had it open for some other reason. Plus you can disable/reenable SOCKS repeatedly using the same SSH tunnel, so there’s no need to kill it every time. To kill any tunnels you have open, run

 killtunnel 

 

* I am not a very good Bash hacker, so I am 100% positive there’s a better way to write these functions. But this works for now.