pi-hole/advanced/Scripts/whitelist.sh

144 lines
3 KiB
Bash
Raw Normal View History

2015-12-06 13:55:50 +00:00
#!/usr/bin/env bash
2016-01-30 20:12:40 +00:00
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Whitelists domains
2015-12-06 13:55:50 +00:00
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
2016-04-13 14:51:48 +00:00
helpFunc() {
cat << EOM
::: Immediately whitelists one or more domains in the hosts file
:::
::: Usage: pihole -w domain1 [domain2 ...]
:::
::: Options:
::: -d, --delmode Remove domains from the whitelist
::: -nr, --noreload Update Whitelist without refreshing dnsmasq
::: -q, --quiet output is less verbose
::: -h, --help Show this help dialog
::: -l, --list Display your whitelisted domains
EOM
2016-05-25 21:07:12 +00:00
exit 1
}
if [[ $# = 0 ]]; then
helpFunc
fi
#globals
2016-04-04 20:08:56 +00:00
basename=pihole
2016-08-21 01:12:02 +00:00
piholeDir=/etc/${basename}
adList=${piholeDir}/gravity.list
whitelist=${piholeDir}/whitelist.txt
reload=true
addmode=true
verbose=true
2016-04-04 20:08:56 +00:00
domList=()
domToRemoveList=()
HandleOther(){
#check validity of domain
validDomain=$(echo "$1" | perl -ne'print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/')
if [ -z "$validDomain" ]; then
2016-01-24 16:46:00 +00:00
echo "::: $1 is not a valid argument or domain name"
else
2016-08-21 01:12:02 +00:00
domList=("${domList[@]}" ${validDomain})
fi
}
PopWhitelistFile() {
#check whitelist file exists, and if not, create it
2016-08-21 01:12:02 +00:00
if [[ ! -f ${whitelist} ]];then
touch ${whitelist}
fi
for dom in "${domList[@]}"
do
2016-08-21 01:12:02 +00:00
if ${addmode}; then
AddDomain "$dom"
else
RemoveDomain "$dom"
fi
done
}
AddDomain() {
#| sed 's/\./\\./g'
bool=false
2016-08-21 01:12:02 +00:00
grep -Ex -q "$1" ${whitelist} || bool=true
if ${bool}; then
#domain not found in the whitelist file, add it!
2016-08-21 01:12:02 +00:00
if ${verbose}; then
2016-04-04 23:03:24 +00:00
echo -n "::: Adding $1 to $whitelist..."
fi
2016-08-21 01:12:02 +00:00
echo "$1" >> ${whitelist}
if ${verbose}; then
2016-01-24 16:46:00 +00:00
echo " done!"
fi
else
2016-08-21 01:12:02 +00:00
if ${verbose}; then
2016-04-04 23:03:24 +00:00
echo "::: $1 already exists in $whitelist, no need to add!"
2016-01-15 15:54:00 +00:00
fi
fi
}
RemoveDomain() {
bool=false
2016-08-21 01:12:02 +00:00
grep -Ex -q "$1" ${whitelist} || bool=true
if ${bool}; then
#Domain is not in the whitelist file, no need to Remove
2016-08-21 01:12:02 +00:00
if ${verbose}; then
2016-01-24 16:46:00 +00:00
echo "::: $1 is NOT whitelisted! No need to remove"
2016-01-15 15:54:00 +00:00
fi
else
echo "$1" | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' ${whitelist}
fi
}
Reload() {
# Reload hosts file
pihole -g -sd
}
DisplayWlist() {
verbose=false
2016-04-08 14:14:17 +00:00
echo -e " Displaying Gravity Resistant Domains \n"
count=1
while IFS= read -r RD
do
echo "${count}: $RD"
count=$((count+1))
done < "$whitelist"
}
###################################################
for var in "$@"
do
case "$var" in
"-nr"| "--noreload" ) reload=false;;
2016-01-15 15:54:00 +00:00
"-d" | "--delmode" ) addmode=false;;
"-q" | "--quiet" ) verbose=false;;
2016-04-08 14:14:17 +00:00
"-h" | "--help" ) helpFunc;;
"-l" | "--list" ) DisplayWlist;;
* ) HandleOther "$var";;
esac
done
PopWhitelistFile
2016-08-21 01:12:02 +00:00
if ${reload}; then
Reload
fi