2016-10-26 08:36:02 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
2017-02-22 17:55:20 +00:00
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
2017-05-14 01:11:44 +00:00
|
|
|
# Whitelist and blacklist domains
|
2016-10-26 08:36:02 +00:00
|
|
|
#
|
2017-02-22 17:55:20 +00:00
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
# Globals
|
2016-10-26 08:36:02 +00:00
|
|
|
basename=pihole
|
2018-02-18 20:18:52 +00:00
|
|
|
piholeDir=/etc/"${basename}"
|
|
|
|
whitelist="${piholeDir}"/whitelist.txt
|
|
|
|
blacklist="${piholeDir}"/blacklist.txt
|
2016-12-31 13:41:19 +00:00
|
|
|
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
|
2016-10-26 08:36:02 +00:00
|
|
|
reload=false
|
|
|
|
addmode=true
|
|
|
|
verbose=true
|
|
|
|
|
|
|
|
domList=()
|
|
|
|
|
|
|
|
listMain=""
|
|
|
|
listAlt=""
|
|
|
|
|
2017-06-21 11:49:05 +00:00
|
|
|
colfile="/opt/pihole/COL_TABLE"
|
|
|
|
source ${colfile}
|
|
|
|
|
|
|
|
|
2016-10-26 08:36:02 +00:00
|
|
|
helpFunc() {
|
2017-05-14 01:11:44 +00:00
|
|
|
if [[ "${listMain}" == "${whitelist}" ]]; then
|
2017-05-16 00:18:32 +00:00
|
|
|
param="w"
|
|
|
|
type="white"
|
|
|
|
elif [[ "${listMain}" == "${wildcardlist}" ]]; then
|
|
|
|
param="wild"
|
|
|
|
type="wildcard black"
|
2017-05-14 01:11:44 +00:00
|
|
|
else
|
2017-05-16 00:18:32 +00:00
|
|
|
param="b"
|
|
|
|
type="black"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
|
|
|
|
2017-05-16 00:18:32 +00:00
|
|
|
echo "Usage: pihole -${param} [options] <domain> <domain2 ...>
|
|
|
|
Example: 'pihole -${param} site.com', or 'pihole -${param} site1.com site2.com'
|
|
|
|
${type^}list one or more domains
|
2017-05-14 01:11:44 +00:00
|
|
|
|
2017-05-16 00:23:53 +00:00
|
|
|
Options:
|
|
|
|
-d, --delmode Remove domain(s) from the ${type}list
|
2017-05-16 00:18:32 +00:00
|
|
|
-nr, --noreload Update ${type}list without refreshing dnsmasq
|
2017-05-14 01:11:44 +00:00
|
|
|
-q, --quiet Make output less verbose
|
|
|
|
-h, --help Show this help dialog
|
2017-10-07 15:29:47 +00:00
|
|
|
-l, --list Display all your ${type}listed domains
|
|
|
|
--nuke Removes all entries in a list"
|
2017-05-14 15:43:20 +00:00
|
|
|
|
2017-05-16 00:18:32 +00:00
|
|
|
exit 0
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 11:57:04 +00:00
|
|
|
EscapeRegexp() {
|
2017-05-14 01:11:44 +00:00
|
|
|
# This way we may safely insert an arbitrary
|
|
|
|
# string in our regular expressions
|
|
|
|
# Also remove leading "." if present
|
|
|
|
echo $* | sed 's/^\.*//' | sed "s/[]\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
2016-12-02 11:57:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
HandleOther() {
|
2017-06-30 12:52:33 +00:00
|
|
|
# Convert to lowercase
|
|
|
|
domain="${1,,}"
|
2017-05-14 01:11:44 +00:00
|
|
|
|
|
|
|
# Check validity of domain
|
2017-07-27 02:34:26 +00:00
|
|
|
if [[ "${#domain}" -le 253 ]]; then
|
|
|
|
validDomain=$(grep -P "^((-|_)*[a-z\d]((-|_)*[a-z\d])*(-|_)*)(\.(-|_)*([a-z\d]((-|_)*[a-z\d])*))*$" <<< "${domain}") # Valid chars check
|
|
|
|
validDomain=$(grep -P "^[^\.]{1,63}(\.[^\.]{1,63})*$" <<< "${validDomain}") # Length of each label
|
|
|
|
fi
|
2017-10-07 15:29:47 +00:00
|
|
|
|
2017-07-27 02:34:26 +00:00
|
|
|
if [[ -n "${validDomain}" ]]; then
|
2017-05-14 01:11:44 +00:00
|
|
|
domList=("${domList[@]}" ${validDomain})
|
2017-07-27 02:34:26 +00:00
|
|
|
else
|
|
|
|
echo -e " ${CROSS} ${domain} is not a valid argument or domain name!"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PoplistFile() {
|
2017-05-14 01:11:44 +00:00
|
|
|
# Check whitelist file exists, and if not, create it
|
2018-02-18 20:18:52 +00:00
|
|
|
if [[ ! -f "${whitelist}" ]]; then
|
|
|
|
touch "${whitelist}"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2017-05-14 15:43:20 +00:00
|
|
|
|
2018-02-18 12:00:56 +00:00
|
|
|
# Check blacklist file exists, and if not, create it
|
2018-02-18 20:18:52 +00:00
|
|
|
if [[ ! -f "${blacklist}" ]]; then
|
|
|
|
touch "${blacklist}"
|
2018-02-18 12:00:56 +00:00
|
|
|
fi
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
for dom in "${domList[@]}"; do
|
|
|
|
# Logic: If addmode then add to desired list and remove from the other; if delmode then remove from desired list but do not add to the other
|
|
|
|
if ${addmode}; then
|
|
|
|
AddDomain "${dom}" "${listMain}"
|
|
|
|
RemoveDomain "${dom}" "${listAlt}"
|
|
|
|
if [[ "${listMain}" == "${whitelist}" || "${listMain}" == "${blacklist}" ]]; then
|
|
|
|
RemoveDomain "${dom}" "${wildcardlist}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
RemoveDomain "${dom}" "${listMain}"
|
|
|
|
fi
|
|
|
|
done
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddDomain() {
|
2017-05-14 01:11:44 +00:00
|
|
|
list="$2"
|
|
|
|
domain=$(EscapeRegexp "$1")
|
2017-06-30 12:52:33 +00:00
|
|
|
|
2017-06-21 11:49:05 +00:00
|
|
|
[[ "${list}" == "${whitelist}" ]] && listname="whitelist"
|
|
|
|
[[ "${list}" == "${blacklist}" ]] && listname="blacklist"
|
|
|
|
[[ "${list}" == "${wildcardlist}" ]] && listname="wildcard blacklist"
|
2017-05-14 01:11:44 +00:00
|
|
|
|
|
|
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
2017-07-27 02:34:26 +00:00
|
|
|
[[ "${list}" == "${whitelist}" && -z "${type}" ]] && type="--whitelist-only"
|
|
|
|
[[ "${list}" == "${blacklist}" && -z "${type}" ]] && type="--blacklist-only"
|
2017-05-14 01:11:44 +00:00
|
|
|
bool=true
|
|
|
|
# Is the domain in the list we want to add it to?
|
|
|
|
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
|
|
|
|
|
|
|
if [[ "${bool}" == false ]]; then
|
|
|
|
# Domain not found in the whitelist file, add it!
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2017-06-21 11:49:05 +00:00
|
|
|
echo -e " ${INFO} Adding $1 to $listname..."
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
|
|
|
reload=true
|
|
|
|
# Add it to the list we want to add it to
|
|
|
|
echo "$1" >> "${list}"
|
|
|
|
else
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2017-06-21 11:49:05 +00:00
|
|
|
echo -e " ${INFO} ${1} already exists in ${listname}, no need to add!"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
|
|
|
source "${piholeDir}/setupVars.conf"
|
2017-06-24 15:14:23 +00:00
|
|
|
# Remove the /* from the end of the IP addresses
|
2017-05-14 01:11:44 +00:00
|
|
|
IPV4_ADDRESS=${IPV4_ADDRESS%/*}
|
2017-06-24 15:14:23 +00:00
|
|
|
IPV6_ADDRESS=${IPV6_ADDRESS%/*}
|
2017-07-27 02:34:26 +00:00
|
|
|
[[ -z "${type}" ]] && type="--wildcard-only"
|
2017-05-14 01:11:44 +00:00
|
|
|
bool=true
|
|
|
|
# Is the domain in the list?
|
|
|
|
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
|
|
|
|
|
|
|
if [[ "${bool}" == false ]]; then
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2017-06-21 11:49:05 +00:00
|
|
|
echo -e " ${INFO} Adding $1 to wildcard blacklist..."
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2017-07-24 11:26:39 +00:00
|
|
|
reload="restart"
|
2017-05-14 01:11:44 +00:00
|
|
|
echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}"
|
|
|
|
if [[ "${#IPV6_ADDRESS}" > 0 ]]; then
|
|
|
|
echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
2017-06-21 11:49:05 +00:00
|
|
|
echo -e " ${INFO} ${1} already exists in wildcard blacklist, no need to add!"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveDomain() {
|
2017-06-21 11:49:05 +00:00
|
|
|
list="$2"
|
|
|
|
domain=$(EscapeRegexp "$1")
|
2017-06-30 12:52:33 +00:00
|
|
|
|
2017-06-21 11:49:05 +00:00
|
|
|
[[ "${list}" == "${whitelist}" ]] && listname="whitelist"
|
|
|
|
[[ "${list}" == "${blacklist}" ]] && listname="blacklist"
|
|
|
|
[[ "${list}" == "${wildcardlist}" ]] && listname="wildcard blacklist"
|
|
|
|
|
|
|
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
|
|
|
bool=true
|
2017-07-27 02:34:26 +00:00
|
|
|
[[ "${list}" == "${whitelist}" && -z "${type}" ]] && type="--whitelist-only"
|
|
|
|
[[ "${list}" == "${blacklist}" && -z "${type}" ]] && type="--blacklist-only"
|
2017-06-21 11:49:05 +00:00
|
|
|
# Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa
|
|
|
|
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
|
|
|
if [[ "${bool}" == true ]]; then
|
|
|
|
# Remove it from the other one
|
|
|
|
echo -e " ${INFO} Removing $1 from $listname..."
|
|
|
|
# /I flag: search case-insensitive
|
|
|
|
sed -i "/${domain}/Id" "${list}"
|
|
|
|
reload=true
|
|
|
|
else
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
|
|
|
echo -e " ${INFO} ${1} does not exist in ${listname}, no need to remove!"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2017-06-21 11:49:05 +00:00
|
|
|
fi
|
|
|
|
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
2017-07-27 02:34:26 +00:00
|
|
|
[[ -z "${type}" ]] && type="--wildcard-only"
|
2017-06-21 11:49:05 +00:00
|
|
|
bool=true
|
|
|
|
# Is it in the list?
|
|
|
|
grep -e "address=\/${domain}\/" "${wildcardlist}" > /dev/null 2>&1 || bool=false
|
|
|
|
if [[ "${bool}" == true ]]; then
|
|
|
|
# Remove it from the other one
|
|
|
|
echo -e " ${INFO} Removing $1 from $listname..."
|
|
|
|
# /I flag: search case-insensitive
|
|
|
|
sed -i "/address=\/${domain}/Id" "${list}"
|
2017-07-27 02:34:26 +00:00
|
|
|
reload=true
|
2017-06-21 11:49:05 +00:00
|
|
|
else
|
|
|
|
if [[ "${verbose}" == true ]]; then
|
|
|
|
echo -e " ${INFO} ${1} does not exist in ${listname}, no need to remove!"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
2016-10-26 08:36:02 +00:00
|
|
|
fi
|
2017-06-21 11:49:05 +00:00
|
|
|
fi
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 11:26:39 +00:00
|
|
|
# Update Gravity
|
2016-10-26 08:36:02 +00:00
|
|
|
Reload() {
|
2017-06-21 11:49:05 +00:00
|
|
|
echo ""
|
2017-07-27 02:34:26 +00:00
|
|
|
pihole -g --skip-download "${type:-}"
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Displaylist() {
|
2017-06-21 11:49:05 +00:00
|
|
|
if [[ -f ${listMain} ]]; then
|
|
|
|
if [[ "${listMain}" == "${whitelist}" ]]; then
|
|
|
|
string="gravity resistant domains"
|
|
|
|
else
|
|
|
|
string="domains caught in the sinkhole"
|
|
|
|
fi
|
|
|
|
verbose=false
|
|
|
|
echo -e "Displaying $string:\n"
|
|
|
|
count=1
|
|
|
|
while IFS= read -r RD; do
|
|
|
|
echo " ${count}: ${RD}"
|
|
|
|
count=$((count+1))
|
|
|
|
done < "${listMain}"
|
2017-05-14 01:11:44 +00:00
|
|
|
else
|
2017-06-21 11:49:05 +00:00
|
|
|
echo -e " ${COL_LIGHT_RED}${listMain} does not exist!${COL_NC}"
|
2017-05-14 01:11:44 +00:00
|
|
|
fi
|
|
|
|
exit 0;
|
2016-10-26 08:36:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 15:29:47 +00:00
|
|
|
NukeList() {
|
|
|
|
if [[ -f "${listMain}" ]]; then
|
2017-10-07 15:46:40 +00:00
|
|
|
# Back up original list
|
2017-10-07 20:04:14 +00:00
|
|
|
cp "${listMain}" "${listMain}.bck~"
|
2017-10-07 15:46:40 +00:00
|
|
|
# Empty out file
|
2017-10-07 15:29:47 +00:00
|
|
|
echo "" > "${listMain}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-10-26 08:36:02 +00:00
|
|
|
for var in "$@"; do
|
2017-05-14 01:11:44 +00:00
|
|
|
case "${var}" in
|
|
|
|
"-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";;
|
|
|
|
"-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";;
|
|
|
|
"-wild" | "wildcard" ) listMain="${wildcardlist}";;
|
|
|
|
"-nr"| "--noreload" ) reload=false;;
|
|
|
|
"-d" | "--delmode" ) addmode=false;;
|
|
|
|
"-q" | "--quiet" ) verbose=false;;
|
|
|
|
"-h" | "--help" ) helpFunc;;
|
|
|
|
"-l" | "--list" ) Displaylist;;
|
2017-10-07 15:29:47 +00:00
|
|
|
"--nuke" ) NukeList;;
|
2017-05-14 01:11:44 +00:00
|
|
|
* ) HandleOther "${var}";;
|
|
|
|
esac
|
2016-10-26 08:36:02 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
2017-05-14 01:11:44 +00:00
|
|
|
helpFunc
|
2016-10-26 08:36:02 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
PoplistFile
|
|
|
|
|
2017-07-24 11:26:39 +00:00
|
|
|
if [[ "${reload}" != false ]]; then
|
|
|
|
# Ensure that "restart" is used for Wildcard updates
|
|
|
|
Reload "${reload}"
|
2016-10-26 08:36:02 +00:00
|
|
|
fi
|