Appendix B: Auto-Proxy Configuration Script Examples

The following ten scripts provide JavaScript or Microsoft® JScript® examples of automatic proxy configuration (.pac) files. As described in Using Automatic Configuration, Automatic Proxy, and Automatic Detection, you can use a .pac file to configure proxies automatically, based on URL requests from your users' browsers.

Each script consists of a function FindProxyForURL(<url>, <host*>), where <url>* is the full URL being accessed and <host*>* refers to the host name extracted from the URL. To use these scripts, you must change the placeholder proxy server names, port numbers (typically 80), and IP addresses to refer to the actual names, numbers, and addresses in your organization.

Note

The isInNet, isResolvable, and dnsResolve functions used in these scripts query a Domain Name System (DNS) server.

Following is the list of example scripts:

  • Example 1: Local hosts connect directly, all others connect using a proxy

  • Example 2: Hosts inside the firewall connect directly, outside local servers connect using a proxy

  • Example 3: If host is resolvable, connect directly—otherwise, connect using a proxy

  • Example 4: If host is in specified subnet, connect directly—otherwise, connect using a proxy

  • Example 5: Determine connection type based on host domain

  • Example 6: Determine connection type based on protocol being used

  • Example 7: Determine proxy setting by checking to see if host name matches IP address

  • Example 8: If host IP matches specified IP, connect using a proxy—otherwise, connect directly

  • Example 9: If there are any dots in the host name, connect using a proxy—otherwise, connect directly

  • Example 10: Specify days of the week to connect using a proxy—other days, connect directly

Example 1: Local hosts connect directly, all others connect using a proxy

The following function checks whether the host name is a local host, and if it is, whether the connection is direct. If the host name is not a local host, the connection is made through a proxy server (<proxy*>*).

function FindProxyForURL(url, host)

{

if (isPlainHostName(host))

return "DIRECT";

else

return "PROXY proxy:80";

}

The isPlainHostName function checks whether there are any dots (periods) in the host name. If there are, it returns false; otherwise, it returns true.

Example 2: Hosts inside the firewall connect directly, outside local servers connect using a proxy

The following function checks whether the host name is a "plain" host name (it does not contain the domain name) or part of a particular domain (such as <.company.com*>*), and it does not contain the text "www" or "home."

function FindProxyForURL(url, host)

{

if ((isPlainHostName(host) ||

dnsDomainIs(host, ".company.com")) &&

!localHostOrDomainIs(host, "www.company.com") &&

!localHostOrDomainIs(host, "home.company.com"))

return "DIRECT";

else return "PROXY proxy:80";

}

Note

The localHostOrDomainIs function is run only for URLs in the local domain. The dnsDomainIs function returns true if the domain of the host name matches the domain given.

Example 3: If host is resolvable, connect directly—otherwise, connect using a proxy

The following function requests the DNS server to resolve the host name passed to it. If it can resolve the name, then a direct connection is made. If it cannot, the connection is made using <proxy*>*. This is useful when an internal DNS server is used to resolve all internal host names.

function FindProxyForURL(url, host)

{

if (isResolvable(host))

return "DIRECT";

else

return "PROXY proxy:80";

}

Note

The isResolvable function used in this script queries a DNS server.

Example 4: If the host is in the specified subnet, connect directly—otherwise, connect using a proxy

The following function compares a given IP address pattern and mask with the host name. This is useful if certain hosts in a subnet should be connected directly and others should be connected using a proxy.

function FindProxyForURL(url, host)

{

if (isInNet(host, "999.99.9.9", "255.0.255.0"))

return "DIRECT";

else

return "PROXY proxy:80";

}

The isInNet(<host>, <pattern>, <mask*>) function returns true if the <host>* IP address matches the specified <pattern*>* (such as <999.99.9.9*>). The <mask>* indicates which part of the IP address to match (255=match, 0=ignore).

Note

The isInNet function used in this script queries a DNS server.

