Purpose
I wanted to not see those annoying flashing ad images on webpages, and also reduce internet bandwidth by not downloading those images. Not for me or others in my network.
Overview
What I did was the following:
- I set up a dns server, using dnsmasq, to redirect all requests to ad servers (like doubleclick) to a local server
- I set up a cron script to update the list of ad servers every four hours
- I set up a Cherokee webserver to handle all requests and return a one by one transparent gif pixel
Details
Fire up OpenSlug
First I unwrapped a new box, to get my new NSLU2 out of it. I just received my new Slug today, the third one.
Of course, setting up OpenSlug was easy from the webinterface: turn slug on, attach my notebook and browse to http://192.168.1.77.
From there I hit the Administration/Upgrade tab and uploaded new firmware, openslug-3.10-beta.bin this time. After that just wait.... beep! and it is alive.
Since openslug appears to use DHCP to get its ip address, I had to check my DHCP table (or ping to 192.168.1.255) to see where it went. It got 192.168.1.103 in my case, you might get a different address. I believe that if you start up your slug without a DHCP server in your network (or without the network cable attached) it will get 192.168.1.77.
To get OpenSlug going, I went into the server using ssh:
turnup init
umount /dev/sda1
mkfs.ext3 /dev/sda1
turnup memstick -i /dev/sda1
reboot
date [MMDDhhmm[[CC]YY]
|
I used 172.16.16.60 for this server, you can pick your own address. All directions below use this ip address for dns server and for the catch-all webserver.
Set up DNS
I use dnsmasq as DNS server, but I don't care about it's DHCP capabilities, because I already have a DHCP server built in my router. The DHCP server will send the clients the specs of DNS servers to use, starting with this new one.
Here is how I setup the DNS server:
ipkg update
ipkg install dnsmasq
# add my own servers to the hosts file
echo "172.16.16.123 myfileserver" >> /etc/hosts
#make a very simple /etc/dnsmasq:
echo '
domain-needed
bogus-priv
strict-order
domain=mydomain.local
addn-hosts=/etc/banner_add_hosts
'>/etc/dnsmasq.conf
|
There are a lot more flags that can be defined in dnsmasq.conf, but I ignore them all for my simple DNS server.
Retrieve the list of ad servers
Create an empty file in which you can add some adservers yourself if you want to:
echo "">/etc/banner_add_hosts
Create an empty file in which you can add some adservers yourself if you want to:
echo "">/etc/banner_add_hosts.manual
Dnsmasq runs under a different user/group by default and will fail to read those files if read permissions are not set properly. It will not give any warning either and will ignore the whole nice setup.
Set the permissions to allow dnsmasq to read those files:
chmod 644 /etc/banner_add_hosts
chmod 644 /etc/banner_add_hosts.manual
Also create the usr/local/bin directory to put our local scripts in:
mkdir -p /usr/local/bin
Now create a script that will fetch the list of adservers:
/usr/local/bin/update_bannerhosts
Here is the script (don't start with empty line):
#!/bin/sh
### short script that downloads a list of ad servers for use with
### squid to block ads.
###
### details on configuring squid itself can be found here:
###
### http://pgl.yoyo.org/adservers/#withsquid
###
### - originally by Stephen Patterson <steve@lexx.uklinux.net>
### - butchered by Peter Lowe <pgl@yoyo.org>
### - altered by Jelle Alten (hotmailaddres:jpalten) to suit his needs on OpenSlug
###
## set things
##
# the ipaddress where we want to send the requests to, instead of the bannerservers
addcatcherip='172.16.16.60'
# the args to add to the request to the yoyo server, to tell it that we want
# a hosts file and that we want to redirect to the addcatcher
listurlargs="hostformat=hosts&showintro=1&mimetype=plaintext&useip=$addcatcherip"
# URL of the ad server list to download
listurl="http://pgl.yoyo.org/adservers/serverlist.php?$listurlargs"
# location of the list of ad servers used by dnsmasq
targetfile='/etc/banner_add_hosts'
# location of a file where hostnames not listed can be added
extrasfile='/etc/banner_add_hosts.manual'
## command to reload squid - change according to your system
## not sure if we need this for dnsmasq
reloadcmd='/etc/init.d/dnsmasq restart'
# temp file to use
tmpfile="/tmp/.adlist.$$"
# command to fetch the list (alternatives commented out)
fetchcmd="/usr/bin/wget -q -O $tmpfile $listurl"
## do things
##
# get a fresh list of ad server addresses for dnsmasq to refuse
$fetchcmd
# add the extras
[ -f "$extrasfile" ] && cat $extrasfile >> $tmpfile
# check the temp file exists OK before overwriting the existing list
if [ ! -s $tmpfile ]
then
echo "temp file '$tmpfile' either doesn't exist or is empty; quitting"
exit
fi
# sort and filter out duplicates
sort $tmpfile > $targetfile
# clean up
rm $tmpfile
# restart dnsmasq (probably not needed)
$reloadcmd
|
Of course, this script needs to be executable:
chmod 700 /usr/local/bin/update_bannerhosts
Now run the script and see if it works. You have to adjust the DHCP server to send DNS requests to this new DNS server. You might need to renew your IP or reconnect your client to
setup dhcp to point dns to this host.
test it by using
dig doubleclick.com
dig cnn.com
nslookup cnn.com
nslookup doubleclick.com
The first and last one should point to the new slug now.
Auto-update the list
Now we have the script run automatically every 4 hours, using cron:
ipkg install cron
Edit the cront tab using
crontab -e
and add the line
0 0,4,8,12,16,18,20 * * * /usr/local/bin/update_bannerhosts
Install Cherokee webserver
ipkg install cherokee
echo "Include /etc/cherokee/catch-all.conf">>/etc/cherokee/cherokee.conf
echo '
# rewrite all requests to serve /images/pixel.gif
# with the original request as argument behind a "?"
Request ".*" {
Handler redir {
Show Rewrite "/images/pixel.gif?"
}
}
# this request will only return a file
# since it is lower than the request above, it will be handled
# with higer priority
Request "/images/pixel.gif.*" {
Handler file
}
' > /etc/cherokee/catch-all.conf
|
Now add the (empty) image in the right place. You can either create it yourself, or download it like I did:
mkdir -p /var/www/images
wget -O /var/www/images/pixel.gif http://www.loremipsum.net/pixelgif/pics/pixel.gif
|
You can start Cherokee by entering
cherokee
But you probably want to autostart Cherokee at boot time. To do so, borrow the start up script from dnsmasq:
cp /etc/init.d/dnsmasq /etc/init.d/cherokee
vi /etc/init.d/cherokee
Replace the first 5 lines with (don't start with empty line):
#!/bin/sh
DAEMON=/usr/sbin/cherokee
NAME=cherokee
DESC="Cherokee webserver"
ARGS="-b"
|
To install the autostart script, do:
update-rc.d cherokee defaults
Now reboot the slug and see it catch the ads!
Check to see if all wanted processes are running using ps.
Note: I had some trouble getting both dns and cherokee to start up automatically. It might have to do something with both getting S20/K20 scripts in the rc?.d directories. I changed one to become S21/K21 links. Seems to work now.