Pac File

🔧

So I run my own squid proxy service on my Synology NAS. While it easy to associate a given proxy setting with an wifi network for both iOS and Android devices, Mac OS X has this semi-working notion of network location, which would make a lot of sense it the location was determined by the wifi network or the physical location. But it does not. So how to configure the laptop to talk to my proxy when at home and not if somewhere else?

The solution I found is to write a custom PAC file which basically uses my proxy if its name can be resolved. The file is standard fare, with just one added line to check that the proxy can be resolved. One important thing is to not configure the pac file as a local file: this will not work, as Safari will be unable to load it from the sandbox, the simplest way I found is to serve it from the local web-server on the machine, i.e. http://127.0.0.1/home.pac.

function FindProxyForURL(url, host) {
  url = url.toLowerCase();
  host = host.toLowerCase();
  /* Don't proxy local hostnames */
  if (isPlainHostName(host)) {
    return 'DIRECT';
  }
  var hostIP = dnsResolve(host);
  /* Don't proxy non-routable addresses (RFC 3330) */
  if (isInNet(hostIP, '0.0.0.0', '255.0.0.0') ||
      isInNet(hostIP, '10.0.0.0', '255.0.0.0') ||
      isInNet(hostIP, '127.0.0.0', '255.0.0.0') ||
      isInNet(hostIP, '169.254.0.0', '255.255.0.0') ||
      isInNet(hostIP, '172.16.0.0', '255.240.0.0') ||
      isInNet(hostIP, '192.0.2.0', '255.255.255.0') ||
      isInNet(hostIP, '192.88.99.0', '255.255.255.0') ||
      isInNet(hostIP, '192.168.0.0', '255.255.0.0') ||
      isInNet(hostIP, '198.18.0.0', '255.254.0.0') ||
      isInNet(hostIP, '224.0.0.0', '240.0.0.0') ||
      isInNet(hostIP, '240.0.0.0', '240.0.0.0')) {
    return 'DIRECT';
  }
  if (isResolvable('tekai.local')) {
    return 'PROXY tekai.local:3128';
  }
  return 'DIRECT';
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.