Tag Archives: javascript

How to use auto config Proxy PAC file for specific domain/url

Why would you need such a thing? I would like to use the squid proxy server only for one domain and connect to everything else directly. A PAC file is nothing but proxy auto-configuration file. This is a specialized JavaScript function definition that a browser calls to determine how requests are handled. So how a PAC looks like?

[codesyntax lang="javascript"]

function FindProxyForURL(url, host)
{
	     return "PROXY proxy-host:3128; DIRECT";
}

[/codesyntax]

If you want to connect to a specific domain via proxy, and directly connect to all other site:

[codesyntax lang="javascript"]

function FindProxyForURL (url, host)
{
	if (localHostOrDomainIs (host, "www.whatismyip.com"))
	{
		return "PROXY proxy-host:3128";
	}
	else
	{
		return "DIRECT";
	}
}

[/codesyntax]

What if you will attempt to match one or more URLs to a specified shell expression?

[codesyntax lang="javascript"]

function FindProxyForURL(url, host)
{
	var proxyserver = 'proxy-host:3128';

	var proxylist = new Array(
		'*.whatismyip.com/*',
		'whatismyipaddress.com/*'
	);

	for (var i = 0; i < proxylist.length; i++)
	{
		var value = proxylist[i];
		if (shExpMatch(url, value))
		{
			return "PROXY "+proxyserver;
		}
	}

	return "DIRECT";
}

[/codesyntax]