diff --git a/allowlists/.gitignore b/allowlists/.gitignore deleted file mode 100644 index 57d81ac..0000000 --- a/allowlists/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# Ignore everything in this directory -* -# Except this file -!.gitignore -# ... And some others -!/allowlist.domains.example diff --git a/allowlists/allowlist.domains.example b/allowlists/allowlist.domains.example deleted file mode 100644 index 923103e..0000000 --- a/allowlists/allowlist.domains.example +++ /dev/null @@ -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 diff --git a/make-block.pl b/make-block.pl index 1ac0d8a..43d2fd1 100755 --- a/make-block.pl +++ b/make-block.pl @@ -23,10 +23,15 @@ 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)) { + + 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; ++$dupes if defined $domains{$domain}; $domains{$domain} = 1; @@ -35,7 +40,15 @@ sub add_domain_list { sub add_host_file { 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; die "Malformed line in $file: $line; @parts" 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: { my $out; my $block_ip = '0.0.0.0 ::1'; @@ -83,7 +82,6 @@ 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); @@ -92,16 +90,6 @@ 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) { @@ -121,6 +109,5 @@ 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; } diff --git a/update.sh b/update.sh index 14d7a95..81b68f5 100755 --- a/update.sh +++ b/update.sh @@ -36,34 +36,6 @@ cleanup() { } 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) for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do @@ -80,16 +52,44 @@ for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do TEMP_OUT=$(mktemp -p "$TEMP_DIR") + if [[ -f $MY_OUT ]]; then + cp -a -- "$MY_OUT" "$TEMP_OUT" + fi + wget \ -O "$TEMP_OUT" \ "$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 -TEMP_BLOCKLIST=$(mktemp -p "$TEMP_DIR") -"$WORKDIR/make-block.pl" --out="$TEMP_BLOCKLIST" -replace_with "$BLOCKLIST" "$TEMP_BLOCKLIST" +if [[ -n $BACKUPSUFFIX && -f $BLOCKLIST ]]; then + mv -- "$BLOCKLIST" "$BLOCKLIST$BACKUPSUFFIX" +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 "${DNSMASQ_RESTART_COMMAND[@]}"