mirror of
https://codeberg.org/h3xx/you-dont-need-pihole.git
synced 2024-08-14 20:27:01 +00:00
Use a common method of reading lines
Adds comment support for domain lists.
This commit is contained in:
parent
59ef0fd513
commit
fd8061714f
1 changed files with 16 additions and 16 deletions
|
@ -25,13 +25,7 @@ my $dupes = 0;
|
|||
my $skip = 0;
|
||||
sub add_domain_list {
|
||||
my $file = shift;
|
||||
|
||||
open my $fni, '<', $file
|
||||
or die "Failed to open file $file for reading: $!";
|
||||
|
||||
foreach my $line (<$fni>) {
|
||||
chomp $line;
|
||||
$line =~ s/^\s+|\s+$//;
|
||||
foreach my $line (&read_stripped($file)) {
|
||||
my $domain = lc $line;
|
||||
++$dupes if defined $domains{$domain};
|
||||
$domains{$domain} = 1;
|
||||
|
@ -40,15 +34,7 @@ sub add_domain_list {
|
|||
|
||||
sub add_host_file {
|
||||
my $file = shift;
|
||||
|
||||
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;
|
||||
foreach my $line (&read_stripped($file)) {
|
||||
my @parts = split /\s+/, $line;
|
||||
die "Malformed line in $file: $line; @parts"
|
||||
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: {
|
||||
my $out;
|
||||
my $block_ip = '0.0.0.0 ::1';
|
||||
|
|
Loading…
Reference in a new issue