mirror of
https://codeberg.org/h3xx/you-dont-need-pihole.git
synced 2024-08-14 20:27:01 +00:00
Compare commits
No commits in common. "1a97ec83653c5ea26b62ebabd615f584552b5512" and "65ade612e7e7ba4a45c7a5cca2eff65a41757830" have entirely different histories.
1a97ec8365
...
65ade612e7
4 changed files with 48 additions and 75 deletions
6
allowlists/.gitignore
vendored
6
allowlists/.gitignore
vendored
|
@ -1,6 +0,0 @@
|
||||||
# Ignore everything in this directory
|
|
||||||
*
|
|
||||||
# Except this file
|
|
||||||
!.gitignore
|
|
||||||
# ... And some others
|
|
||||||
!/allowlist.domains.example
|
|
|
@ -1,8 +0,0 @@
|
||||||
# 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
|
|
|
@ -23,10 +23,15 @@ use FindBin qw//;
|
||||||
my %domains;
|
my %domains;
|
||||||
my $dupes = 0;
|
my $dupes = 0;
|
||||||
my $skip = 0;
|
my $skip = 0;
|
||||||
my $removed_allowed = 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;
|
||||||
|
@ -35,7 +40,15 @@ 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;
|
||||||
|
@ -53,20 +66,6 @@ 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';
|
||||||
|
@ -83,7 +82,6 @@ MAIN: {
|
||||||
|
|
||||||
my @domain_lists = glob "$workdir/lists/*.domains";
|
my @domain_lists = glob "$workdir/lists/*.domains";
|
||||||
my @hosts_lists = glob "$workdir/lists/*.hosts";
|
my @hosts_lists = glob "$workdir/lists/*.hosts";
|
||||||
my @allow_lists = glob "$workdir/allowlists/*.domains";
|
|
||||||
|
|
||||||
foreach my $listfile (@domain_lists) {
|
foreach my $listfile (@domain_lists) {
|
||||||
&add_domain_list($listfile);
|
&add_domain_list($listfile);
|
||||||
|
@ -92,16 +90,6 @@ MAIN: {
|
||||||
&add_host_file($hostfile);
|
&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 $written = 0;
|
||||||
my $fho = \*STDOUT;
|
my $fho = \*STDOUT;
|
||||||
if (defined $out && length $out) {
|
if (defined $out && length $out) {
|
||||||
|
@ -121,6 +109,5 @@ MAIN: {
|
||||||
printf STDERR " - %d .domains files\n", (scalar @domain_lists);
|
printf STDERR " - %d .domains files\n", (scalar @domain_lists);
|
||||||
printf STDERR " - %d .hosts files\n", (scalar @hosts_lists);
|
printf STDERR " - %d .hosts files\n", (scalar @hosts_lists);
|
||||||
printf STDERR "(%d duplicates)\n", $dupes if $dupes;
|
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;
|
printf STDERR "(%d skipped)\n", $skip if $skip;
|
||||||
}
|
}
|
||||||
|
|
64
update.sh
64
update.sh
|
@ -36,34 +36,6 @@ cleanup() {
|
||||||
}
|
}
|
||||||
trap 'cleanup' EXIT
|
trap 'cleanup' EXIT
|
||||||
|
|
||||||
copy_perms() {
|
|
||||||
local -r FROM=$1 TO=$2
|
|
||||||
chmod --reference="$FROM" -- "$TO"
|
|
||||||
if [[ $UID -eq 0 ]]; then
|
|
||||||
chown --reference="$FROM" -- "$TO"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
replace_with() {
|
|
||||||
local -r ORIG=$1 NEW=$2
|
|
||||||
if ! diff -q -- "$ORIG" "$NEW" &>/dev/null; then
|
|
||||||
# There's a change
|
|
||||||
if [[ -f $ORIG ]]; then
|
|
||||||
copy_perms "$ORIG" "$NEW"
|
|
||||||
if [[ -n $BACKUPSUFFIX ]]; then
|
|
||||||
mv -- "$ORIG" "$ORIG$BACKUPSUFFIX"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
mkdir -p -- "${ORIG%/*}"
|
|
||||||
fi
|
|
||||||
mv -- "$NEW" "$ORIG"
|
|
||||||
else
|
|
||||||
printf 'File "%s" not modified\n' \
|
|
||||||
"$ORIG" \
|
|
||||||
>&2
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
(cd "$WORKDIR" && git submodule update --init --remote)
|
(cd "$WORKDIR" && git submodule update --init --remote)
|
||||||
|
|
||||||
for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
|
for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
|
||||||
|
@ -80,16 +52,44 @@ for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do
|
||||||
|
|
||||||
TEMP_OUT=$(mktemp -p "$TEMP_DIR")
|
TEMP_OUT=$(mktemp -p "$TEMP_DIR")
|
||||||
|
|
||||||
|
if [[ -f $MY_OUT ]]; then
|
||||||
|
cp -a -- "$MY_OUT" "$TEMP_OUT"
|
||||||
|
fi
|
||||||
|
|
||||||
wget \
|
wget \
|
||||||
-O "$TEMP_OUT" \
|
-O "$TEMP_OUT" \
|
||||||
"$MY_URL"
|
"$MY_URL"
|
||||||
|
|
||||||
replace_with "$MY_OUT" "$TEMP_OUT"
|
if [[ -f $MY_OUT ]]; then
|
||||||
|
chmod --reference="$MY_OUT" "$TEMP_OUT"
|
||||||
|
if [[ -n $BACKUPSUFFIX ]]; then
|
||||||
|
mv -- "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p -- "${MY_OUT%/*}"
|
||||||
|
mv -- "$TEMP_OUT" "$MY_OUT"
|
||||||
|
|
||||||
|
# If the old one is the same, don't keep it around
|
||||||
|
if [[ -n $BACKUPSUFFIX && -f $MY_OUT$BACKUPSUFFIX ]]; then
|
||||||
|
if diff -q "$MY_OUT" "$MY_OUT$BACKUPSUFFIX"; then
|
||||||
|
rm -f -- "$MY_OUT$BACKUPSUFFIX"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
TEMP_BLOCKLIST=$(mktemp -p "$TEMP_DIR")
|
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST ]]; then
|
||||||
"$WORKDIR/make-block.pl" --out="$TEMP_BLOCKLIST"
|
mv -- "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"
|
||||||
replace_with "$BLOCKLIST" "$TEMP_BLOCKLIST"
|
fi
|
||||||
|
"$WORKDIR/make-block.pl" --out="$BLOCKLIST"
|
||||||
|
|
||||||
|
# If the old one is the same the same, don't keep it around
|
||||||
|
if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST$BACKUPSUFFIX ]]; then
|
||||||
|
if diff -q "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX"; then
|
||||||
|
rm -f -- "$BLOCKLIST$BACKUPSUFFIX"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ${#DNSMASQ_RESTART_COMMAND[@]} -gt 0 ]]; then
|
if [[ ${#DNSMASQ_RESTART_COMMAND[@]} -gt 0 ]]; then
|
||||||
"${DNSMASQ_RESTART_COMMAND[@]}"
|
"${DNSMASQ_RESTART_COMMAND[@]}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue