Update queryFunc()

* Allow scanList() to search files using a wildcard by removing quotes wrapped around `${list}`
* scanList() will not provide a domain ouput on each string if exact is specified (`grep -l`)
* Remove unused processWildcards() function
* Return a message if no domain is specified
* IDN domains are converted to punycode when running a `pihole -q` search if the `python` package is available, otherwise will revert to current behaviour
* Scan Blacklist & Wildcards first, exiting from search if a match is found (Fixes #1330)
* Use one `grep` subshell to search for all "*.domains" lists at once (opposed to looping to get every matching file name, and then spawning a `grep` instance for every matching file)
* queryFunc() will not return "(0 results)" output from files where no match is found
* Sort results based off list number
* Return a message if no results are found
pull/1417/head
WaLLy3K 7 years ago committed by GitHub
parent 8c657910ae
commit a0603ad3b7

@ -84,62 +84,45 @@ scanList(){
domain="${1}" domain="${1}"
list="${2}" list="${2}"
method="${3}" method="${3}"
if [[ ${method} == "-exact" ]] ; then
grep -i -E "(^|\s)${domain}($|\s)" "${list}" if [[ ${method} == "-exact" ]]; then
grep -i -E -l "(^|\/)${domain}($|\/)" ${list}
else else
grep -i "${domain}" "${list}" grep -i "${domain}" ${list}
fi fi
} }
processWildcards() {
IFS="." read -r -a array <<< "${1}"
for (( i=${#array[@]}-1; i>=0; i-- )); do
ar=""
for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
if [[ $j == $((${#array[@]}-1)) ]]; then
ar="${array[$j]}"
else
ar="${array[$j]}.${ar}"
fi
done
echo "${ar}"
done
}
queryFunc() { queryFunc() {
domain="${2}"
method="${3}" method="${3}"
lists=( /etc/pihole/list.* /etc/pihole/blacklist.txt)
for list in ${lists[@]}; do # If domain contains non ASCII characters, convert domain to punycode if python exists
if [ -e "${list}" ]; then # Cr: https://serverfault.com/a/335079
result=$(scanList ${domain} ${list} ${method}) if [ -z "${2}" ]; then
# Remove empty lines before couting number of results echo "::: No domain specified"
count=$(sed '/^\s*$/d' <<< "$result" | wc -l) exit 1
echo "::: ${list} (${count} results)" elif [[ ${2} = *[![:ascii:]]* ]]; then
if [[ ${count} > 0 ]]; then [ `which python` ] && domain=$(python -c 'import sys;print sys.argv[1].decode("utf-8").encode("idna")' "${2}")
echo "${result}" else
fi domain="${2}"
echo "" fi
else
echo "::: ${list} does not exist" # Scan Blacklist and Wildcards
echo "" lists="/etc/pihole/blacklist.txt $wildcardlist"
fi result=$(scanList ${domain} "${lists}" ${method})
done if [ -n "$result" ]; then
echo "$result"
# Scan for possible wildcard matches exit 0
if [ -e "${wildcardlist}" ]; then fi
local wildcards=($(processWildcards "${domain}"))
for domain in ${wildcards[@]}; do # Scan Domains lists
result=$(scanList "\/${domain}\/" ${wildcardlist}) result=$(scanList ${domain} "/etc/pihole/*.domains" ${method})
# Remove empty lines before couting number of results if [ -n "$result" ]; then
count=$(sed '/^\s*$/d' <<< "$result" | wc -l) sort -t . -k 2 -g <<< "$result"
if [[ ${count} > 0 ]]; then else
echo "::: Wildcard blocking ${domain} (${count} results)" [ -n "$method" ] && exact="exact "
echo "${result}" echo "::: No ${exact}results found for ${domain}"
echo ""
fi
done
fi fi
exit 0 exit 0
} }

Loading…
Cancel
Save