-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind_refresh_old.pl
More file actions
188 lines (142 loc) · 5.5 KB
/
Copy pathbind_refresh_old.pl
File metadata and controls
188 lines (142 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/perl
use strict;
use Proc::Simple;
use Proc::Killall;
use Time::localtime;
use LWP::Simple qw($ua getstore);
$ua->agent("");
use Mozilla::CA;
#use Data::Dumper;
my $adblock_stack = [
#pgl.yoyo exclusion list
{ url => 'http://pgl.yoyo.org/adservers/serverlist.php?hostformat=adblockplus&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext',
path => '/var/named/pgl-adblock.txt',
refresh => 7,
},
#adblockplus.org tracker list
{ url => "abp:subscribe?location=https%3A%2F%2Feasylist-downloads.adblockplus.org%2Feasyprivacy.txt&title=EasyPrivacy&requiresLocation=https%3A%2F%2Feasylist-downloads.adblockplus.org%2Feasylist.txt&requiresTitle=EasyList",
path => '/var/named/easyprivacy.txt',
refresh => 5,
},
# Add additional hashrefs if you like--script will accept standard or abp:subscribe? urls.
# A collection of lists is available at http://adblockplus.org/en/subscriptions.
];
my %adfilter = ();
#my $blacklist = { path => '/var/named/blacklist' }; #single column format
#my $whitelist = { path => '/var/named/whitelist' }; #single column format
my $outfile = '/var/named/adslist.txt'; #include file for named.conf
read_config( adblock_stack => $adblock_stack, blacklist => $blacklist, whitelist => $whitelist);
open (OUT, ">$outfile") or die "Couldn't open output file: $!";
print OUT '// bind config generated ',ctime(),"\n\n";
foreach my $key (sort(keys %adfilter)) {
print OUT 'zone "',$key,'" { type master; notify no; file "null.zone.file"; };',"\n";
}
close OUT;
print "BIND ad zones updated.\n";
print "Halting BIND.\n" if killall('KILL','/usr/sbin/named');
print "Restarting BIND.\n" if $proc->start('/usr/sbin/named -4');
sub read_config {
my %cache;
if ($adblock_stack) {
for ( @{ $adblock_stack } ) {
%cache = load_adblock_filter($_); # adblock plus hosts
%adfilter = %adfilter ? ( %adfilter, %cache )
: %cache;
}
}
if ($blacklist) {
%cache = parse_single_col_hosts($blacklist->{path}); # local, custom hosts
%adfilter = %adfilter ? ( %adfilter, %cache )
: %cache;
}
if ($whitelist) {
%cache = parse_single_col_hosts($whitelist->{path}); # remove entries
for ( keys %cache ) { delete ( $adfilter{$_} ) };
}
#dump_adfilter;
}
sub load_adblock_filter {
my %cache;
my $hostsfile = $_->{path} or die "adblock {path} is undefined";
my $refresh = $_->{refresh} || 7;
my $age = -M $hostsfile || $refresh;
if ($age >= $refresh) {
my $url = $_->{url} or die "attempting to refresh $hostsfile failed as {url} is undefined";
$url =~ s/^\s*abp:subscribe\?location=//;
$url =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
$url =~ s/&.*$//;
print("refreshing hosts: $hostsfile\n");
getstore($url, $hostsfile);
}
%cache = parse_adblock_hosts($hostsfile);
return %cache;
}
sub parse_adblock_hosts {
my $hostsfile = shift;
my %hosts;
open(HOSTS, $hostsfile) or die "cant open $hostsfile file: $!";
while (<HOSTS>) {
chomp;
next unless s/^\|\|(.*)\^(\$third-party)?$/$1/; #extract adblock host
$hosts{$_}++;
}
close(HOSTS);
return %hosts;
}
sub parse_single_col_hosts {
my $hostsfile = shift;
my %hosts;
if (-e $hostsfile) {
open(HOSTS, $hostsfile) or die "cant open $hostsfile file: $!";
while (<HOSTS>) {
chomp;
next if /^\s*#/; # skip comments
next if /^$/; # skip empty lines
s/\s*#.*$//; # delete in-line comments and preceding whitespace
$hosts{$_}++;
}
close(HOSTS);
}
return %hosts;
}
sub dump_adfilter {
my $str = Dumper(\%adfilter);
open(OUT, ">/var/named/adfilter_dumpfile") or die "cant open dump file: $!";
print OUT $str;
close OUT;
}
=head1 NAME
bind_refresh.pl
=head1 DESCRIPTION
This is a maintenance script for use with I<BIND> acting as an ad blocking proxy.
Its purpose is to refresh and format domain lists into master zone definitions for
inclusion in a bind config file. It was written for osx (10.4), and manually kills
and restarts the I<named> process using I<launchd>.
The script loads externally maintained lists of ad hosts intended for use by the
I<adblock plus> Firefox extension. Use of the lists focuses only on third-party
listings that define dedicated ad/tracking hosts.
Locally maintained blacklists/whitelists can also be loaded. In this case, host
listings must conform to a one host per line format:
# ad nauseam
googlesyndication.com
facebook.com
twitter.com
...
adinfinitum.com
Script output can easily be included in your named.conf file:
...
include "/var/named/adslist.txt";
The script outputs zone definitions of the form:
zone "googlesyndication.com" { type master; notify no; file "null.zone.file"; };
where null.zone.file is a localhost definition such as:
$TTL 24h
@ IN SOA example.homeip.net. unused-email.example.homeip.net. (
2003052800 86400 300 604800 3600 )
@ IN NS ns1.example.homeip.net.
@ IN A 127.0.0.1
* IN A 127.0.0.1
=head1 AUTHOR
David Watson <terminalfool@yahoo.com>
=head1 COPYRIGHT
This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
=cut