Print
Parent Category: Tutorials
Hits: 8291

Recent Web browsers have the ability to automatically detect a web proxy to use. This tutorial shows you how to set up automatic proxy detection.
History

The first automatic detection system, called PAC, was in Netscape Navigator. This allowed you to specify a javascript script which told the browser which proxy to use. This provided a central position from which to change proxy servers.

This still had a slight disadvantage in that you needed to specify the script location. If you needed to change the lcoation you still had to visit all your clients. The solution for this is WPAD (Web Proxy AutoDiscovery). This is a set of methods for finding the PAC script to be tried in order.
Proxy Auto Configuration (PAC)

The involves creating a javascript script to return which proxy servers to use. The script needs to define the function which will be called by the browser for every URL that is retrieved:
string FindProxyForURL stringurl stringhostname
url

The full URL being accessed.
host

The hostname extracted from the URL. This is only for convenience, it is the exact same string as between :// and the first : or / after that. The port number is not included in this parameter. It can be extracted from the URL when necessary.
return value

A string describing the configuration.

The return value of the function should be a semicolon seperated list of options from the following list:
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.

A null string is the same as DIRECT. Each option will be tried in turn until one is useable.
Basic proxy.pac

You can download the source for this example at http://www.davidpashley.com/articles/proxy.pac
function FindProxyForURL(url, host)
{
if (isInNet(host, "10.0.0.1", "255.0.0.0")) {
return "DIRECT";
} else {
if (shExpMatch(url, "http:*"))
return "PROXY squid.domain.com:3128" ;
if (shExpMatch(url, "https:*"))
return "PROXY squid.domain.com:3128" ;
if (shExpMatch(url, "ftp:*"))
return "PROXY squid.domain.com:3128" ;
return "DIRECT";
}
}

You need to place this on a web server accessable from your clients. The file should have a .pac extension (e.g. proxy.pac) and should just contain the javascript and not embedded in HTML. You will also need to configure your webserver to map the .pac filename extension to the application/x-ns-proxy-autoconfig MIME type. If you are using the Apache webserver put the following line in your httpd.conf
AddType application/x-ns-proxy-autoconfig .pac

Note:

We use Hosting and VPS Hosting, from: www.star-host.org

We like and trust them.

Good prices, high security.