Skip to content

Instantly share code, notes, and snippets.

@Holzhaus
Created December 26, 2014 01:44
Show Gist options
  • Save Holzhaus/ed4ac1675a57f11c3057 to your computer and use it in GitHub Desktop.
Save Holzhaus/ed4ac1675a57f11c3057 to your computer and use it in GitHub Desktop.
Adblock script for OpenWRT
# 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