Proxy auto-config 3979847 223504978 2008-07-04T10:38:39Z 87.194.121.112 The '''proxy auto-config''' file defines how [[web browser]]s and other [[user agent]]s can automatically choose the appropriate [[proxy server]] (access method) for fetching a given [[Uniform Resource Locator|URL]]. A '''PAC file''' contains a [[JavaScript]] function "FindProxyForURL(url, host)". This function returns a string with one or more access method specifications. These specifications cause the user agent to use a particular [[proxy server]] or to connect directly. Multiple specifications provide a fallback when a proxy fails to respond. The browser fetches this PAC file before retrieving other pages. The URL of the PAC file is either configured manually or determined automatically by the [[Web Proxy Autodiscovery Protocol]]. == Context == Modern web browsers implement several levels of automation; you can choose the level that is appropriate to your needs. The following methods are commonly implemented: * Manual proxy selection: Specify a hostname and a port number to be used for all URLs. Most browsers allow you to specify a list of domains (such as localhost) that will bypass this proxy. * Proxy auto-configuration (PAC): Specify the URL for a PAC file with a JavaScript function that determines the appropriate proxy for each URL. This method is more suitable for laptop users who need several different proxy configurations, or complex corporate setups with many different proxies, and is discussed in this article. * [[Web Proxy Autodiscovery Protocol]] (WPAD): Let the browser guess the location of the PAC file through [[DHCP]] and [[Domain name system|DNS]] lookups. This is discussed in a separate article. == The PAC File == To use PAC, you publish a PAC file on a Web server and instruct a user agent to utilize it, either by entering the URL in the proxy connection settings of your browser or through the use of the WPAD protocol. A PAC file is a text file that defines at least one JavaScript function, '''FindProxyForURL(url, host)'''. By convention, this file is normally named '''proxy.pac'''. The WPAD standard uses '''wpad.dat'''. Even though most clients will process the script regardless of the MIME type returned in the HTTP request, for the sake of completeness and to maximize compatibility, you should instruct your web server to declare the MIME type of this file to be either '''application/x-ns-proxy-autoconfig''' or '''application/x-javascript-config'''. There is little evidence to favor the use of one MIME type over the other. It would be, however, reasonable to assume that '''application/x-ns-proxy-autoconfig''' will be supported in more clients than '''application/x-javascript-config''' as it was defined in the original Netscape specification, the latter type coming in to use more recently. A very simple example of a PAC file is: function FindProxyForURL(url, host) { return "PROXY proxy.foo.com:8080; DIRECT"; } This function instructs the browser to retrieve all pages through the proxy on port 8080 of the server proxy.foo.com. Should this proxy fail to respond, the browser contacts the WWW directly, without using a proxy. Here is a more complicated example demonstrating some available JavaScript functions to be used in the FindProxyForURL function: function FindProxyForURL(url, host) { // our local URLs from the domains below foo.com don't need a proxy: if (shExpMatch(url,"*.foo.com/*")) {return "DIRECT";} if (shExpMatch(url, "*.foo.com:*/*")) {return "DIRECT";} // URLs within this network are accessed through // port 8080 on fastproxy.foo.com: if (isInNet(host, "10.0.0.0", "255.255.248.0")) { return "PROXY fastproxy.foo.com:8080"; } // All other requests go through port 8080 of proxy.foo.com. // should that fail to respond, go directly to the WWW: return "PROXY proxy.foo.com:8080; DIRECT"; } Here is a [http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html list of predefined functions available for use]. === Limitations === The function dnsResolve (and similar other functions) performs a [[Domain name system|DNS]] lookup that can block your browser for a long time if the DNS server does not respond. Caching of proxy autoconfiguration results by domain name in Microsoft's [[Internet Explorer]] 5.5 or higher limits the flexibility of the PAC standard. In effect, you can choose the proxy based on the domain name, but not on the path of the URL. Alternatively, you need to disable caching of proxy autoconfiguration results by editing the [[Windows Registry|registry]]. Refer to the article by de Boyne Pollard (listed in [[#Further_reading]]) for more details. It is recommended to always use IP instead of host domain names in isInNet function for compatibilities with other components in Windows which makes use of Internet Explorer PAC settings, such as .NET 2.0 Framework. For example, if (isInNet(host, dnsResolve(sampledomain) , "255.255.248.0") // .NET 2.0 will resolve proxy properly if (isInNet(host, sampledomain, "255.255.248.0") // .NET 2.0 will not resolve proxy properly Some versions of Apple's Safari (3.0.4) and Software Update (2.0.8) fail to connect if the PAC is unavailable. By contrast most other browsers will use direct connections in this case. Additionally, some versions of Safari retrieve the PAC file for every http request rather than caching it. This can cause slow page loads and a potential overload of the PAC web-server. Further limitations are related to the javascript engine on the local machine. Certain function calls such as ipAddress() will not work correctly on an Apple. == Further reading == {{cite web|url=http://homepages.tesco.net/~J.deBoynePollard/FGA/web-browser-auto-proxy-configuration.html|title=Automatic proxy HTTP server configuration in web browsers|work=Frequently Given Answers|author=Jonathan de Boyne Pollard|date=2004}} == External links == * [http://wp.netscape.com/eng/mozilla/2.0/relnotes/demo/proxy-live.html Lists of predefined functions available for use] * [http://code.google.com/p/pacparser pacparser] C and Python library to parse PAC files. * [http://code.google.com/p/pactester Pactester] A tool to test PAC files. * [http://luno.org/project/proxyvalidator/ proxyvalidator] Test all destination proxies within a PAC file. * [http://www.schooner.com/~loverso/no-ads/ PAC-file to filter online advertisement] * [http://www.securemecca.com/pac.html PAC-file to filter bad hosts and pornography] [[Category:Web browsers]] [[de:Proxy Auto-Config]]