Skip Navigation

PAC file example

PAC files should start with a clear and concise coding methodology. You can achieve the same result using several different methods using the PAC file functions that are available and the flexibility of the 
JavaScript
 language. The following example shows how to:
  • Normalize the requested URL for pattern matching
  • Bypass the proxy when the destination is a plain hostname (a hostname that doesn't include a domain)
  • Bypass the proxy for a defined set of local domains
  • Bypass non-routable addresses (RFC 3330, better known as Special-Use IPv4 Addresses)
  • Send remaining HTTP, HTTPS, and FTP traffic to a specific proxy
function FindProxyForURL(url, host) /* Normalize the URL for pattern matching */ { url = url.toLowerCase(); host = host.toLowerCase(); /* Don't proxy local hostnames */ if (isPlainHostName(host)) { return 'DIRECT'; } /* Don't proxy local domains */ if (dnsDomainIs(host, ".example1.com") || (host == "example1.com") || dnsDomainIs(host, ".example2.com") || (host == "example2.com") || dnsDomainIs(host, ".example3.com") || (host == "example3.com")) { return 'DIRECT'; } /* 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'; } /* Don't proxy local addresses.*/ if (false) { return 'DIRECT'; } } if (url.substring(0, 5) == 'http:' || url.substring(0, 6) == 'https:' || url.substring(0, 4) == 'ftp:') { return 'PROXY xyz1.example.com:8080'; } return 'DIRECT'; }
The following example shows a simple load distribution and failover using DNS:
{ if (isInNet(myIpAddress(), "10.1.0.0", "255.255.0.0")) { return "PROXY xyz1.example.com:8080; " + "PROXY xyz2.example.com:8080"; } if (isInNet(myIpAddress(), "10.2.0.0", "255.255.0.0")) { return "PROXY xyz1.example.com:8080; " + "PROXY xyz2.example.com:8080"; } if (isInNet(myIpAddress(), "10.3.0.0", "255.255.0.0")) { return "PROXY xyz2.example.com:8080; " + "PROXY xyz1.example.com:8080"; } if (isInNet(myIpAddress(), "10.4.0.0", "255.255.0.0")) { return "PROXY xyz2.example.com:8080; " + "PROXY xyz1.example.com:8080"; } else return "DIRECT"; }
The following example (new in version 2.9) shows how to specify URLs to open in the native browser and URLs to block:
function FindProxyForURL(url, host) { if (shExpMatch (url, "*example.org*")){ return "PROXY example.net:8080; PROXY :3128"; } if (dnsDomainIs (host, "blackberry.com")){ return "NATIVE"; } if (dnsDomainIs (host, ".example.com")){ return "BLOCK"; } //redirect on http page if (shExpMatch (url, "*domain123.example.net*")){ return "BLOCK http://domain1.example.org/"; } return "DIRECT"; }