Skip to content

Instantly share code, notes, and snippets.

@Fabian1976
Forked from Ar0xA/update-adblock-dnsmasq.sh
Created February 13, 2017 06:52
Show Gist options
  • Save Fabian1976/b2a69b4c1a16e2694c20c248c7f30a2b to your computer and use it in GitHub Desktop.
Save Fabian1976/b2a69b4c1a16e2694c20c248c7f30a2b to your computer and use it in GitHub Desktop.

Revisions

  1. @Ar0xA Ar0xA created this gist Feb 12, 2017.
    65 changes: 65 additions & 0 deletions update-adblock-dnsmasq.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash
    #
    #DNS adblock/malware block for USG
    #
    #Orginal script: https://community.ubnt.com/t5/UniFi-Routing-Switching/Use-USG-to-block-sites-apps-like-ER/td-p/1497045
    #
    #Howto: SSH into your USG:
    #sudo su -
    #vi /config/user-data/update-adblock-dnsmasq.sh (add file content)
    #ESC :wq
    #chmod +x /config/user-data/update-adblock-dnsmasq.sh
    #/config/user-data/update-adblock-dnsmasq.sh
    #
    #check if all went fine by nslookup on a box that uses your USG as DNS (default from DHCP)
    #>nslookup zyban.1.p21.info (should return address: 0.0.0.0)
    #
    #crontab -l should show you now a line to automatically update once a day
    #
    #enjoy!
    # @ar0xa

    if grep -q adblock /var/spool/cron/crontabs/root
    then
    echo "Cron OK"
    else
    echo "0 3 * * 0 /config/user-data/update-adblock-dnsmasq.sh" >> /var/spool/cron/crontabs/root
    fi

    ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf"
    temp_ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf.tmp"

    # get two hosts files with 127.0.0.1 adress, extract out only ad blocking lists and write them to a temporary file
    curl -s "http://hosts-file.net/ad_servers.txt" "http://www.malwaredomainlist.com/hostslist/hosts.txt" | grep -w ^127.0.0.1$'\(\t\| \)' | sed 's/^127.0.0.1\s\{1,\}//' > $temp_ad_file

    # get another two hosts files with 0.0.0.0 adress, extract out only ad blocking lists, remove whitespace at the end of some lines and output it to temp
    curl -s "http://winhelp2002.mvps.org/hosts.txt" "http://someonewhocares.org/hosts/zero/hosts" | grep -w ^0.0.0.0 | cut -c 9- | sed 's/\s\{1,\}.*//' >> $temp_ad_file

    # remove the carriage return at the end of each line of the temporary file, and convert it into a Dnsmasq format
    sed -i -e 's/\r$//; s:.*:address=/&/0\.0\.0\.0:' $temp_ad_file

    # get another hosts file in Dnsmasq format, and add the contents to a temporary file
    curl -s "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext&useip=0.0.0.0" >> $temp_ad_file

    if [ -f "$temp_ad_file" ]
    then
    # sort ad blocking list in the temp file and remove duplicate lines from it
    sort -o $temp_ad_file -t '/' -uk2 $temp_ad_file

    # uncomment the line below, and modify it to remove your favorite sites from the ad blocking list
    sed -i -e '/spclient\.wg\.spotify\.com/d' $temp_ad_file
    mv $temp_ad_file $ad_file
    else
    echo "Error building the ad list, please try again."
    exit
    fi

    #
    ## add some specific add hosts
    #
    echo 'address=/aa.i-stream.pl/0.0.0.0' >> $ad_file

    #
    ## restart dnsmasq
    /etc/init.d/dnsmasq force-reload