Proxy PAC or WPAD Functions

Overview

A Proxy Auto-Configuration (PAC) file is a JavaScript function that determines whether web browser requests (HTTP, HTTPS, and FTP) go directly to the destination or are forwarded via a proxy server. The JavaScript function contained in the PAC file defines the function: FindProxyForUrl(url, host)

Support for IPv6 is implemented in Extended functions in Internet Explorer but not widely supported by browsers yet.

To test your PAC script, try my PAC Tester.

Functions

Proxy PAC files support the following functions:
FindProxyForUrl(url, host)
Description
This function must be defined in your PAC file. It returns a string describing the proxies to be used to the browser. The format of this string is defined above. If there are multiple semicolon-separated settings, the left-most setting will be used, until the browser fails to establish a connection to the proxy. In that case, the next value will be used, etc.
Parameters
Note: The path and query components of HTTPS URLs are by stripped default in most modern browsers. In Chrome path stripping can't be disabled. In Firefox, path stripping can be disabled with the preference network.proxy.autoconfig_url.include_path.
Returns
Returns a string with semicolon separated list of proxies to use. The string can contain any number of the following building blocks, separated by a semicolon:
  • "DIRECT" = Connections should be made directly, without any proxies.
  • "PROXY host:port" = The specified proxy should be used.
  • "SOCKS host:port" = The specified SOCKS server should be used.
Examples
function FindProxyForURL(url, host)
{
    // Send all plain DNS entries (e.g. intranet) and everything in mydomain.local DIRECT.
    if (isPlainHostName(host) ||
        dnsDomainIs(host, ".mydomain.local") ||
        host == "mydomain.local")
        {
            return "DIRECT";
        }

    // Otherwise use the proxies. All traffic uses proxy1 unless it's down.
    return "PROXY proxy1.mydomain.local:8080; proxy2.mydomain.local:8080";
}
See this example in PAC Tester.
isPlainHostName(host)
Description
Returns true if the host name does NOT contains dots (full stops).
Parameters
  • host (string) - The domain or host name to be tested.
Returns
Boolean
  • true if the domain does not contain dots.
  • false if the domain contains dots.
Examples
isPlainHostName("www.example.com"); // returns false
isPlainHostName("intranet");        // returns true
See this example in PAC Tester.
dnsDomainIs(host, domain)
Description
Returns true if the host name is a child domain of the specified domain.
Parameters
  • host (string) - The hostname/domain name to be tested.
  • domain (string) - The domain name to test the host name against.

Returns
Boolean
  • true if the host name is a child domain of the specified domain.
  • false if the host name is a not child domain of the specified domain.
Examples
dnsDomainIs("www.example.com", ".example.com"); // returns true
dnsDomainIs("intranet", ".example.com");        // returns false
dnsDomainIs("blahexample.com", "example.com");  // returns true

Note: The domain should be specified with a leading dot, for the third example to return false.

See this example in PAC Tester.
shExpMatch(str, shexp)
Description
Returns true if the string matches the specified shell expression.
Parameters
  • str (string) - The string to compare (e.g. the URL, or the hostname).
  • shexp (string) - A shell expression to compare against.
Returns
Boolean
  • true if the pattern matches.
  • false if the pattern does not match.
Examples
shExpMatch("https://www.example.com/path1/index.html", "*/path1/*"); // returns true
shExpMatch("https://www.example.com/index.html", "*/path/*");        // returns false
shExpMatch("https://www.example.com/index.html", "https://*");       // returns true
shExpMatch("https://google.com",                 "(*.|)google.com")  // returns true
shExpMatch("https://www.google.com",             "(*.|)google.com")  // returns true

Note: * (match any number of characters) and ? (match one character) are supported by all browsers. Braces can be used to create a group. Groups are useful if you are using the or "|" expression - see example 5.

Note: The path and query components of https:// URLs are by stripped default in most modern browsers. This means example 1 is likely to only work for http.

See this example in PAC Tester.
isInNet(host, subnet, mask)
Description
Returns true if the host name is a child domain of the specified domain.
Warning: DNS resolution can impact performance. If DNS resolution is needed, try and limit its use by proceeding it with dnsDomainIs() - see example 4.
Parameters
  • host (string) - The IPv4 address, domain or host name to be tested.
  • subnet (string) - The IPv4 subnet to test the host against.
  • mask (string) - The IPv4 subnet mask.
