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()
- isPlainHostName()
- dnsDomainIs()
- shExpMatch()
- isInNet()
- myIpAddress()
- dnsResolve()
- isResolvable()
- dnsDomainLevels()
- localHostOrDomainIs()
- weekdayRange()
- dateRange()
- timeRange()
- alert()
- url (string) - The full URL being accessed e.g. https://example.com/index.html. The path and query components of HTTPS URLs are stripped, thus https://example.com/index.html becomes https://example.com/.
- host (string) - The hostname extracted from the URL. It is the same string as between :// and the first : or / after that. Thus host for https://example.com/index.html is: example.com. The port number is not included in this parameter. It can be extracted from the url string when necessary.
- "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.
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.
- host (string) - The domain or host name to be tested.
- true if the domain does not contain dots.
- false if the domain contains dots.
isPlainHostName("www.example.com"); // returns false isPlainHostName("intranet"); // returns trueSee this example in PAC Tester.
- host (string) - The hostname/domain name to be tested.
- domain (string) - The domain name to test the host name against.
- 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.
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.- str (string) - The string to compare (e.g. the URL, or the hostname).
- shexp (string) - A shell expression to compare against.
- true if the pattern matches.
- false if the pattern does not match.
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.- 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.
- 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.
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(); // 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.- host (string) - The domain or host name to be queried.
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.localSee this example in PAC Tester.
- host (string) - The domain or host name to be queried.
- true if the domain or host name resolves in DNS.
- false if the domain or host name do not resolve in DNS.
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.localSee this example in PAC Tester.
- host (string) - The hostname/domain name to be tested.
dnsDomainLevels("www"); // returns 0 dnsDomainLevels("example.com"); // returns 1 dnsDomainLevels("www.example.com"); // returns 2See this example in PAC Tester.
- host (string) - The hostname/domain name to be tested.
- domain (string) - The domain name to test the host name against.
- 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.
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.- 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.
- 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.
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 MondaySee this example in PAC Tester.
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])
- 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
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 1997See this example in PAC Tester.
Returns true if within the time range.
- 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.
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 midnightSee this example in PAC Tester.
dnsDomainLevels("www"); // returns 0 dnsDomainLevels("example.com"); // returns 1 dnsDomainLevels("www.example.com"); // returns 2See this example in PAC Tester. Note: Logged messages do not work in all browsers but PAC Tester will log alerts in the output box.