Skip to content

Instantly share code, notes, and snippets.

@ap
Created December 14, 2008 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ap/35693 to your computer and use it in GitHub Desktop.
Save ap/35693 to your computer and use it in GitHub Desktop.
Efficient ad blocking via proxy autoconfig
/* the network your computer is on, and the subnet mask of that network */
var local_network = '192.168.1.0';
var local_netmask = '255.255.255.0';
/* the host and port of your regular proxy, if any; otherwise an empty string */
var upstreamProxy = '';
/* space-separated list of hosts or domains that should always be connected to directly without using a proxy */
var directZones = 'plasmasturm.org delicious.com paulgraham.com yahoo.com';
/* space-separated list of hosts or domains that should never connected to */
var blockedZones = 'googlesyndication.com google-analytics.com snap.com ixnp.com adbrite.com doubleclick.com doubleclick.net hitbox.com 2o7.net';
/*-------------------------------------------------------------------*/
function TrimString(string) { return string.replace(/^ +| +$/,'') }
function ListFromString(list_string) { return TrimString(list_string).split(/ +/) }
local_network = TrimString(local_network);
local_netmask = TrimString(local_netmask);
upstreamProxy = TrimString(upstreamProxy);
directZones = ListFromString(directZones);
blockedZones = ListFromString(blockedZones);
function TryMatch(hosts, matcher) {
for (var i = 0; i < hosts.length; ++i)
if (matcher(hosts[i])) return true;
return false;
}
function FindProxyForURL(url, host) {
if (isInNet(host, '127.0.0.0', '255.0.0.0'))
return 'DIRECT';
var hostCheck = function(domain) { return host == domain };
var dnsZoneCheck = function(domain) { return dnsDomainIs(host, '.' + domain) };
if (isPlainHostName(host) || TryMatch(directZones, dnsZoneCheck))
return 'DIRECT';
if (TryMatch(blockedZones, hostCheck) || TryMatch(blockedZones, dnsZoneCheck))
return 'PROXY 127.0.0.1:1';
if (isInNet(host, local_network, local_netmask))
return 'DIRECT';
if (upstreamProxy)
return 'PROXY ' + upstreamProxy;
return 'DIRECT';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment