thelonelycoder
Part of the Furniture
AB-Solution is now Diversion
Please check the Diversion website for latest version and discussion link.
These snippets of code posted here are now a automated install script with control center character under the name of AB-Solution.
This is a simple but effective way to block ads on all your devices
------------------------------------------------------------
Changelog
------------------------------------------------------------
September 06, 2015: Added Quick Enable / Disable Adblocking, TOC added
August 31, 2015: Removed requirement for Entware, it was never really needed.
April 10, 2015: Typo, changed addn-hosts=/tmp/mnt/sda1/hosts/hosts.blocked.txt to [...]/hosts.blocked in script dnsmasq.conf.add, thanks tijaune.
April 08, 2015: Added Advanced method
March 31, 2015: Added preparatory steps for jffs
March 21, 2015: Simplified install steps.
March 20, 2015: Adding the cron job with /jffs/scripts/services-start instead of /jffs/scripts/init-start for compatibility with latest firmware.
March 17, 2015: If DNS-Filtering is enabled in your routers interface AdBlocking will not work. The DNS Filter bypasses Dnsmasq settings. See this thread for details.
April 30, 2014: If you want to remove some domains appearing in the hosts.clean you can do this with a whitelist file. See post 65 in this thread.
If you want to go even further with my ad-blocking solution, e2301010 has posted AdBlocking with combined hosts file with Lighttpd.
March 14, 2014: wget scripts are modified to reflect the much better and smarter findings of decedion.
------------------------------------------------------------
Table of content
------------------------------------------------------------
Indroduction
1. Standard method (the original)
2. Advanced method
3. Notes
4. Quick Enable / Disable Adblocking
------------------------------------------------------------
Introduction
This combines well maintained hosts files into one, with no duplicate entries.
The resulting file is about 728 KB (1450 KB Advanced method) containing more than 26000 (52600) lines of blocked domains.
Preparatory steps for both methods:
In Administration / System set to Yes:
- Enable JFFS partition*
- Enable JFFS custom scripts and configs
*) If this has not been set before set 'Format JFFS partition at next boot' to Yes and Reboot.
In this post two ways to use the combined hosts file are explained, the Standard and Advanced method. The Standard method is the original method, updated as per the Changelog. On April 04, 2015 the Advanced method was added with additional hosts files and support for a black- and whitelist of domains.
Note: The two methods are not compatible, use only one in your router.
1. Standard method (the original)
The files are stored on a USB disk, /tmp/mnt/sda1/ is the path. If you use /jffs/ edit accordingly.
Using a USB disk we need to delay the startscript.
This will create an up-to-date hosts file after every reboot.
Add to /jffs/scripts/post-mount
Tell dnsmasq to add the new hosts file and where to send the unwanted requests to (sending them to 0.0.0.0)
Create or add to /jffs/configs/dnsmasq.conf.add
To update the hosts file (manually or as a cron job) create /jffs/scripts/update-hosts.sh
To run update-hosts.sh every Friday midnight as a cron job create or add to /jffs/scripts/init-start /jffs/scripts/services-start
Set all files in /jffs/scripts/ as executable
That's it, reboot and enjoy!
Tested on a RT-AC66U.
2. Advanced method
This method supports a black- and a whitelist for domains you want to include/exclude in the hosts file. It uses 5 well maintained hosts files (Standard method uses 3). Thank you Weble for the input in post #143.
Note: The code below is NOT compatible with the Standard method!
This will create an up-to-date hosts file after every reboot and update the hosts file with cron every Friday at midnight.
Note that the files are stored in /tmp/mnt/sda1/hosts/. Edit all references in the code if you use a different path.
Add this to /jffs/scripts/services-start
Create or add to /jffs/configs/dnsmasq.conf.add
To update the hosts file (to run it manually, and automatically as a cron job and when rebooting the rooter) create /jffs/scripts/update-hosts.sh
Set all files in /jffs/scripts/ as executable
Create /tmp/mnt/sda1/hosts/whitelist.txt and fill it with domains you want to whitelist, one domain per line. An example would look like so:
Create /tmp/mnt/sda1/hosts/blacklist.txt and fill it with domains you want to blacklist that are not in the combined hosts file, one domain per line. Add 0.0.0.0 to every line as in this example:
These two files (whitelist.txt and blacklist.txt) need to be present for the scripts to work. Leave the files empty if you have no white/blacklist domains.
Reboot and enjoy!
3. Notes
A note for Windows users: You need to flush the DNS Cache when you tinker with your Router.
In a command prompt enter ipconfig/flushdns and hit Enter.
On Windows 7 or 8 Systems this generally needs Administrator privileges (hit Windowskey and type cmd. In the list, right-click on cmd (.exe) and choose 'run as Administrator').
Also, clear your browsers cache after this.
4. Quick Disable / Enable Adblocking
To temporarily disable the hosts files remove the following, depending on which method you use:
1. Standard method (the original)
in /jffs/configs/dnsmasq.conf.add remove this:
2. Advanced method
in /jffs/configs/dnsmasq.conf.add remove this:
And then restart dnsmasq (or reboot the router):
Please check the Diversion website for latest version and discussion link.
These snippets of code posted here are now a automated install script with control center character under the name of AB-Solution.
This is a simple but effective way to block ads on all your devices
------------------------------------------------------------
Changelog
------------------------------------------------------------
September 06, 2015: Added Quick Enable / Disable Adblocking, TOC added
August 31, 2015: Removed requirement for Entware, it was never really needed.
April 10, 2015: Typo, changed addn-hosts=/tmp/mnt/sda1/hosts/hosts.blocked.txt to [...]/hosts.blocked in script dnsmasq.conf.add, thanks tijaune.
April 08, 2015: Added Advanced method
March 31, 2015: Added preparatory steps for jffs
March 21, 2015: Simplified install steps.
March 20, 2015: Adding the cron job with /jffs/scripts/services-start instead of /jffs/scripts/init-start for compatibility with latest firmware.
March 17, 2015: If DNS-Filtering is enabled in your routers interface AdBlocking will not work. The DNS Filter bypasses Dnsmasq settings. See this thread for details.
April 30, 2014: If you want to remove some domains appearing in the hosts.clean you can do this with a whitelist file. See post 65 in this thread.
If you want to go even further with my ad-blocking solution, e2301010 has posted AdBlocking with combined hosts file with Lighttpd.
March 14, 2014: wget scripts are modified to reflect the much better and smarter findings of decedion.
------------------------------------------------------------
Table of content
------------------------------------------------------------
Indroduction
1. Standard method (the original)
2. Advanced method
3. Notes
4. Quick Enable / Disable Adblocking
------------------------------------------------------------
Introduction
This combines well maintained hosts files into one, with no duplicate entries.
The resulting file is about 728 KB (1450 KB Advanced method) containing more than 26000 (52600) lines of blocked domains.
Preparatory steps for both methods:
In Administration / System set to Yes:
- Enable JFFS partition*
- Enable JFFS custom scripts and configs
*) If this has not been set before set 'Format JFFS partition at next boot' to Yes and Reboot.
In this post two ways to use the combined hosts file are explained, the Standard and Advanced method. The Standard method is the original method, updated as per the Changelog. On April 04, 2015 the Advanced method was added with additional hosts files and support for a black- and whitelist of domains.
Note: The two methods are not compatible, use only one in your router.
1. Standard method (the original)
The files are stored on a USB disk, /tmp/mnt/sda1/ is the path. If you use /jffs/ edit accordingly.
Using a USB disk we need to delay the startscript.
This will create an up-to-date hosts file after every reboot.
Add to /jffs/scripts/post-mount
Code:
#!/bin/sh
sleep 12
wget -qO- "http://winhelp2002.mvps.org/hosts.txt" "http://someonewhocares.org/hosts/zero/hosts" "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext&useip=0.0.0.0" | grep -w ^0.0.0.0 | sed $'s/\r$//' | sort -u > /tmp/mnt/sda1/hosts.clean
sleep 5
service restart_dnsmasq
Create or add to /jffs/configs/dnsmasq.conf.add
Code:
address=/0.0.0.0/0.0.0.0
addn-hosts=/tmp/mnt/sda1/hosts.clean
Code:
#!/bin/sh
wget -qO- "http://winhelp2002.mvps.org/hosts.txt" "http://someonewhocares.org/hosts/zero/hosts" "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext&useip=0.0.0.0" | grep -w ^0.0.0.0 | sed $'s/\r$//' | sort -u > /tmp/mnt/sda1/hosts.clean
sleep 5
service restart_dnsmasq
Code:
#!/bin/sh
cru a UpdateHosts "00 00 * * 5 /jffs/scripts/update-hosts.sh"
Code:
chmod a+rx /jffs/scripts/*
Tested on a RT-AC66U.
2. Advanced method
This method supports a black- and a whitelist for domains you want to include/exclude in the hosts file. It uses 5 well maintained hosts files (Standard method uses 3). Thank you Weble for the input in post #143.
Note: The code below is NOT compatible with the Standard method!
This will create an up-to-date hosts file after every reboot and update the hosts file with cron every Friday at midnight.
Note that the files are stored in /tmp/mnt/sda1/hosts/. Edit all references in the code if you use a different path.
Add this to /jffs/scripts/services-start
Code:
#!/bin/sh
sleep 10
cru a UpdateHosts "00 00 * * 5 /jffs/scripts/update-hosts.sh"
sleep 5
source /jffs/scripts/update-hosts.sh
Create or add to /jffs/configs/dnsmasq.conf.add
Code:
# AdBlocking
address=/0.0.0.0/0.0.0.0
ptr-record=0.0.0.0.in-addr.arpa,0.0.0.0
addn-hosts=/tmp/mnt/sda1/hosts/hosts.blocked
addn-hosts=/tmp/mnt/sda1/hosts/blacklist.txt
To update the hosts file (to run it manually, and automatically as a cron job and when rebooting the rooter) create /jffs/scripts/update-hosts.sh
Code:
#!/bin/sh
# set directory
dir=/tmp/mnt/sda1/hosts
# get hosts files and combine and sort, write to temp file
wget -qO- "http://winhelp2002.mvps.org/hosts.txt" "http://someonewhocares.org/hosts/zero/hosts" "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&startdate[day]=&startdate[month]=&startdate[year]=&mimetype=plaintext&useip=0.0.0.0" | grep -w ^0.0.0.0 | sed $'s/\r$//' | sort -u > $dir/temp
#get hosts files combine and convert 127.0.0.1 to 0.0.0.0 and output to temp
wget -qO- "http://www.malwaredomainlist.com/hostslist/hosts.txt" "http://hosts-file.net/ad_servers.txt" | grep -w ^127.0.0.1 | sed s/127.0.0.1/0.0.0.0/g | sed $'s/\r$//' >> $dir/temp
# remove whitelisted entries in temp and write final file, remove temp file
cat $dir/whitelist.txt | sed $'s/\r$//' | grep -vf - $dir/temp > $dir/hosts.blocked
#remove temp file
rm $dir/temp
#restart dnsmasq to apply changes
sleep 1
service restart_dnsmasq
Set all files in /jffs/scripts/ as executable
Code:
chmod a+rx /jffs/scripts/*
Create /tmp/mnt/sda1/hosts/whitelist.txt and fill it with domains you want to whitelist, one domain per line. An example would look like so:
Code:
clients2.google.com
something.other.com
anotherdomain.com
Create /tmp/mnt/sda1/hosts/blacklist.txt and fill it with domains you want to blacklist that are not in the combined hosts file, one domain per line. Add 0.0.0.0 to every line as in this example:
Code:
0.0.0.0 www.rivalo.com
0.0.0.0 rivalo.com
0.0.0.0 madpandatv.net
0.0.0.0 www.snapdo.com
Reboot and enjoy!
3. Notes
A note for Windows users: You need to flush the DNS Cache when you tinker with your Router.
In a command prompt enter ipconfig/flushdns and hit Enter.
On Windows 7 or 8 Systems this generally needs Administrator privileges (hit Windowskey and type cmd. In the list, right-click on cmd (.exe) and choose 'run as Administrator').
Also, clear your browsers cache after this.
4. Quick Disable / Enable Adblocking
To temporarily disable the hosts files remove the following, depending on which method you use:
1. Standard method (the original)
in /jffs/configs/dnsmasq.conf.add remove this:
Code:
address=/0.0.0.0/0.0.0.0
addn-hosts=/tmp/mnt/sda1/hosts.clean
in /jffs/configs/dnsmasq.conf.add remove this:
Code:
# AdBlocking
address=/0.0.0.0/0.0.0.0
ptr-record=0.0.0.0.in-addr.arpa,0.0.0.0
addn-hosts=/tmp/mnt/sda1/hosts/hosts.blocked
addn-hosts=/tmp/mnt/sda1/hosts/blacklist.txt
And then restart dnsmasq (or reboot the router):
Code:
service restart_dnsmasq
Last edited: