commit c6f800c04a39987bb5f5ef5461c40f0e639ae151 Author: Dan Church Date: Fri Jan 29 21:26:05 2021 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..032ba38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/lists/StevenBlack-all.hosts +block.list diff --git a/custom.list b/custom.list new file mode 100644 index 0000000..8ee38c0 --- /dev/null +++ b/custom.list @@ -0,0 +1,8 @@ +# Custom IP addresses to serve via dnsmasq, one per line +# +# Format: +# IP DNS-NAME... +# +# Examples: +# ffff::211:ffff:ffff:dddd joe joe-vm +# 192.168.0.2 cindy diff --git a/lists/.gitkeep b/lists/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/local.list b/local.list new file mode 100644 index 0000000..684aa76 --- /dev/null +++ b/local.list @@ -0,0 +1,8 @@ +# Local IP addresses to serve via dnsmasq, one per line +# +# Format: +# IP DNS-NAME... +# +# Examples: +# ffff::211:ffff:ffff:dddd joe joe-vm +# 192.168.0.2 cindy diff --git a/make-block.pl b/make-block.pl new file mode 100755 index 0000000..39c05db --- /dev/null +++ b/make-block.pl @@ -0,0 +1,89 @@ +#!/usr/bin/perl +# vi: et sts=4 sw=4 ts=4 +use strict; +use warnings; + +use Getopt::Long qw/ GetOptions :config no_ignore_case /; + +my %domains; +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+$//; + my $domain = lc $line; + ++$dupes if defined $domains{$domain}; + $domains{$domain} = 1; + } +} + +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; + my @parts = split /\s+/, $line; + die "Malformed line in $file: $line; @parts" + unless @parts > 1; + if (lc $parts[0] eq lc $parts[1]) { + ++$skip; + next; + } + unless (lc $parts[0] eq '0.0.0.0') { + ++$skip; + next; + } + my $domain = lc $parts[1]; + ++$dupes if defined $domains{$domain}; + $domains{$domain} = 1; + } +} + +MAIN: { + my $out = 'block.list'; + my $block_ip = '0.0.0.0'; + + unless (&GetOptions( + 'out=s' => \$out, + 'O=s' => \$out, + 'i=s' => \$block_ip, + 'block-ip=s' => \$block_ip, + )) { + exit 2; + } + my @domain_lists = glob 'lists/*.domains'; + my @hosts_lists = glob 'lists/*.hosts'; + + foreach my $listfile (@domain_lists) { + &add_domain_list($listfile); + } + foreach my $hostfile (@hosts_lists) { + &add_host_file($hostfile); + } + + my $written = 0; + open my $fho, '>', $out + or die "Failed to open file $out for writing: $!"; + print $fho map { + ++$written; + "$block_ip $_\n" + } sort keys %domains; + + printf STDERR "%d domains written to %s from\n", $written, $out; + 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 skipped)\n", $skip if $skip; +} diff --git a/update.cfg b/update.cfg new file mode 100644 index 0000000..3d2704f --- /dev/null +++ b/update.cfg @@ -0,0 +1,19 @@ +# vi: ft=sh +WORKDIR=${WORKDIR:-${BASH_SOURCE%/*}} +BACKUPSUFFIX=.old +BLOCKLIST=$WORKDIR/block.list +LIST_DIR=$WORKDIR/lists +OUT=() +URL=() + +# StevenBlack comprehensive host list (all) +OUT+=("$LIST_DIR/StevenBlack-all.hosts") +URL+=('https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts') + +# Individual hosts lists in that collection +#OUT+=("$LIST_DIR/sb-Badd-Boyz-Hosts.hosts") +#URL+=('https://raw.githubusercontent.com/StevenBlack/hosts/master/data/Badd-Boyz-Hosts/hosts') + +# Not sure how I'm supposed to update this file... +#OUT+=("$LIST_DIR/list.1.raw.githubusercontent.com.domains") +#URL+=('???') diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..9fed703 --- /dev/null +++ b/update.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# vi: et sts=4 sw=4 ts=4 + +set -e + +WORKDIR=${0%/*} +CFG=$WORKDIR/update.cfg + +. "$CFG" + +TEMP_FILES=() +cleanup() { + rm -f -- "${TEMP_FILES[@]}" +} +trap 'cleanup' EXIT + +for (( I = 0 ; I < ${#OUT[@]} ; ++I )); do + MY_URL=${URL[$I]} + MY_OUT=${OUT[$I]} + if [[ -z $MY_URL ]]; then + echo "$CFG: URL[$I] empty" >&2 + exit 2 + fi + if [[ -z $MY_OUT ]]; then + echo "$CFG: OUT[$I] empty" >&2 + exit 2 + fi + + TEMP_OUT=$(mktemp -t "${0##*/}.XXXXXX") + TEMP_FILES+=("$TEMP_OUT") + + if [[ -f $MY_OUT ]]; then + cp -a -- "$MY_OUT" "$TEMP_OUT" + fi + + wget \ + -O "$TEMP_OUT" \ + "$MY_URL" + + 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 + +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 [[ -x /etc/rc.d/rc.dnsmasq ]]; then + /etc/rc.d/rc.dnsmasq restart +fi