2

It is necessary for several of our laptops to operate on both our own and customer-based networks. This often involves switching proxy settings, which we did via a custom proxy.pac file at first (after some help from you here). Now, one of the networks we use has their own proxy.pac that we must use which means that currently, we have to manually change our proxy.pac location for that network.

So, is there a way to write a proxy.pac such that it defers to another proxy.pac in some circumstances?

3 Answers 3

6

No, not within a static .pac file. The problem would be that you would end up redefining the FindProxyForURL() function when the second file was loaded which would set off the javascript interpreter.

You would need to develop a dynamic page (ASP, PHP, etc) that would implement the required logic and then, based on the decision, read in the contents of the desired .pac file and serve it back to the client. I used to do this to build in some dynamic load balancing between proxies that I had in different physical locations and it worked well for me.

If you choose to go this route, make sure that the script you write sets the return mime type to application/x-ns-proxy-autoconfig or application/x-javascript-config, otherwise your browser will not recognize it as a pac file.

3
  • Thanks, I suspected that it might be complicated. In order for this to work, the dynamic page would have to run locally, otherwise, how could the proxy be properly configured to reach it if it was needed to properly configure the proxy? head explodes
    – Jeff Yates
    Aug 24, 2009 at 17:33
  • Yeah, if you don't have concurrent connections to both networks you're at a bit of a loss... If you're using Firefox there are some add-ins that will let you switch between mutliple configurations. Other than that I think you're about stuck.
    – squillman
    Aug 24, 2009 at 17:44
  • Thanks for the help. Really appreciated. I'll post an update if I get something working.
    – Jeff Yates
    Aug 24, 2009 at 18:09
1

After getting annoyed with IE for not having a quick and easy way to turn proxy settings off, this question inspired me to write this PAC file in PHP.

It determines whether a request for the file is made from internally (in which case, use the proxy server) or externally: go direct. Perhaps someone can adjust it for their own needs.

<?php
    $proxy = "192.168.0.x";
    $port  = "8080";

    header ("Content-type: application/x-ns-proxy-autoconfig");
    header ("Date: " . gmdate('D, d M Y H:i:s \G\M\T', time ()));
    header ("Last-Modified: " . gmdate('D, d M Y H:i:s \G\M\T', time ()));
    header ("Expires: " . gmdate('D, d M Y H:i:s \G\M\T', time () + 60 * 30));

    echo "// Request from: " . $_SERVER ['REMOTE_ADDR'] . "\n";

    if (substr ($_SERVER ['REMOTE_ADDR'], 0, 8) == "192.168.")
    {
?>
// We're inside the firewall
var external = false;
<?php
    }
    else
    {
?>
// We're outside the firewall
var external = true;
<?php
    }
?>

// Default connection
var direct = "DIRECT";

// Alternate Proxy Server
var proxy = "PROXY <?php echo $proxy; ?>:<?php echo $port; ?>";

//
// Proxy Logic
//

function FindProxyForURL(url, host)
{
    if (external || isPlainHostName(host) || shExpMatch(host, "192.168.*") || (dnsDomainIs(host, ".example.com")) && !localHostOrDomainIs(host, "www.example.com") || dnsDomainIs (host, ".example2.com"))
    {
        return direct;
    }
    else
    {
        return proxy;
    }
}

I have now adjusted my Group Policy settings so that all browsers will point to this file which is located on a webserver within the firewall. Now if laptops go offsite, their proxy file will change automatically without the need to reconfigure the browser's proxy setting.

Edit: updated to match local IP address hosts also...

0

You might be able to use WPAD (web proxy auto discovery) to distribute different proxy.pac files in different networks.

But you'll need a simple webserver in each of the networks and the search domain provided by dhcp must be different in each of the networks. If each webserver is hosting a different proxy.pac file under the URL http://wpad[.dhcp-search-domain.com]/wpad.dat, then all you need to do is set "autodiscover" in your browser's proxy settings and restart the browser each time you switch between networks.

If you are really lucky, your customer already has a working proxy autodiscovery and you just missed it, due to your static proxy.pac config...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .