Add support for allowlists

This commit is contained in:
Dan Church 2022-11-25 16:15:13 -06:00
parent fd8061714f
commit 1a97ec8365
Signed by: h3xx
GPG key ID: EA2BF379CD2CDBD0
3 changed files with 27 additions and 0 deletions

6
allowlists/.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
# ... And some others
!/allowlist.domains.example

View file

@ -0,0 +1,8 @@
# Omit these domains from the blocklist.
#
# Must be in a file called *.domains in this directory.
# Comments are allowed. One domain per line.
#
# Examples:
# domain1.example.com
# domain2.example.com

View file

@ -23,6 +23,7 @@ use FindBin qw//;
my %domains;
my $dupes = 0;
my $skip = 0;
my $removed_allowed = 0;
sub add_domain_list {
my $file = shift;
foreach my $line (&read_stripped($file)) {
@ -82,6 +83,7 @@ MAIN: {
my @domain_lists = glob "$workdir/lists/*.domains";
my @hosts_lists = glob "$workdir/lists/*.hosts";
my @allow_lists = glob "$workdir/allowlists/*.domains";
foreach my $listfile (@domain_lists) {
&add_domain_list($listfile);
@ -90,6 +92,16 @@ MAIN: {
&add_host_file($hostfile);
}
# Apply allowlists
my @allow_domains;
foreach my $allowlist (@allow_lists) {
push @allow_domains, &read_stripped($allowlist);
}
my $before = %domains;
delete %domains{@allow_domains};
# Count number removed
$removed_allowed = $before - %domains;
my $written = 0;
my $fho = \*STDOUT;
if (defined $out && length $out) {
@ -109,5 +121,6 @@ MAIN: {
printf STDERR " - %d .domains files\n", (scalar @domain_lists);
printf STDERR " - %d .hosts files\n", (scalar @hosts_lists);
printf STDERR "(%d duplicates)\n", $dupes if $dupes;
printf STDERR "(%d domains removed via allowlist)\n", $removed_allowed if $removed_allowed;
printf STDERR "(%d skipped)\n", $skip if $skip;
}