TechInfoDepot:Tomato/Clean, Lean and Mean Adblocking

I've been looking into blocking ads on Tomato firmwares. There are already scripts for this, but they did not suit my needs.

xcooling's script is outdated, uses very ugly and slow code and is on top of that buggy. ALL-U-NEED Ad Blocking is very cumbersome through their decoder, bloated, unreadable and again, slow.

Both also don't employ the method I envisioned. So I wrote my own script, took ideas from xcooling's script and this DD-WRT script, improved it, combined it with my own sh expertise and optimized the heck out of it.

Features

 * Takes public blocklists for known ad hosts and redirects them via DNS poisoning
 * pixelserv (optional) through a second IP (Web GUI on port 80 still works!)

-> e.g. router on 192.168.0.1, but also responds as 192.168.0.254 with pixelserv -> this serves transparent pixels instead of causing error messages on blocked ads


 * Does not interfere with normal dnsmasq operation

-> does not try to "optimize" it (that's what the "Custom configuration" box on the web GUI is for, people!) -> does not break Tomato's ability to restart dnsmasq should it crash


 * Additional blocklist sources can easily be added
 * Easy blacklist and whitelist
 * Very optimized: Updates as quickly and with as little CPU/memory usage as possible
 * Small and lean: Only does what it needs to do, then gets out of the way
 * Readable code

Instructions
If you're using Adblock pre-v4.0, please remove it completely from your router and reboot it first! Note: If the paste procedure fails for you, try transferring the corresponding file manually using scp, WinSCP or something (the DD-WRT link up there has some WinSCP usage examples)


 * Verify that your Tomato supports custom dnsmasq configs (i.e. shows this line under Advanced->DHCP/DNS: "Note: The file /etc/dnsmasq.custom is also added to the end of Dnsmasq's configuration file if it exists.")
 * Set up some kind of non-volatile storage. This is up to you, options are JFFS, CIFS, SD card, USB and possibly more. Note the path.

-> The simplest would probably be JFFS. Check this link -> I recommend other storage methods however, as JFFS is very limited in size (depends on which filter lists you'll ultimately use of course)


 * Designate a directory on your storage for adblock, e.g. /jffs/adblock/ (as seen by the router). Avoid spaces! This is the PREFIX.
 * Install pixelserv v30 if desired (thread). Take the entire chunk of script in this link, adjust PREFIX at the top, paste it into the box on Tools->System and press Execute. This is also how you can update pixelserv in the future.

If this fails, extract the pixelserv binary from this link and manually transfer it to PREFIX/pixelserv


 * Install adblock.sh v4.1. Take the entire chunk of script in this link, adjust PREFIX at the top, paste it into the box on Tools->System and press Execute. This is also how you can update Adblock in the future.

If this fails, paste the script from this link into a file and manually transfer it to PREFIX/adblock.sh


 * Install the config file. It will become PREFIX/config. Take the script below, adjust the config to your tastes, paste it into the box on Tools->System and press Execute. That's also how you can change the configuration in the future.

Config File

 * Ready for the first run! Paste this into the box on Tools->System, edit the PREFIX and press Execute: /YOUR/PREFIX/HERE/adblock.sh
 * The script also accepts a few command-line options:

-> /YOUR/PREFIX/HERE/adblock.sh - default, update and enable adblocker -> /YOUR/PREFIX/HERE/adblock.sh force - force updating of filters, even if not outdated -> /YOUR/PREFIX/HERE/adblock.sh stop - disable the adblocker -> /YOUR/PREFIX/HERE/adblock.sh toggle - disable the adblocker if active, enable if inactive (perfect for the SES button on your WRT54G!) -> /YOUR/PREFIX/HERE/adblock.sh restart - restart adblocker (e.g. for config changes, script updates)


 * If you want to enable Adblock automatically when the router boots, just put /YOUR/PREFIX/HERE/adblock.sh into the WAN Up section on Administration->Scripts
 * If you want to have Adblock automatically update its filters, just put /YOUR/PREFIX/HERE/adblock.sh into one of the Custom commands on Administration->Scheduler
 * Have fun! :)

Changelog

 * 3.3 - Fixed small issue with IPv6 present
 * 3.4 - Added bridge interface selector and minor changes
 * 3.5 - Prevent multiple instances, block everything but pixelserv on pixel IP, add Malware source and minor changes
 * 3.6 - Now checks if blocklists are outdated
 * 3.6.1 - Added "force" option to ignore Last-Modified headers
 * 3.7 - Added "bigmem" mode
 * 4.0 - Fundamental changes, see this thread
 * 4.1 - Minor changes, more checks