Example 5: Determine connection type based on host domain

The following function specifies a direct connection if the host is local. If the host is not local, this function determines which proxy to use (<comproxy>, <eduproxy>, or <proxy>) based on the host domain. This is useful if the host domain name is one of the criteria for proxy selection.

function FindProxyForURL(url, host)

{

if (isPlainHostName(host))

return "DIRECT";

else if (shExpMatch(host, "*.com"))

return "PROXY comproxy:80";

else if (shExpMatch(host, "*.edu"))

return "PROXY eduproxy:80";

else

return "PROXY proxy";

}

The shExpMatch(<str>, <shexp*>*) function returns true if <str> matches the <shexp> shell expression pattern.

Example 6: Determine connection type based on protocol being used

The following function extracts the protocol being used and makes a proxy server selection (<proxy>, <fproxy>, <gproxy>, or <secproxy*>*) accordingly. If no match is made for the protocol, a direct connection is made. This function is useful if the protocol being used is one of the criteria for proxy selection.

function FindProxyForURL(url, host)

{

if (url.substring(0, 5) == "http:") {

return "PROXY proxy:80";

}

else if (url.substring(0, 4) == "ftp:") {

return "PROXY fproxy:80";

}

else if (url.substring(0, 7) == "gopher:") {

return "PROXY gproxy";

}

else if (url.substring(0, 6) == "https:") {

return "PROXY secproxy:8080";

}

else {

return "DIRECT";

}

}

The substring function in this example extracts the specified number of characters from the beginning of a string.

Example 7: Determine proxy setting by checking to see if host name matches the IP address

The following function selects a proxy server by translating the host name into an IP address, and comparing it to a specified string (such as <999.99.99.999*>*).

function FindProxyForURL(url, host)

{

if (dnsResolve(host) == "999.99.99.999") { // = http://secproxy

return "PROXY secproxy:8080";

}

else {

return "PROXY proxy:80";

}

}

Note

The dnsResolve function used in this script queries a DNS server.

Example 8: If the host IP matches specified IP, connect using a proxy—otherwise, connect directly

The following function is another way to select a proxy based on specifying an IP address.

function FindProxyForURL(url, host)

{

if (myIpAddress() == "999.99.999.99") {

return "PROXY proxy:80"; }

else {

return "DIRECT";

}

}

The myIpAddress function returns the IP address (in integer-dot format) of the host that the browser is running on. This example, unlike Example 7, uses the function call to get the numeric IP address explicitly. (Example 7 uses dnsResolve to translate the host name into the numeric IP address.)

Example 9: If there are any dots in the host name, connect using a proxy—otherwise, connect directly

The following function checks to see how many dots (periods) are in the host name. If there are any dots in the host name, the browser will make a connection using <proxy>. If there are no dots in the host name, it will make a direct connection. This is another way to determine connection types based on host name characteristics.

function FindProxyForURL(url, host)

{

if (dnsDomainLevels(host) > 0) { // if number of dots in host > 0

return "PROXY proxy:80";

}

return "DIRECT";

}

The dnsDomainLevels function returns an integer equal to the number of dots in the host name.

Example 10: Specify days of the week to connect through a proxy server—on other days, connect directly

The following function determines the connection type by specifying days of the week when the browser uses a proxy server (<proxy>). On days that do not fall within the range, the browser makes a direct connection. This function can be useful in situations when you want to use a proxy when traffic is heavy, and then allow a direct connection when traffic is light.

function FindProxyForURL(url, host)

{

if(weekdayRange("WED", "SAT", "GMT"))

return "PROXY proxy:80";

else

return "DIRECT";

}

The weekdayRange( <day1> [, <day2>] [,"GMT"] ) function returns true if the current system time falls within the range specified by the parameters <day1> and optionally, <day2*>*. The optional GMT parameter specifies that times are in Greenwich Mean Time rather than in the local time zone.