Skip to content

Instantly share code, notes, and snippets.

@BlakeRain
Last active July 3, 2024 14:57
Show Gist options
  • Save BlakeRain/75bc52434528dcabb6ca87908d70468e to your computer and use it in GitHub Desktop.
Save BlakeRain/75bc52434528dcabb6ca87908d70468e to your computer and use it in GitHub Desktop.
Script to generate adblocking configuration for Unbound
#!/bin/sh
# See https://blakerain.com/blog/adding-dns-adblock-to-unbound/
UNBOUND="/var/unbound/etc"
VARIANT=${VARIANT:-fakenews-gambling-social}
URL="https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/$VARIANT/hosts"
wget -qO- "$URL" | grep '^0\.0\.0\.0' | sort | \
awk '{print "local-zone: \""$2"\" refuse"}' > "$UNBOUND/adblock.conf"
if [ -f "$HOME/adblock.exceptions" ]; then
(cat "$HOME/adblock.exceptions" | \
awk '{gsub(/\./,"\\."); print "g/\"" $0 "\"/d"}'; echo w) | ed - "$UNBOUND/adblock.conf"
cat "$HOME/adblock.exceptions" | \
awk '{print "local-zone: \""$0".\" transparent"}' > "$UNBOUND/adblock.exceptions.conf"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment