Saturday, January 29, 2011

How to setup browser to automatically switch proxy

I'm a notebook user where in one of my locations I need to use http proxy to access Internet. It is obviously a tedious work to switch it manually every time you change a network. You usually need to go deeply into options to switch it. For Firefox there are fortunately some plugins which simplify that process. But still manual step is needed. I suppose that many people suffer from this and the Firefox plugins are proving it.

But browsers (at least Firefox, IE, Opera) provide a proxy automatic configuration (PAC) script. It is simply a JavaScript which allows you to select proxy settings depending on eg. host being accessed or your IP. And here is the key. If your network with proxy has a unique address space, you can use it to select proxy or direct connection.

To do that just create a file of this type (it typically has pac extension):

function FindProxyForURL(url, host)
{
  if (isInNet(myIpAddress(), "proxy_network_address", "proxy_network_mask"))
    return "PROXY proxy_host:proxy_port;";
  else
    return "DIRECT";

Where:
  • proxy_network_address is address of the network where you need to use proxy
  • proxy_network_mask is a mask for the network with proxy
  • proxy_host address of the proxy to be used
  • proxy_host port of the proxy to be used
For instance if you are in network 192.168.0.0 with mask 255.255.255.0, which means your IP address is in range  192.168.0.1 - 192.168.0.254, and your proxy host is 192.168.0.10 on port 80 the file would look like this:

function FindProxyForURL(url, host)
{
  if (isInNet(myIpAddress(), "192.168.0.0", "255.255.255.0"))
    return "PROXY 192.168.0.10:80;";
  else
    return "DIRECT";
}

You can return more than one proxy as a fall back after the semicolon.


Now you just need to point proxy setting to that file. In Firefox you go to Options -> Advanced -> Network -> Settings in "Connection" group -> and set "URL address for automatic configuration". Note it is expected to be in URL format, so you need to use "file://" format. The easiest way to get the address is just open the file (File -> Open or Ctrl-O) and copy the address.

And that is all. No more proxy switching assuming you do not use other network of the same address space. There are also other checks available (eg. if some host name can be resolved) but they would delay load if it cannot be resolved (timeouts).

Note also that there is just one address returned which might be a problem if you have multiple addresses. Firefox has some bugs to return loopback addresses in some cases.

Obviously you may need to have more advanced rules, especially within proxy zone (eg. to provide direct access to intranet sites). There are number of options to do that. Some additional information for PAC can be found here:

Lets start

Finally I start my blog. I mainly plan to archive here some tips or small how-tos that required a bit of knowledge acquisition. I hope that not only I will benefit from that work.