Skip to content

Instantly share code, notes, and snippets.

@xabolcs
Forked from Holzhaus/adblockupdater.sh
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xabolcs/b71e1220bcd943f9dd70 to your computer and use it in GitHub Desktop.
Save xabolcs/b71e1220bcd943f9dd70 to your computer and use it in GitHub Desktop.
# Adblock script for OpenWRT
# (c) 2014 by Jan Holthuis
#
# This is an adblocker script for OpenWRT. Simply run this script as a
# daily cronjob on your OpenWRT-router. This works since OpenWRT
# revision 39312 [1] and does not manipulate any files in /etc/.
# Instead, this adds the adblock serverlist as a separate file
# to /tmp/dnsmasq.d/. It also checks the file with grep to make sure
# that it doesn't contain malicious commands.
#
# [1] See https://dev.openwrt.org/changeset/39312/
# Set DNSmasq serverlist url
srcurl="http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext"
# Set destination file
dstfile="/tmp/dnsmasq.d/adblock.conf"
# And here comes the magic...
_tmpfile=$(mktemp -tu)
wget -O "$_tmpfile" "$srcurl"
if [ "$?" -eq 0 ]
then
# For security reasons, we check if the file only contains 'address'
# statements and that these statements only specify 127.0.0.1 as
# target address. Otherwise, someone evil might slip in malicious
# commands into the yoyo.org file (or manipulate it during transfer)
grep -E -v "address=/.+/127.0.0.1" "$_tmpfile"
if [ "$?" -ne 0 ]
then
# Write the destination file header
echo "#" > "$dstfile"
echo "# $dstfile: adblock server list for dnsmasq" >> "$dstfile"
echo "# " >> "$dstfile"
echo "" >> "$dstfile"
echo "# Downloaded from URL:" >> "$dstfile"
echo "# $srcurl" >> "$dstfile"
echo "# " >> "$dstfile"
_now=$(date)
echo "# Last updated: $_now" >> "$dstfile"
echo "" >> "$dstfile"
# Add the servers to the destination file
cat "$_tmpfile" >> "$dstfile"
echo "Everything fine, restarting dnsmasq to implement new serverlist..."
/etc/init.d/dnsmasq restart
else
echo "WARNING: Adblock serverlist from $srcurl looks fishy and will not be used for security reasons!"
fi
else
echo "WARNING: Unable to download adblock serverlist from $srcurl!"
fi
rm "$_tmpfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment