Sunday, January 18, 2009

AutoTunnel - Persistent SSH tunnel connection for Socks Proxy

Recently, I had a need to create a persistent tunnel to a remote host to be utilized as a Socks proxy. After having been annoyed with gstm's inability to start hidden, and the lack of an option to re-establish a broken tunnel; I've whipped up a simple script to replace it. If you are connecting to the net and surfing from an internet cafe or any other location where you don't want to worry with the person sitting next to you capturing your unencrypted traffic due to a mis-configured access point, this may be the script for you.

Establishing an ssh connection as a socks proxy is pretty simple, the syntax is:

ssh -D port user@host

This is unfortunately limited to a timeout that's very high. To work around it, I added a few options to force a client side timeout. Then I tell ssh to compress, and I wrap it in a fork with simple logic to look for active interfaces and a sleep.

AutoTunnel will test the connection every 10 seconds and if it's broken, check to ensure you have an interface with a valid IP before retrying.

To run it, extract the zip package (URL) make the autotunnel script executable. Now, create a new session entry for it in GNOME with the syntax as follows: /path/to/autotunnel user@host. If you would like GUI notifications, make sure libnotify-bin is installed on your computer.

To use it, set your application to use a Socks proxy with an address of localhost and a port of 8888.