mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-12-25 06:10:20 +00:00
Merge pull request #1417 from WaLLy3K/tweak/queryFunc
QueryFunc() rewrite
This commit is contained in:
commit
5e0572637e
1 changed files with 33 additions and 50 deletions
83
pihole
83
pihole
|
@ -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}"
|
|
||||||
else
|
|
||||||
grep -i "${domain}" "${list}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
processWildcards() {
|
if [[ ${method} == "-exact" ]]; then
|
||||||
IFS="." read -r -a array <<< "${1}"
|
grep -i -E -l "(^|\s|\/)${domain}($|\s|\/)" ${list}
|
||||||
for (( i=${#array[@]}-1; i>=0; i-- )); do
|
else
|
||||||
ar=""
|
grep -i "${domain}" ${list}
|
||||||
for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
|
fi
|
||||||
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 ""
|
|
||||||
else
|
|
||||||
echo "::: ${list} does not exist"
|
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Scan for possible wildcard matches
|
|
||||||
if [ -e "${wildcardlist}" ]; then
|
|
||||||
local wildcards=($(processWildcards "${domain}"))
|
|
||||||
for domain in ${wildcards[@]}; do
|
|
||||||
result=$(scanList "\/${domain}\/" ${wildcardlist})
|
|
||||||
# Remove empty lines before couting number of results
|
|
||||||
count=$(sed '/^\s*$/d' <<< "$result" | wc -l)
|
|
||||||
if [[ ${count} > 0 ]]; then
|
|
||||||
echo "::: Wildcard blocking ${domain} (${count} results)"
|
|
||||||
echo "${result}"
|
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Scan Blacklist and Wildcards
|
||||||
|
lists="/etc/pihole/blacklist.txt $wildcardlist"
|
||||||
|
result=$(scanList ${domain} "${lists}" ${method})
|
||||||
|
if [ -n "$result" ]; then
|
||||||
|
echo "$result"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Scan Domains lists
|
||||||
|
result=$(scanList ${domain} "/etc/pihole/*.domains" ${method})
|
||||||
|
if [ -n "$result" ]; then
|
||||||
|
sort -t . -k 2 -g <<< "$result"
|
||||||
|
else
|
||||||
|
[ -n "$method" ] && exact="exact "
|
||||||
|
echo "::: No ${exact}results found for ${domain}"
|
||||||
|
fi
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue