Skip to content

Instantly share code, notes, and snippets.

@g3rhard
Created December 13, 2016 04:38
Show Gist options
  • Save g3rhard/b30f6a4af4648469d99f5fd4453f5c5e to your computer and use it in GitHub Desktop.
Save g3rhard/b30f6a4af4648469d99f5fd4453f5c5e to your computer and use it in GitHub Desktop.
OpenWRT rc.local for advertising block
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
# http://paul.is-a-geek.org/2015/06/dns-based-adblock-using-openwrt-opendns-and-dnsmasq/
#!/bin/sh
#Block ads, malware, etc.
logger -t "adblock" -s 'Starting adblock setup...'
#Delete the old adblock_hosts to make room for the updates
rm /tmp/adblock_hosts
logger -t "adblock" -s 'Downloading hosts lists...'
wget -qO- "http://winhelp2002.mvps.org/hosts.txt" | awk '/^0.0.0.0/' > /tmp/block.build.list
wget -qO- "http://www.malwaredomainlist.com/hostslist/hosts.txt" | awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> /tmp/block.build.list
wget -qO- "http://hosts-file.net/ad_servers.txt" | awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> /tmp/block.build.list
wget -qO- "http://adaway.org/hosts.txt" | awk '{sub(/^127.0.0.1/, "0.0.0.0")} /^0.0.0.0/' >> /tmp/block.build.list
if [ -s "/etc/black.list" ]
then
logger -t "adblock" -s 'Adding blacklist...'
awk '/^[^#]/ { print "0.0.0.0",$1 }' /etc/black.list >> /tmp/block.build.list
fi
logger -t "adblock" -s 'Sorting lists...'
awk '{sub(/\r$/,"");print $1,$2}' /tmp/block.build.list|sort -u > /tmp/block.build.before
if [ -s "/etc/white.list" ]
then
#Filter the blacklist, supressing whitelist matches
# This is relatively slow =-(
logger -t "adblock" -s 'Filtering white list...'
awk '/^[^#]/ {sub(/\r$/,"");print $1}' /etc/white.list | grep -vf - /tmp/block.build.before > /tmp/adblock_hosts
else
cat /tmp/block.build.before > /tmp/adblock_hosts
fi
logger -t "adblock" -s 'Cleaning up...'
#Delete files used to build list to free up the limited space
rm -f /tmp/block.build.before
rm -f /tmp/block.build.list
logger -t "adblock" -s 'Restarting dnsmasq...'
/etc/init.d/dnsmasq restart
logger -t "adblock" -s 'Finished adblock setup'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment