you-dont-need-pihole/update.sh

96 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# vi: et sts=4 sw=4 ts=4
###############################################################################
# You Don't Need Pi-hole
# Network-wide DNS blocking without extra hardware.
#
# Project URL: https://codeberg.org/h3xx/you-dont-need-pihole
#
# License GPLv3: GNU GPL version 3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
# with Commons Clause 1.0 (https://commonsclause.com/).
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# 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=()
if [[ -f $CFG ]]; then
. "$CFG"
fi
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
cleanup() {
rm -fr -- "$TEMP_DIR"
}
trap 'cleanup' EXIT
(cd "$WORKDIR" && git submodule update --init --remote)
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 -p "$TEMP_DIR")
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