hostblocker

System-Wide adblocker for GNU/Linux
git clone https://src.jayvii.de/pub/hostblocker.git
Log | Files | Refs | README

hostblockingscript.sh (3821B)


      1 #!/bin/bash
      2 # Credits to "Ben" (Administrator) from CupOfLinux.
      3 
      4 # check for root rights
      5 if [ ! $(whoami) == root ]; then
      6 	echo "Please run this script with root-rights!"
      7 	echo "su -c \"sh hostblockingscript.sh\" root"
      8 	echo "sudo ./hostblockingscript.sh"
      9 	exit 1;
     10 fi
     11 
     12 # backup "/etc/hosts"
     13 if [ ! -f /etc/hosts.BAK ]; then
     14 	cp /etc/hosts /etc/hosts.BAK
     15 fi
     16 
     17 # counting blocked domains
     18 Blockeddomainsold=$(cat /etc/hosts | grep "0.0.0.0" | wc -l)
     19 
     20 # temporary files to safe blocked domains
     21 Selfdestructinghosts=$(mktemp)
     22 Selfdestructinghosts2=$(mktemp)
     23 
     24 # User-output
     25 echo ""
     26 echo "[ HOSTBLOCKINGSCRIPT ]"
     27 echo "https://notabug.org/jayvii/Scripts/src/master/Adblocking"
     28 echo ""
     29 echo "##########"
     30 
     31 # download lists
     32 
     33 echo "Getting (outdated) DisconnectMe-list..."
     34 wget -q -O - https://raw.githubusercontent.com/disconnectme/disconnect/b27abbf033c6f80f157fe9d98cb767c87065fbf4/firefox/content/disconnect.safariextension/opera/chrome/scripts/data.js >> $Selfdestructinghosts
     35 
     36 echo "Getting Adblock Easy-list..."
     37 wget -q -O - https://easylist-downloads.adblockplus.org/easylist.txt >> $Selfdestructinghosts
     38 
     39 echo "Getting Adblock EasyPrivacy-list..."
     40 wget -q -O - https://easylist-downloads.adblockplus.org/easyprivacy.txt >> $Selfdestructinghosts
     41 
     42 echo "Getting Adblock AntiAdblock-list..."
     43 wget -q -O - https://easylist-downloads.adblockplus.org/antiadblockfilters.txt >> $Selfdestructinghosts
     44 
     45 echo "Getting Adblock MalewareDomain-list..."
     46 wget -q -O - https://easylist-downloads.adblockplus.org/malwaredomains_full.txt >> $Selfdestructinghosts
     47 
     48 echo "Getting Adblock FanboyAnnoyance-list..."
     49 wget -q -O - https://easylist-downloads.adblockplus.org/fanboy-annoyance.txt >> $Selfdestructinghosts
     50 
     51 echo "Getting FanboySocial-list..."
     52 wget -q -O - https://easylist-downloads.adblockplus.org/fanboy-social.txt >> $Selfdestructinghosts
     53 
     54 echo "Getting Winhelp2002 Hosts-list..."
     55 wget -q -O - http://winhelp2002.mvps.org/hosts.txt >> $Selfdestructinghosts
     56 
     57 echo "Getting HostsFile AdServer-list..."
     58 wget -q -O - http://hosts-file.net/ad_servers.asp >> $Selfdestructinghosts
     59 
     60 echo "Getting SomeoneWhoCares Hosts-file..."
     61 wget -q -O - http://someonewhocares.org/hosts/hosts >> $Selfdestructinghosts
     62 
     63 echo "Getting NoTrack Trackers-list..."
     64 wget -q -O - https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt >> $Selfdestructinghosts
     65 
     66 echo "Getting NoTrack MaliciousSites-list..."
     67 wget -q -O - https://raw.githubusercontent.com/quidsup/notrack/master/malicious-sites.txt >> $Selfdestructinghosts
     68 
     69 echo "Getting MalewareDomainList Hosts-list..."
     70 wget -q -O - http://www.malwaredomainlist.com/hostslist/hosts.txt >> $Selfdestructinghosts
     71 
     72 echo "Getting Spam404 Hosts-list..."
     73 wget -q -O - https://raw.githubusercontent.com/Dawsey21/Lists/master/adblock-list.txt >> $Selfdestructinghosts
     74 
     75 # https://raw.githubusercontent.com/disconnectme/disconnect-tracking-protection/master/services.json
     76 # https://s3.amazonaws.com/lists.disconnect.me/entitylist.json
     77 
     78 sed -e 's/\r//' -e '/^0.0.0.0/!d' -e '/localhost/d' -e 's/0.0.0.0/0.0.0.0/' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < $Selfdestructinghosts | sort -u > $Selfdestructinghosts2
     79 
     80 # create a master hosts file
     81 echo -e "\n#Hostslist created "$(date) | cat /etc/hosts.BAK - $Selfdestructinghosts2 > /etc/hosts.NEW
     82 
     83 # cleaning
     84 rm -rf $Selfdestructinghosts $Selfdestructinghosts2
     85 
     86 # replace current /etc/hosts with our new one.
     87 cp /etc/hosts.NEW /etc/hosts
     88 
     89 # counting blocked domains
     90 Blockeddomainsnew=$(cat /etc/hosts | grep "0.0.0.0" | wc -l)
     91 
     92 # final output for user
     93 if [ "$1" == "libnotify" ]; then
     94 	notify-send "HostBlockingScript" "Done! Replaced $Blockeddomainsold with $Blockeddomainsnew blocks." -t 0
     95 else
     96 	echo "##########"
     97 	echo ""
     98 	echo "Done!"
     99 	echo "Previously blocked domains: $Blockeddomainsold"
    100 	echo "Currently blocked domains:  $Blockeddomainsnew"
    101 fi