I wanted a simple DNS-based ad blocking solution.
I set up uHTTPd to respond with a 1x1 pixel transparent gif for any invalid page requests. Since LuCI is set up on port 80 and 443 already, I simply added
option error_page '/1.gif'
to /etc/config/uhttpd Any valid requests will still go to LuCI, but invalid requests will return a 1 pixel gif. I downloaded a 1x1 pixel transparent gif and put it into /www/
/usr/bin/wget -O /www/1.gif http://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif
And then restarted uHTTPd:
/etc/init.d/uhttpd restart
I saved this script to /etc/adblock.sh, but it could be stored anywhere:
#!/bin/sh
GIFSERVER=192.168.1.1
/usr/bin/wget -qO- http://mvps.org/winhelp2002/hosts.txt | /bin/grep -v -e localhost -e=feeds.feedburner.com | /bin/grep '^0.0.0.0' | /bin/sed "s/^0\.0\.0\.0\s*//" | /bin/sed 's/#.*$//' | /bin/sed 's/\s*$//' > /etc/hosts.block-1
/usr/bin/wget -qO- http://www.malwaredomainlist.com/hostslist/hosts.txt | /bin/grep -v -e localhost | /bin/grep '^127.0.0.1' | /bin/sed "s/^127\.0\.0\.1\s*//" | /bin/sed 's/#.*$//' | /bin/sed 's/\s*$//' > /etc/hosts.block-2
/usr/bin/wget -qO- http://hosts-file.net/ad_servers.txt | /bin/grep -v -e localhost | /bin/grep '^127.0.0.1' | /bin/sed "s/^127\.0\.0\.1\s*//" | /bin/sed 's/#.*$//' | /bin/sed 's/\s*$//' | /bin/sed 's/\.$//' > /etc/hosts.block-3
/usr/bin/wget -qO- http://adaway.org/hosts.txt | /bin/grep -v -e localhost | /bin/grep '^127.0.0.1' | /bin/sed "s/^127\.0\.0\.1\s*//" | /bin/sed 's/#.*$//' | /bin/sed 's/\s*$//' > /etc/hosts.block-4
/bin/cat /etc/hosts.block-1 /etc/hosts.block-2 /etc/hosts.block-3 /etc/hosts.block-4 | /usr/bin/tr [A-Z] [a-z] | /usr/bin/awk -F "." '{for(i=NF; i > 1; i--) printf "%s.", $i; print $1}' | /usr/bin/sort -fn | /usr/bin/awk -F "." '{for(i=NF; i > 1; i--) printf "%s.", $i; print $1}' | /bin/sed "s/^/$GIFSERVER\t/" | /usr/bin/uniq >> /etc/hosts.block
/bin/rm /etc/hosts.block-1 /etc/hosts.block-2 /etc/hosts.block-3 /etc/hosts.block-4
This script basically downloads four different host lists of ad servers, replaces the ad servers IP addresses with the IP address of the OpenWRT box, and combines them into one file without duplicate entries. This script could be run with cron, or by hand like so:
/bin/sh /etc/adblock.sh
Take a look at /etc/hosts.block to see the result. Next I replaced the contents of /etc/dnsmasq.conf with the following:
addn-hosts=/etc/hosts.block
all-servers
bogus-priv
domain-needed
no-resolv
server=8.8.8.8
server=8.8.4.4
It adds the contents of /etc/hosts.block to the local DNS. All that is needed is to restart Dnsmasq:
/etc/init.d/dnsmasq restart
Now browsing the web (with or without a ad block browser extension) will result in a much cleaner browsing experience. It'll also be faster since the requests for ads will be handled locally.
The drawbacks are that many web sites out there depend on ad revenue and with this solution, you'd be depriving them of that revenue. Also, "sponsored" ads in Google searches don't work, but usually the URL can be modified to get to the expected site. Other than that, I've been using this solution in my home for several years, albeit with DD-WRT, Pixelserv, and Dnsmasq. Now that I've switched to OpenWRT, it's a much simpler solution than I'm used to. :-)
(Last edited by ptb on 23 May 2015, 03:22)