Returns
Boolean
  • true if the domain, host name or IP address are in the subnet.
  • false if the domain, host name or IP address are not in the subnet.
Examples
isInNet("192.168.1.10", "192.168.1.0", "255.255.255.0"); // returns true
isInNet("intranet",     "192.168.1.0", "255.255.255.0"); // returns true if dnsResolve("intranet") returns an IP address in the specified subnet.
isInNet("8.8.8.8",      "192.168.1.0", "255.255.255.0"); // returns false
(dnsDomainIs(host, ".mydomain.local") && isInNet(host, "192.168.1.0", "255.255.255.0"));
// only attempt the dnsResolve if host is in .mydomain.local

Note: If the host does not match the pattern for an IPv4 address, isInNet does a DNS lookup first.

See this example in PAC Tester.
myIpAddress()
Description
Returns a single IPv4 address of the device running the Browser, as a string in the dot-separated integer format.
Parameters
nil.
Returns
Returns a string with a single IPv4 address.
Examples
myIpAddress(); // returns "192.168.1.100"

Note: On Linux myIpAddress() returns the same IP address as the address returned by nslookup localhost.

See this example in PAC Tester.
dnsResolve(host)
Description
Resolves the given host name into an IP address. Returns a single IPv4 address in the dot-separated integer format as a string.
Warning: DNS resolution can impact performance, it is best to avoid this function. If DNS resolution is needed, try and limit its use by proceeding it with dnsDomainIs() - see example 2.
Parameters
  • host (string) - The domain or host name to be queried.
Returns
Returns a string with a single IPv4 address.
Examples
dnsResolve("dns.google"); // returns "8.8.8.8"
(dnsDomainIs(host, ".mydomain.local") && dnsResolve(host) == "192.168.10.10");
// only attempt the dnsResolve if host is in .mydomain.local
See this example in PAC Tester.
isResolvable(host)
Description
Returns true if the host name resolves to an IPv4 address.
Warning: DNS resolution can impact performance, it is best to avoid this function. If DNS resolution is needed, try and limit its use by proceeding it with dnsDomainIs() - see example 3.
Parameters
  • host (string) - The domain or host name to be queried.
Returns
Boolean
  • true if the domain or host name resolves in DNS.
  • false if the domain or host name do not resolve in DNS.
Examples
isResolvable("www.google.com"); // returns true
isResolvable("non.existent.domain"); // returns false
(dnsDomainIs(host, ".mydomain.local") && isResolvable(host));
// only attempt the isResolvable if host is in .mydomain.local
See this example in PAC Tester.
dnsDomainLevels(host)
Description
Returns the number of domain levels (number of dots) in the domain/host name.
Parameters
  • host (string) - The hostname/domain name to be tested.
Returns
Returns an Integer with the number of dots in the domain/host name.
Examples
dnsDomainLevels("www");             // returns 0
dnsDomainLevels("example.com");     // returns 1
dnsDomainLevels("www.example.com"); // returns 2
See this example in PAC Tester.
localHostOrDomainIs(host, domain)
Description
Returns true if the host name matches exactly the specified domain, or if there beginning of the domain matches the domain name.
Parameters
  • host (string) - The hostname/domain name to be tested.
  • domain (string) - The domain name to test the host name against.
Returns
Boolean
  • true if the host name matches exactly the specified domain.
  • true if there beginning of the host name matches the specified domain
  • false if there are no matches.
Examples
localHostOrDomainIs("www.example.com"   , "www.example.com"); // returns true (exact match)
localHostOrDomainIs("www"               , "www.example.com"); // returns true (host name match)
localHostOrDomainIs("www.google.com"    , "www.example.com"); // returns false (domain name mismatch)
localHostOrDomainIs("mobile.example.com", "www.example.com"); // returns false (host name mismatch)
localHostOrDomainIs("www.example"       , "www.example.com"); // returns true (host name match)

Note: Example 5 is likely not expected, but will return true as the logic matches the beginning of the strings.

See this example in PAC Tester.
weekdayRange(wd1, [wd2], [gmt])
Description
Returns true if within the day range.
Parameters
  • wd1 (string) - One of the weekday strings: "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT".
  • wd2 (string) - (optional) One of the weekday strings: "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT".
  • gmt (string) - (optional) Is either the string "GMT", which makes time comparison occur in GMT timezone, or is left out. If left unspecified, times are taken to be in the local timezone.
