2014-09-26 14:28:44 +00:00
|
|
|
#!/bin/bash
|
2015-03-30 11:08:32 +00:00
|
|
|
# The Pi-hole now blocks over 90,000 ad domains
|
2014-09-26 14:28:44 +00:00
|
|
|
# Address to send ads to (the RPi)
|
2015-02-08 22:58:47 +00:00
|
|
|
piholeIP="127.0.0.1"
|
2015-03-23 18:43:13 +00:00
|
|
|
# Optionally, uncomment to automatically detect the local IP address.
|
2015-03-23 18:47:35 +00:00
|
|
|
#piholeIP=$(hostname -I)
|
2015-02-17 19:52:54 +00:00
|
|
|
|
2014-09-26 14:28:44 +00:00
|
|
|
# Config file to hold URL rules
|
|
|
|
eventHorizion="/etc/dnsmasq.d/adList.conf"
|
2015-03-30 11:08:32 +00:00
|
|
|
piholeDir='/etc/pihole/'
|
|
|
|
|
|
|
|
blacklist=$piholeDir'blacklist.txt'
|
|
|
|
whitelist=$piholeDir'whitelist.txt'
|
2015-03-10 17:27:57 +00:00
|
|
|
|
|
|
|
# Create the pihole resource directory if it doesn't exist. Future files will be stored here
|
2015-03-30 11:08:32 +00:00
|
|
|
if [[ -d $piholeDir ]];then
|
2015-03-10 17:27:57 +00:00
|
|
|
:
|
|
|
|
else
|
|
|
|
echo "Forming pihole directory..."
|
2015-03-30 11:08:32 +00:00
|
|
|
sudo mkdir $piholeDir
|
2015-03-10 17:27:57 +00:00
|
|
|
fi
|
2014-09-26 14:28:44 +00:00
|
|
|
|
2015-03-30 11:08:32 +00:00
|
|
|
tmpDir='/tmp/'
|
|
|
|
tmpAdList=$tmpDir'matter.pihole.txt'
|
|
|
|
tmpConf=$tmpDir'andLight.pihole.txt'
|
|
|
|
tmpWhiteList=$tmpDir'yang.pihole.txt'
|
|
|
|
tmpBlackList=$tmpDir'yin.pihole.txt'
|
|
|
|
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting yoyo ad list..." # Approximately 2452 domains at the time of writing
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s -d mimetype=plaintext -d hostformat=unixhosts http://pgl.yoyo.org/adservers/serverlist.php? | sort > $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting winhelp2002 ad list..." # 12985 domains
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s http://winhelp2002.mvps.org/hosts.txt | grep -v "#" | grep -v "127.0.0.1" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting adaway ad list..." # 445 domains
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s https://adaway.org/hosts.txt | grep -v "#" | grep -v "::1" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting hosts-file ad list..." # 28050 domains
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s http://hosts-file.net/.%5Cad_servers.txt | grep -v "#" | grep -v "::1" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting malwaredomainlist ad list..." # 1352 domains
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s http://www.malwaredomainlist.com/hostslist/hosts.txt | grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $3}' | grep -v '^\\' | grep -v '\\$' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting adblock.gjtech ad list..." # 696 domains
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s http://adblock.gjtech.net/?format=unix-hosts | grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting someone who cares ad list..." # 10600
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -s http://someonewhocares.org/hosts/hosts | grep -v "#" | sed '/^$/d' | sed 's/\ /\\ /g' | grep -v '^\\' | grep -v '\\$' | awk '{print $2}' | grep -v '^\\' | grep -v '\\$' >> $tmpAdList
|
2015-02-17 19:52:54 +00:00
|
|
|
echo "Getting Mother of All Ad Blocks list..." # 102168 domains!! Thanks Kacy
|
2015-03-30 11:08:32 +00:00
|
|
|
curl -A 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0' -e http://forum.xda-developers.com/ http://adblock.mahakala.is/ | grep -v "#" | awk '{print $2}' >> $tmpAdList
|
2014-09-26 14:28:44 +00:00
|
|
|
|
2015-03-30 11:08:32 +00:00
|
|
|
# Add entries from the local blacklist file if it exists in $piholeDir directory
|
|
|
|
echo -n "" > $tmpBlackList
|
2015-03-19 22:09:36 +00:00
|
|
|
if [[ -f $blacklist ]];then
|
2015-03-30 11:08:32 +00:00
|
|
|
echo "Getting the local blacklist from $piholeDir directory"
|
|
|
|
awk -F'[# \t]' 'NF>0&&$1!="" {print $1}' $blacklist > $tmpBlackList
|
|
|
|
cat $tmpBlackList >> $tmpAdList
|
2015-03-19 22:09:36 +00:00
|
|
|
fi
|
|
|
|
|
2015-03-30 11:08:32 +00:00
|
|
|
# Clean-up entries from the local whitelist file if it exists in $piholeDir directory
|
|
|
|
echo -n "^$" > $tmpWhiteList
|
2015-02-22 23:54:03 +00:00
|
|
|
if [[ -f $whitelist ]];then
|
2015-03-30 11:08:32 +00:00
|
|
|
echo "Getting the local whitelist from $piholeDir directory"
|
|
|
|
awk -F'[# \t]' 'NF>0&&$1!="" {print $1"$"}' $whitelist > $tmpWhiteList
|
2015-02-22 23:54:03 +00:00
|
|
|
fi
|
2015-02-17 19:52:54 +00:00
|
|
|
|
2015-03-30 11:08:32 +00:00
|
|
|
# Sort the aggregated results and remove any duplicates
|
|
|
|
# Remove entries from the whitelist file if it exists in $piholeDir folder
|
|
|
|
echo "Removing duplicates, whitelisting, and formatting the list of domains..."
|
|
|
|
grep -vhE "^\s*(#|$)" $tmpAdList|
|
|
|
|
sed $'s/\r$//'|
|
|
|
|
awk -F. '{for (i=NF; i>1; --i) printf "%s.",$i;print $1}'|
|
2015-03-30 20:59:18 +00:00
|
|
|
sort -t'.' -k1,2| uniq |
|
2015-03-30 11:08:32 +00:00
|
|
|
awk -F. 'NR!=1&&substr($0,1,length(p))==p {next} {p=$0".";for (i=NF; i>1; --i) printf "%s.",$i;print $1}'|
|
2015-03-30 20:59:18 +00:00
|
|
|
grep -vwf $tmpWhiteList |
|
2015-03-30 11:08:32 +00:00
|
|
|
awk -v "IP=$piholeIP" '{sub(/\r$/,""); print "address=/"$0"/"IP}' > $tmpConf
|
|
|
|
numberOfSitesWhitelisted=$(cat $tmpWhiteList | wc -l | sed 's/^[ \t]*//')
|
|
|
|
numberOfSitesBlacklisted=$(cat $tmpBlackList | wc -l | sed 's/^[ \t]*//')
|
|
|
|
echo "$numberOfSitesWhitelisted domain(s) whitelisted, $numberOfSitesBlacklisted domain(s) blacklisted."
|
|
|
|
|
2015-02-22 23:54:03 +00:00
|
|
|
# Count how many domains/whitelists were added so it can be displayed to the user
|
2015-03-30 11:08:32 +00:00
|
|
|
numberOfAdsBlocked=$(cat $tmpConf | wc -l | sed 's/^[ \t]*//')
|
2015-02-22 23:54:03 +00:00
|
|
|
echo "$numberOfAdsBlocked ad domains blocked."
|
2015-02-17 19:52:54 +00:00
|
|
|
|
|
|
|
# Turn the file into a dnsmasq config file
|
2015-03-30 11:08:32 +00:00
|
|
|
sudo mv $tmpConf $eventHorizion
|
2014-09-26 14:28:44 +00:00
|
|
|
|
|
|
|
# Restart DNS
|
2015-03-19 21:59:55 +00:00
|
|
|
sudo service dnsmasq restart
|