Compare commits

...

8 Commits
v0.2.0 ... main

Author SHA1 Message Date
Dan Church 85d9a5feca
Configure Getopt::Long with better options 2024-04-22 10:17:45 -05:00
Dan Church 7d9188c6fa
Require Perl 5.12
Fix some postfix 'if' statements too.
2023-09-04 10:50:30 -05:00
Dan Church 133b271208
Combine GetOpt::Long options 2023-09-04 10:08:36 -05:00
Dan Church 41ba29d218
Remove Perl 4 sigils (PBP) 2023-09-04 10:08:00 -05:00
Dan Church dc38b1a376
Remove unused variable 2023-07-11 12:55:25 -05:00
Dan Church 942465cc22
Remove 'set -e'
Makes scripts less able to handle errors properly.
2023-07-11 12:54:22 -05:00
Dan Church 06e9f59489
Remove vim modelines
We have .editorconfig for that.
2023-07-11 12:51:51 -05:00
Dan Church cc52e56e47
Fix uninitialized string warning 2023-07-11 11:54:52 -05:00
2 changed files with 37 additions and 34 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/perl
# vi: et sts=4 sw=4 ts=4
###############################################################################
# You Don't Need Pi-hole
@ -14,10 +13,10 @@
# You may NOT use this software for commercial purposes.
###############################################################################
use strict;
use 5.012;
use warnings;
use Getopt::Long qw/ GetOptions :config no_ignore_case /;
use Getopt::Long qw/ GetOptions :config bundling no_getopt_compat no_ignore_case /;
use FindBin qw//;
my %domains;
@ -26,16 +25,18 @@ my $skip = 0;
my $removed_allowed = 0;
sub add_domain_list {
my $file = shift;
foreach my $line (&read_stripped($file)) {
foreach my $line (read_stripped($file)) {
my $domain = lc $line;
++$dupes if defined $domains{$domain};
if (defined $domains{$domain}) {
++$dupes;
}
$domains{$domain} = 1;
}
}
sub add_host_file {
my $file = shift;
foreach my $line (&read_stripped($file)) {
foreach my $line (read_stripped($file)) {
my @parts = split /\s+/, $line;
die "Malformed line in $file: $line; @parts"
unless @parts > 1;
@ -48,7 +49,9 @@ sub add_host_file {
next;
}
my $domain = lc $parts[1];
++$dupes if defined $domains{$domain};
if (defined $domains{$domain}) {
++$dupes;
}
$domains{$domain} = 1;
}
}
@ -72,11 +75,9 @@ MAIN: {
my $block_ip = '0.0.0.0 ::';
my $workdir = $FindBin::RealBin;
unless (&GetOptions(
'out=s' => \$out,
'O=s' => \$out,
'i=s' => \$block_ip,
'block-ip=s' => \$block_ip,
unless (GetOptions(
'out|O=s' => \$out,
'block-ip|i=s' => \$block_ip,
)) {
exit 2;
}
@ -86,16 +87,16 @@ MAIN: {
my @allow_lists = glob "$workdir/allowlists/*.domains";
foreach my $listfile (@domain_lists) {
&add_domain_list($listfile);
add_domain_list($listfile);
}
foreach my $hostfile (@hosts_lists) {
&add_host_file($hostfile);
add_host_file($hostfile);
}
# Apply allowlists
my @allow_domains;
foreach my $allowlist (@allow_lists) {
push @allow_domains, &read_stripped($allowlist);
push @allow_domains, read_stripped($allowlist);
}
my $before = %domains;
delete %domains{@allow_domains};
@ -117,10 +118,16 @@ MAIN: {
} @block_ip;
} sort keys %domains;
printf STDERR "%d domains written to %s from\n", $written, $out;
printf STDERR "%d domains written to %s from\n", $written, $out // 'STDOUT';
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;
if ($dupes) {
say STDERR "($dupes duplicates)";
}
if ($removed_allowed) {
say STDERR "($removed_allowed domains removed via allowlist)";
}
if ($skip) {
say STDERR "($skip skipped)";
}
}

View File

@ -1,5 +1,4 @@
#!/bin/bash
# vi: et sts=4 sw=4 ts=4
###############################################################################
# You Don't Need Pi-hole
@ -14,20 +13,17 @@
# You may NOT use this software for commercial purposes.
###############################################################################
set -e
WORKDIR=${0%/*}
CFG=$WORKDIR/update.cfg
# Config defaults
BACKUPSUFFIX=
BLOCKLIST=$WORKDIR/block.list
LIST_DIR=$WORKDIR/lists
OUT=()
URL=()
DNSMASQ_RESTART_COMMAND=()
if [[ -f $CFG ]]; then
. "$CFG"
. "$CFG" || exit
fi
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
@ -38,9 +34,9 @@ trap 'cleanup' EXIT
copy_perms() {
local -r FROM=$1 TO=$2
chmod --reference="$FROM" -- "$TO"
chmod --reference="$FROM" -- "$TO" || exit
if [[ $UID -eq 0 ]]; then
chown --reference="$FROM" -- "$TO"
chown --reference="$FROM" -- "$TO" || exit
fi
}
@ -51,12 +47,12 @@ replace_with() {
if [[ -f $ORIG ]]; then
copy_perms "$ORIG" "$NEW"
if [[ -n $BACKUPSUFFIX ]]; then
mv -- "$ORIG" "$ORIG$BACKUPSUFFIX"
mv -- "$ORIG" "$ORIG$BACKUPSUFFIX" || exit
fi
else
mkdir -p -- "${ORIG%/*}"
mkdir -p -- "${ORIG%/*}" || exit
fi
mv -- "$NEW" "$ORIG"
mv -- "$NEW" "$ORIG" || exit
else
printf 'File "%s" not modified\n' \
"$ORIG" \
@ -67,7 +63,7 @@ replace_with() {
(cd "$WORKDIR" &&
git submodule update --init 'repos-noupdates/*' &&
git submodule update --init --remote 'repos/*'
)
) || exit
for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
MY_URL=${URL[$I]}
@ -85,13 +81,13 @@ for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
wget \
-O "$TEMP_OUT" \
"$MY_URL"
"$MY_URL" || exit
replace_with "$MY_OUT" "$TEMP_OUT"
done
TEMP_BLOCKLIST=$(mktemp -p "$TEMP_DIR")
"$WORKDIR/make-block.pl" --out="$TEMP_BLOCKLIST"
"$WORKDIR/make-block.pl" --out="$TEMP_BLOCKLIST" || exit
# Blocklist generation succeeded, install it!
@ -103,11 +99,11 @@ if [[ ! -e $BLOCKLIST ]]; then
# whatever file permissions 'mktemp' sets.
umask 0022
touch -- "$BLOCKLIST"
)
) || exit
fi
replace_with "$BLOCKLIST" "$TEMP_BLOCKLIST"
if [[ ${#DNSMASQ_RESTART_COMMAND[@]} -gt 0 ]]; then
"${DNSMASQ_RESTART_COMMAND[@]}"
"${DNSMASQ_RESTART_COMMAND[@]}" || exit
fi