Wednesday, March 15, 2006

DynamicForward, Where Have You Been All My Life?

So I just recently discovered the DynamicForward option for openssh, and man, I've gotta tell you, it's just about the coolest thing ever. I've been using ssh to forward specific ports practically forever, but this option starts up a full fledged SOCKS proxy, so you can point Firefox at it and it'll let you do arbitrary HTTP requests through your tunnel, not to mention all the other applications that work with SOCKS proxies.

Just run something like ssh -o 'DynamicForward 1080' user@host.name and you'll get your SOCKS proxy listening on port 1080 on your local machine.

Now that's cool, but combine it with Firefox's PAC support for dynamically configuring proxies and you're really cooking. You see, the regular proxy settings in Firefox just let you set your proxy and exclude a list of URLs if you're so inclined. That's nice, but what I really want is for a specific set of URLs (say, the internal servers on a remote network accessible only via ssh) to be proxied but everything else should use my direct connection to the internet.

First, we write a PAC file, which is just a text file that contains a single Javascript function:

function FindProxyForURL(url, host)
{
if (dnsDomainIs(host, ".domain.name")) {
return "SOCKS localhost:1080; DIRECT";
} else {
return "DIRECT";
}
}

Then you stick it up on a web server someplace and set the URL as your Automatic Proxy Configuration URL in your Firefox Connection Settings. From then on every time you try to connect to a host in *.domain.name it'll go through the proxy (or try to go directly via the internet if the proxy doesn't work), and everything else just uses your local internet connection.

There are a bunch of other interesting and useful functions like dnsDomainIs() that you can make use of in your implementation of FindProxyForURL(), just take a look at the docs.

If you want to confirm that the things you want to go through the proxy are doing so, just start up the ssh tunnel with the -v option and you'll get debug output every time a new proxy connection is established.

I seriously cannot believe that all this functionality was just sitting there in tools I literally use every day and I never knew about it until now. No more screwing around with /etc/hosts and port forwarding for me!

3 comments:

  1. Check out FoxyProxy extension for Firefox and you might write another post like that about it.

    ReplyDelete
  2. Wow. This is the answer to so many of my prayers. Additional neat features:
    1. No support from the remote sshd server is required, so you can do this using the OpenSSH client and any SSH server.
    2. It supports both SOCKS4 and SOCKS5 protocols, which is nice.

    ReplyDelete
  3. How often does Firefox read from the URL? That is, if I make a change, does Firefox need to be restarted, does the user have to go back to the Preferences to manually reload it? Or does Firefox check this all the time?

    ReplyDelete