mirror of
https://github.com/pi-hole/pi-hole
synced 2025-01-03 04:30:55 +00:00
Add blacklisting wildcard support
This commit is contained in:
parent
1f9b0f7cef
commit
40798da6b1
@ -15,6 +15,7 @@ basename=pihole
|
|||||||
piholeDir=/etc/${basename}
|
piholeDir=/etc/${basename}
|
||||||
whitelist=${piholeDir}/whitelist.txt
|
whitelist=${piholeDir}/whitelist.txt
|
||||||
blacklist=${piholeDir}/blacklist.txt
|
blacklist=${piholeDir}/blacklist.txt
|
||||||
|
readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard"
|
||||||
reload=false
|
reload=false
|
||||||
addmode=true
|
addmode=true
|
||||||
verbose=true
|
verbose=true
|
||||||
@ -47,13 +48,17 @@ helpFunc() {
|
|||||||
::: -h, --help Show this help dialog
|
::: -h, --help Show this help dialog
|
||||||
::: -l, --list Display your ${word}listed domains
|
::: -l, --list Display your ${word}listed domains
|
||||||
EOM
|
EOM
|
||||||
|
if [[ "letter" == "b" ]]; then
|
||||||
|
echo "::: -wild, --wildcard Add whitecard entry (only blacklist)"
|
||||||
|
fi
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
EscapeRegexp() {
|
EscapeRegexp() {
|
||||||
# This way we may safely insert an arbitrary
|
# This way we may safely insert an arbitrary
|
||||||
# string in our regular expressions
|
# string in our regular expressions
|
||||||
echo $* | sed "s/[]\\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
# Also remove leading "." if present
|
||||||
|
echo $* | sed 's/^\.//' | sed "s/[]\\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleOther(){
|
HandleOther(){
|
||||||
@ -89,22 +94,51 @@ AddDomain() {
|
|||||||
list="$2"
|
list="$2"
|
||||||
domain=$(EscapeRegexp "$1")
|
domain=$(EscapeRegexp "$1")
|
||||||
|
|
||||||
bool=true
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
||||||
#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
|
bool=true
|
||||||
#domain not found in the whitelist file, add it!
|
#Is the domain in the list we want to add it to?
|
||||||
if [[ "${verbose}" == true ]]; then
|
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
||||||
echo "::: Adding $1 to $list..."
|
|
||||||
fi
|
if [[ "${bool}" == false ]]; then
|
||||||
reload=true
|
#domain not found in the whitelist file, add it!
|
||||||
# Add it to the list we want to add it to
|
if [[ "${verbose}" == true ]]; then
|
||||||
echo "$1" >> ${list}
|
echo "::: Adding $1 to $list..."
|
||||||
else
|
fi
|
||||||
if [[ "${verbose}" == true ]]; then
|
reload=true
|
||||||
echo "::: ${1} already exists in ${list}, no need to add!"
|
# Add it to the list we want to add it to
|
||||||
fi
|
echo "$1" >> "${list}"
|
||||||
|
else
|
||||||
|
if [[ "${verbose}" == true ]]; then
|
||||||
|
echo "::: ${1} already exists in ${list}, no need to add!"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
||||||
|
|
||||||
|
source "${piholeDir}/setupVars.conf"
|
||||||
|
#Remove the /* from the end of the IPv4addr.
|
||||||
|
IPV4_ADDRESS=${IPV4_ADDRESS%/*}
|
||||||
|
IPV6_ADDRESS=${IPV6_ADDRESS}
|
||||||
|
|
||||||
|
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
|
||||||
|
echo "::: Adding $1 to wildcard blacklist..."
|
||||||
|
fi
|
||||||
|
reload=true
|
||||||
|
echo "address=/$1/${IPV4_ADDRESS}" >> "${wildcardlist}"
|
||||||
|
if [[ ${#IPV6_ADDRESS} > 0 ]] ; then
|
||||||
|
echo "address=/$1/${IPV6_ADDRESS}" >> "${wildcardlist}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ "${verbose}" == true ]]; then
|
||||||
|
echo "::: ${1} already exists in wildcard blacklist, no need to add!"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,18 +146,38 @@ RemoveDomain() {
|
|||||||
list="$2"
|
list="$2"
|
||||||
domain=$(EscapeRegexp "$1")
|
domain=$(EscapeRegexp "$1")
|
||||||
|
|
||||||
bool=true
|
if [[ "${list}" == "${whitelist}" || "${list}" == "${blacklist}" ]]; then
|
||||||
#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
|
bool=true
|
||||||
if [[ "${bool}" == true ]]; then
|
#Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa
|
||||||
# Remove it from the other one
|
grep -Ex -q "${domain}" "${list}" > /dev/null 2>&1 || bool=false
|
||||||
echo "::: Removing $1 from $list..."
|
if [[ "${bool}" == true ]]; then
|
||||||
# /I flag: search case-insensitive
|
# Remove it from the other one
|
||||||
sed -i "/${domain}/Id" ${list}
|
echo "::: Removing $1 from $list..."
|
||||||
reload=true
|
# /I flag: search case-insensitive
|
||||||
else
|
sed -i "/${domain}/Id" "${list}"
|
||||||
if [[ "${verbose}" == true ]]; then
|
reload=true
|
||||||
echo "::: ${1} does not exist in ${list}, no need to remove!"
|
else
|
||||||
|
if [[ "${verbose}" == true ]]; then
|
||||||
|
echo "::: ${1} does not exist in ${list}, no need to remove!"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
elif [[ "${list}" == "${wildcardlist}" ]]; then
|
||||||
|
|
||||||
|
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 "::: Removing $1 from $list..."
|
||||||
|
# /I flag: search case-insensitive
|
||||||
|
sed -i "/address=\/${domain}/Id" "${list}"
|
||||||
|
reload=true
|
||||||
|
else
|
||||||
|
if [[ "${verbose}" == true ]]; then
|
||||||
|
echo "::: ${1} does not exist in ${list}, no need to remove!"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@ -153,6 +207,7 @@ for var in "$@"; do
|
|||||||
case "${var}" in
|
case "${var}" in
|
||||||
"-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";;
|
"-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";;
|
||||||
"-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";;
|
"-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";;
|
||||||
|
"-wild" | "wildcard" ) listMain="${wildcardlist}";;
|
||||||
"-nr"| "--noreload" ) reload=false;;
|
"-nr"| "--noreload" ) reload=false;;
|
||||||
"-d" | "--delmode" ) addmode=false;;
|
"-d" | "--delmode" ) addmode=false;;
|
||||||
"-f" | "--force" ) force=true;;
|
"-f" | "--force" ) force=true;;
|
||||||
|
6
pihole
6
pihole
@ -37,6 +37,11 @@ blacklistFunc() {
|
|||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wildcardFunc() {
|
||||||
|
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
debugFunc() {
|
debugFunc() {
|
||||||
"${PI_HOLE_SCRIPT_DIR}"/piholeDebug.sh
|
"${PI_HOLE_SCRIPT_DIR}"/piholeDebug.sh
|
||||||
exit 0
|
exit 0
|
||||||
@ -274,6 +279,7 @@ fi
|
|||||||
case "${1}" in
|
case "${1}" in
|
||||||
"-w" | "whitelist" ) whitelistFunc "$@";;
|
"-w" | "whitelist" ) whitelistFunc "$@";;
|
||||||
"-b" | "blacklist" ) blacklistFunc "$@";;
|
"-b" | "blacklist" ) blacklistFunc "$@";;
|
||||||
|
"-wild" | "wildcard" ) wildcardFunc "$@";;
|
||||||
"-d" | "debug" ) debugFunc;;
|
"-d" | "debug" ) debugFunc;;
|
||||||
"-f" | "flush" ) flushFunc;;
|
"-f" | "flush" ) flushFunc;;
|
||||||
"-up" | "updatePihole" ) updatePiholeFunc;;
|
"-up" | "updatePihole" ) updatePiholeFunc;;
|
||||||
|
Loading…
Reference in New Issue
Block a user