Setting up SSH keys and an automatic SSH Tunnel for HTTP browsing on a Mac (OS X)

Often, I find myself having to surf the web through my employer. The main reason is that the library subscriptions for online journals recognizes the IP. For some strange reason, my ISP doesn't buy those subscriptions, but my university does. One option would be to set up a hassle free enterprise level VPN connection, while another, to set up a dedicated tunnel with tools available on your OS X.

The easiest thing to do is to tunnel the HTTP requests through an SSH tunnel. This can be done by opening a terminal window and typing the appropriate SSH command. Then, one has to change the relevant proxy configuration. For Firefox, this can be done through its own preferences (Firefox menu -> preferences -> network). For other browsers (such as Safari or Chrome), one has to change the system wide proxy settings (i.e., Apple menu -> system preferences -> network -> advanced -> proxies). The question is how can it be done automatically?

Here's how.

The automatic way of setting up a tunnel includes: (a) Setting up keys to allow password-less SSHing into the remote server. (b) Setting a launchd daemon that keeps the SSH alive. (c) Either setting up firefox to be your tunneled browser or (d) Setting up a proxy file (.pac) with proxy settings which tell any browser which URLs should be surfed from your ISP and which through the tunnel. These steps are not complicated and they are required only once. Then, any time you need to surf with the ip of a remote server, either you use firefox, or, you do nothing special at all.

In what follows, "client" will be your personal machine (i.e., the one you work at home), and "server" will be your machine at work into which you can SSH.