Note: If the string "GMT" is specified as a second or third parameter, times are taken to be in GMT.
Returns
Boolean
  • If only one weekday parameter is present, the function returns a value of true on the weekday that the parameter represents.
  • If both wd1 and wd2 are defined, the condition is true if the current weekday is in between those two ordered weekdays. Bounds are inclusive.
Examples
weekdayRange("MON", "FRI");        // returns true Monday through Friday (local timezone)
weekdayRange("MON", "FRI", "GMT"); // returns true Monday through Friday (GMT timezone)
weekdayRange("SAT");               // returns true on Saturdays local time
weekdayRange("SAT", "GMT");        // returns true on Saturdays GMT time
weekdayRange("FRI", "MON");        // returns true Friday through Monday
See this example in PAC Tester.
dateRange(<day> | <month> | <year>, <day> | <month> | <year>, [gmt])
Description

Returns true if within the date range.

dateRange(<day1>, <day2>, [gmt])
dateRange(<month1>, <month2>, [gmt])
dateRange(<year1>, <year2>, [gmt])
dateRange(<day1>, <month1>, <day2>, <month2>, [gmt])
dateRange(<month1>, <year1>, <month2>, <year2>, [gmt])
dateRange(<day1>, <month1>, <year1>, <day2>, <month2>, <year2>, [gmt])
Parameters
  • day (integer) - The day of the month between 1 and 31.
  • month (string) - One of the month strings: "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC".
  • year (integer) - The full year number. For example, 2016 (not 16).
  • gmt (string) - Either the string "GMT" for GMT timezone, or not specified, for local timezone
Returns
Boolean. If only a single value is specified (from each category: day, month, year), the function returns a true value only on days that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.
Examples
dateRange(1);            // returns true on the first day of each month, local timezone
dateRange(1, "GMT");     // returns true on the first day of each month, GMT timezone
dateRange(1, 15);        // returns true on the first half of each month
dateRange(24, "DEC");    // returns true on 24th of December each year
dateRange("JAN", "MAR"); // returns true on the first quarter of the year

dateRange(1, "JUN", 15, "AUG"); // returns true from June 1st until August 15th, each year
// (including June 1st and August 15th)

dateRange(1, "JUN", 1995, 15, "AUG", 1995); // returns true from June 1st, 1995, until August 15th, same year

dateRange("OCT", 1995, "MAR", 1996); // returns true from October 1995 until March 1996
// (including the entire month of October 1995 and March 1996)

dateRange(1995); // returns true during the entire year of 1995

dateRange(1995, 1997); // returns true from beginning of year 1995 until the end of year 1997
See this example in PAC Tester.
timeRange(<hour1>, <min1>, <sec1>, <hour2>, <min2>, <sec2>, [gmt])
Description

Returns true if within the time range.

Parameters
  • hour (integer) - Hour from 0 to 23. (0 is midnight, 23 is 11 pm.)
  • min (integer) - Minutes from 0 to 59.
  • sec (integer) - Seconds from 0 to 59.
  • gmt (string) - Either the string "GMT" for GMT timezone, or not specified, for local timezone.
Returns
Boolean. If only a single value is specified (from each category: hour, minute, second), the function returns a true value only at times that match that specification. If both values are specified, the result is true between those times, including bounds, but the bounds are ordered.
Examples
timeRange(12);                // returns true from noon to 1pm
timeRange(12, 13);            // returns true from noon to 1:59pm
timeRange(12, "GMT");         // returns true from noon to 1pm, in GMT timezone
timeRange(9, 17);             // returns true from 9am to 5:59pm
timeRange(8, 30, 17, 0);     // returns true from 8:30am to 5:00pm
timeRange(0, 0, 0, 0, 0, 30); // returns true between midnight and 30 seconds past midnight
See this example in PAC Tester.
alert(message)
Parameters
message (string) - The string to log.
Returns
Nil
Description
Logs the message in the browser console.
Examples
dnsDomainLevels("www");             // returns 0
dnsDomainLevels("example.com");     // returns 1
dnsDomainLevels("www.example.com"); // returns 2
See this example in PAC Tester. Note: Logged messages do not work in all browsers but PAC Tester will log alerts in the output box.