Easy Conditional Proxy Configuration

From Zanecorpwiki

Jump to: navigation, search

The Problem: you need/want to proxy some but not all your Internet connections.

The Solution: write a short JavaScript and use it as your "automatic proxy configure URL".

Firefox and other browsers support an automatic proxy configuration URL that works by providing a snippet of JavaScript which the browser then interprets before every new connection to determine if and which proxy service to use. This seems as if it originally conceived as a way for intranet based proxy services and appliances to set proxy rules without the need for user configuration, but it's a great tool for the professional developer who needs to use a proxy service to access restricted sites that avoids the need to constantly reconfigure their web browser.

The script must define FindProxyForURL(url, host) which may return one of four values:

  • 'DIRECT' : means make a direct connection to the indicated URL
  • 'PROXY xxx.xxx.xxx.xxx:yyyy' : where xxx... and yyyy are the host and port to make a proxy connection to
  • 'SOCKS xxx.xxx.xxx.xxx:yyy' : where xxx... and yyyy are the host and port where a SOCKSV4/5[notes 1] service is running.
  • a combination of one of the above separated by a ';' which provides default and fall back behavior: e.g., 'PROXY 192.168.0.10:12345; DIRECT' would attempt to make the connection through the indicated proxy and then fall back to a direct connection if that fails.

Within the script, some special functions are made available:

  • isInNet(host, network, netmask) determines whether the specific host IP or DNS is in the network indicated by the network and netmask parameters; e.g.: isInNet(host, "192.168.5.0", "255.255.255.0")
  • shExpMatch(string, pattern) checks the pattern against string with "shell" style glob matching; e.g.: shExpMatch(url, "http:*")

There are lots of details we could get into but which are beyond my purpose here.

Notes

  1. I believe SOCKS5 should work, though all the references I've seen only refer to SOCKS4. In any case, Firefox at least works with the normal SSH Proxy on Linux.
Personal tools