09.26.06

Wrong proxy settings? Write your own .pac!

Posted in web dev, programming, mac/os x at 3:32 pm by Clinton

Moving between home and work, I sometimes have issues with the proxy settings of my web browsers.

Today, I was trying out Eclipse, and while looking at the “what-new” and other tutorial related into, I had problems. The proxy.pac file that work has was making requests for 127.0.0.1 (aka “localhost”) also go through the proxy. Now, 1) I think that’s the wrong setting - if I ask for localhost it should go straight back to me - not to the local proxy and (then be ignored). 2) yes, I could set up the manual proxy settings with the appropriate “ignore proxy for” … addresses, but… i’m a coder at heart, so what I could I do?

Write my own (local) .pac file, and tell my browser to use that of course! :)

// Clinton’s custom proxy files - works at home and at work.
// Change the work vars to suit your home/school/work needs
// If you have multiple proxy setups at home, double-up the proxy behaviour.
function FindProxyForURL(url, host)
{
    var work_ip = “136.186.0.0″;
    var work_ip_mask = “255.255.0.0″;
    var work_proxy = “PROXY 136.186.1.14:8000; DIRECT”;

    // If only hostname specified, local host so go direct
    if (isPlainHostName(host))
        return “DIRECT”;
 
    // local host is always direct 
    if(isInNet(dnsResolve(host), “127.0.0.1″,“255.255.255.255″))
        return “DIRECT”;

    // Am I at work? Check for my IP in the work ip range
    if (isInNet(myIpAddress(),work_ip,work_ip_mask))
    {
        // Connect directly to local work domains
        if (isInNet(dnsResolve(host), work_ip, work_ip_mask))
            return “DIRECT”;
 
        // Is this a protocol that needs to be sent to the proxy?
        var bits = url.split(‘:’,2); // set limit - only need 2
        var p = (bits.length==2) ? bits[0] : ‘’;
        if (p ==‘http’ || p == ‘https’ || p == ‘ftp’ || p == ‘gopher’)
            return work_proxy;
    }

    // if you get to here, you don’t get a proxy
    return “DIRECT”;
}

It’s a quick hack, it works (so far) for me, and ymmv, but if you find it useful as well, that would great. This technique could be (and has been) extended to support ‘ad’ filtering from known URLs as well. There is a shExpMatch(url, “http://ads.somedomain.com/*”) function - I was hoping for regex powers, but alas it just supports “shell expressions” and that ain’t half as fun. :)

Helpful links on PAC files:
* http://en.wikipedia.org/wiki/Proxy_auto-config
* http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html
* Firebug http://www.joehewitt.com/software/firebug/ - yes it will tell you about errors in the pac file as well - v.cool.

3 Comments

  1. sesh said,

    September 28, 2006 at 12:59 pm

    Although on my laptop I use Opera (F12, up, enter to enable and disable proxy servers) and Firefox with Foxy Proxy, this does look like a very novel idea.

    I tried testing it, however Firefox didn’t seem to like it when I had the file stored locally. I’m over at uni, so ftp’ing it to my server at home is out + I would not be able to access that without using the proxy anyway.

    Just out of curiosity - did you have the file stored locally, or hosted on a server within the university? I’m guessing it is hosted on your laptops development server or something like that so that you can access it using http://localhost/pacfile.pac, as opposed to file://C:/pacfile.pac.

  2. Clinton said,

    September 28, 2006 at 3:59 pm

    I was able to use a local file with firefox in OS X. There was no “browse” button in firefox, but I typed in the path and it worked fine - although it needed three / for file:///Uses/myname/myproxy.pac.

    It’s definitely better if you don’t have to have your local dev httpd running just for web browsing! :) (I tend to only start apache when i need it). I know that for IE on Win there are special locations where you can store a local pac file (without the pac extension) but I didn’t take note at the time strangely enough.

    I’ll be interested if this works well for others, so let me know. :)

  3. Andrew Cain said,

    September 28, 2006 at 6:13 pm

    Finally got around to doing this… all works fine for me. Thanks.