Use a common method of reading lines
Adds comment support for domain lists.
This commit is contained in:
parent
59ef0fd513
commit
fd8061714f
|
@ -25,13 +25,7 @@ my $dupes = 0;
|
||||||
my $skip = 0;
|
my $skip = 0;
|
||||||
sub add_domain_list {
|
sub add_domain_list {
|
||||||
my $file = shift;
|
my $file = shift;
|
||||||
|
foreach my $line (&read_stripped($file)) {
|
||||||
open my $fni, '<', $file
|
|
||||||
or die "Failed to open file $file for reading: $!";
|
|
||||||
|
|
||||||
foreach my $line (<$fni>) {
|
|
||||||
chomp $line;
|
|
||||||
$line =~ s/^\s+|\s+$//;
|
|
||||||
my $domain = lc $line;
|
my $domain = lc $line;
|
||||||
++$dupes if defined $domains{$domain};
|
++$dupes if defined $domains{$domain};
|
||||||
$domains{$domain} = 1;
|
$domains{$domain} = 1;
|
||||||
|
@ -40,15 +34,7 @@ sub add_domain_list {
|
||||||
|
|
||||||
sub add_host_file {
|
sub add_host_file {
|
||||||
my $file = shift;
|
my $file = shift;
|
||||||
|
foreach my $line (&read_stripped($file)) {
|
||||||
open my $fni, '<', $file
|
|
||||||
or die "Failed to open file $file for reading: $!";
|
|
||||||
|
|
||||||
foreach my $line (<$fni>) {
|
|
||||||
chomp $line;
|
|
||||||
# strip whitespace and comments
|
|
||||||
$line =~ s/^\s+|\s+$|\s*#.*$//;
|
|
||||||
next unless $line;
|
|
||||||
my @parts = split /\s+/, $line;
|
my @parts = split /\s+/, $line;
|
||||||
die "Malformed line in $file: $line; @parts"
|
die "Malformed line in $file: $line; @parts"
|
||||||
unless @parts > 1;
|
unless @parts > 1;
|
||||||
|
@ -66,6 +52,20 @@ sub add_host_file {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub read_stripped {
|
||||||
|
my $file = shift;
|
||||||
|
|
||||||
|
open my $fni, '<', $file
|
||||||
|
or die "Failed to open file $file for reading: $!";
|
||||||
|
|
||||||
|
map {
|
||||||
|
chomp;
|
||||||
|
# Strip whitespace and comments
|
||||||
|
s/^\s+|\s+$|\s*#.*$//;
|
||||||
|
$_ || ()
|
||||||
|
} <$fni>;
|
||||||
|
}
|
||||||
|
|
||||||
MAIN: {
|
MAIN: {
|
||||||
my $out;
|
my $out;
|
||||||
my $block_ip = '0.0.0.0 ::1';
|
my $block_ip = '0.0.0.0 ::1';
|
||||||
|
|
Loading…
Reference in New Issue