EdgeRouter : Activate the DNS filtering of add servers

By | February 10, 2017

Everybody hates Add Servers so all my PC and mobile browsers use AddBlock+, but still I do not want the extra traffic that is done even by using ad blockers.
The best solution is to simply block from the EdgeRouter Ubiquiti Networks ERLite-3 EdgeRouter Lite the ad sites. The best guide for this is:
EdgeMAX – Ad-blocking (content filtering) using EdgeRouter

If you want to have your own extra ad servers (for example the Internet resource list referred in the script from the tutorial does not include a lot of Romanian ad servers), you can create your own list and host it on your site.
See my list:serverlist

See bellow the replicated steps from the tutorial adapted to my router

Step 1:
Create the file /config/user-data/update-adblock-dnsmasq.sh and ad the following lines (alternatively you can download the file attached to this post and copy it in /config/user-data directory of your router )


#!/bin/bash
ad_list_url="https://blog.voina.be/wp-content/uploads/2015/10/serverlist.txt"
#The IP address below should point to the IP of your router or to 0.0.0.0
pixelserv_ip="0.0.0.0"
ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf"
temp_ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf.tmp"
curl -s -k $ad_list_url | sed "s/127\.0\.0\.1/$pixelserv_ip/" > $temp_ad_file
if [ -f "$temp_ad_file" ]
then
#sed -i -e '/www\.favoritesite\.com/d' $temp_ad_file
mv $temp_ad_file $ad_file
else
echo "Error building the ad list, please try again."
exit
fi

/etc/init.d/dnsmasq force-reload

In the above script there is a line starting with “#sed “. You can uncomment that, and modify it to remove your favorite sites from the ad blocking list so you can continue to support them. You can add as many of those lines as you’d like. One example would be:


sed -i -e '/ads\.stackoverflow\.com/d' $temp_ad_file

Step 2:
Run:


chmod a+x /config/user-data/update-adblock-dnsmasq.sh

to make the script executable

Step 3:
Test it by running it
/config/user-data/update-adblock-dnsmasq.sh
This will create the file /etc/dnsmasq.d/dnsmasq.adlist.conf that will be read every time that dnsmasq starts.

Note:

  • to be able to access the https resource with a self-signed certificate ad -k to curl command
  • ad the custom list https://blog.voina.be/wp-content/uploads/2015/10/serverlist.txt
  • An alternative to the web resource for the adds list is to use an internal hosted file and access it by sftp. This was the solution in my setup due to the fact that the EdgeRouter itself cannot see the blog.voina.org resource. The resource is hidden due to the way port forwarding works. The script to update the list changes to:


    #!/bin/bash

    ad_list_url="sftp://internal_server_ip/media/storage/www/html/owncloud/wordpress/wp-content/uploads/2015/10/serverlist.txt"
    #The IP address below should point to the IP of your router or to 0.0.0.0
    pixelserv_ip="0.0.0.0"
    ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf"
    temp_ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf.tmp"
    credentials="user:password"

    curl -k -u $credentials $ad_list_url | sed "s/127\.0\.0\.1/$pixelserv_ip/" > $temp_ad_file

    if [ -f "$temp_ad_file" ]
    then
    #sed -i -e '/www\.favoritesite\.com/d' $temp_ad_file
    mv $temp_ad_file $ad_file
    else
    echo "Error building the ad list, please try again."
    exit
    fi
    /etc/init.d/dnsmasq force-reload

    The best way to discover new ad servers is by using the Ghostery Firefox Plugin. Look at the servers that the plugin finds and blocks and copy them into your custom list.

    The second source of add servers is AddBlock+ plugin which can be used to block unknown add server also. You can then copy the name of the add server from the custom AddBlock+ list from your browser and add it to your custom list.

    After a new add server is added to the serverlist.txt resource you only have to connect to the EdgeRouter CLI, connect as admin (sudo -i) and rerun /config/user-data/update-adblock-dnsmasq.sh

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.