randomfox (randomfox) wrote,
randomfox
randomfox

Perl script to convert an Adblock filter to a PAC filter

The following script takes an Adblock filter from the FilterSet.G archive and converts it to a PAC file.

#!perl -w
use strict;
use Getopt::Long;

# Converts Adblock filter to a PAC file.
# Usage: adb2pac.pl < adblock > adfilter.pac

# Filter toggle based on John's no-Ads PAC script
# http://www.schooner.com/~loverso/no-ads/
# To disable, visit this URL:		http://no-ads.int/off
# To re-enable, visit this URL:		http://no-ads.int/on

# Google Web Accelerator can be enabled or disabled dynamically.
# To disable, visit this URL:		http://gwa.int/off
# To enable, visit this URL:		http://gwa.int/on
#
# 2005-09-06: Added wunderground to whitelist. Turn GWA off by default.
# Added -gwa option to enable.
#
# 2005-06-10: Added flickr to whitelist.
#
# 2005-05-13: Added a way to enable/disable GWA dynamically.
#
# 2005-05-11: Added built-in exception list for GWA.
#
# 2005-05-06: Added support for Google Web Accelerator. To disable, use
# the -nogwa option.
 
my @regexps;

my $normal = "DIRECT";
my $gwaproxy = "PROXY 127.0.0.1:9100";
my $blackhole = "PROXY 127.0.0.1:3421";

my $normproxy = "normalProxy(url)";

my $gwadefault = 0;

{
    sub usage {
	die <<EOM;
    $0 [-nogwa] [-gwa] < adblockfile > adfilter.pac

    Options:
	-help -?: Show help.
	-nogwa: Disable Google Web Accelerator by default.
	-gwa: Enable Google Web Accelerator by default.
EOM
    }

    my $opt_help;
    my $opt_gwa;
    my $opt_nogwa;
    GetOptions(
	"help|?" => \$opt_help,
	"gwa" => \$opt_gwa, 
	"nogwa" => \$opt_nogwa) or usage;
    $opt_help and usage;
    $opt_nogwa and $gwadefault = 0;
    $opt_gwa and $gwadefault = 1;
}

while (<>) {
    chomp;
    next if /^\[Adblock\]/;
    next if /^!/;
    next if /^$/;

    if (/^\/(.*)\/$/) {
	my $regex = $1;
	$regex =~ s/\//\\\//g;
	push @regexps, "/$regex/i";
    }
    else {
	s/\./\\\./g;
	s/\//\\\//g;
	push @regexps, "/$_/i";
    }
}

my $vimcmd = "vim:set ft=javascript tw=0:";

print <<EOM;
var regexps = [
@{[ join(",\n", @regexps) ]}
];

var whitelist = [
/geocaching\\.com/i,
/groundspeak\\.com/i,
/wunderground\\.com/i,
/wheresgeorge\\.com/i,
/flickr\\.com/i,
/jibjab\\.com/i,
/mypoints\\.com/i,
/schooner\\.com/i
];

var gwaexcept = [
/flickr\\.com/i,
/geosnapper\\.com/i
];

var isActive = 1;

var adFilterOn = /no-ads.int\\\/on/i;
var adFilterOff = /no-ads.int\\\/off/i;

var gwa = $gwadefault;

var gwaOn = /gwa.int\\\/on/i;
var gwaOff = /gwa.int\\\/off/i;

function normalProxy(url) {
    if (!gwa)
	return "$normal";

    for (var i = 0; i < gwaexcept.length; i++) {
	if (gwaexcept[i].test(url))
	    return "$normal";
    }

    if (url.substr(0, 5).toLowerCase() == "http:")
	return "$gwaproxy";

    return "$normal";
}

function FindProxyForURL(url, host) {
    if (adFilterOn.test(url)) {
	isActive = 1;
	return "$blackhole";
    }
    else if (adFilterOff.test(url)) {
	isActive = 0;
	return "$blackhole";
    }
    else if (gwaOn.test(url)) {
	gwa = 1;
	return "$blackhole";
    }
    else if (gwaOff.test(url)) {
	gwa = 0;
	return "$blackhole";
    }

    if (!isActive) {
	return $normproxy;
    }

    for (var i = 0; i < whitelist.length; i++) {
	if (whitelist[i].test(url))
	    return $normproxy;
    }

    for (var i = 0; i < regexps.length; i++) {
	if (regexps[i].test(url))
	    return "$blackhole";
    }

    return $normproxy;
}

// $vimcmd
EOM

__END__

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments