mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-15 02:42:58 +00:00
Space/Tab query.sh
Signed-off-by: Dan Schaper <dan.schaper@pi-hole.net>
This commit is contained in:
parent
a08ea28feb
commit
8f727c1f35
1 changed files with 126 additions and 126 deletions
|
@ -26,42 +26,42 @@ source "${colfile}"
|
||||||
# Print each subdomain
|
# Print each subdomain
|
||||||
# e.g: foo.bar.baz.com = "foo.bar.baz.com bar.baz.com baz.com com"
|
# e.g: foo.bar.baz.com = "foo.bar.baz.com bar.baz.com baz.com com"
|
||||||
processWildcards() {
|
processWildcards() {
|
||||||
IFS="." read -r -a array <<< "${1}"
|
IFS="." read -r -a array <<< "${1}"
|
||||||
for (( i=${#array[@]}-1; i>=0; i-- )); do
|
for (( i=${#array[@]}-1; i>=0; i-- )); do
|
||||||
ar=""
|
ar=""
|
||||||
for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
|
for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
|
||||||
if [[ $j == $((${#array[@]}-1)) ]]; then
|
if [[ $j == $((${#array[@]}-1)) ]]; then
|
||||||
ar="${array[$j]}"
|
ar="${array[$j]}"
|
||||||
else
|
else
|
||||||
ar="${array[$j]}.${ar}"
|
ar="${array[$j]}.${ar}"
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
|
echo "${ar}"
|
||||||
done
|
done
|
||||||
echo "${ar}"
|
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Scan an array of files for matching strings
|
# Scan an array of files for matching strings
|
||||||
scanList(){
|
scanList(){
|
||||||
# Escape full stops
|
# Escape full stops
|
||||||
local domain="${1//./\\.}" lists="${2}" type="${3:-}"
|
local domain="${1//./\\.}" lists="${2}" type="${3:-}"
|
||||||
|
|
||||||
# Prevent grep from printing file path
|
# Prevent grep from printing file path
|
||||||
cd "$piholeDir" || exit 1
|
cd "$piholeDir" || exit 1
|
||||||
|
|
||||||
# Prevent grep -i matching slowly: http://bit.ly/2xFXtUX
|
# Prevent grep -i matching slowly: http://bit.ly/2xFXtUX
|
||||||
export LC_CTYPE=C
|
export LC_CTYPE=C
|
||||||
|
|
||||||
# /dev/null forces filename to be printed when only one list has been generated
|
# /dev/null forces filename to be printed when only one list has been generated
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
case "${type}" in
|
case "${type}" in
|
||||||
"exact" ) grep -i -E -l "(^|\\s)${domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
|
"exact" ) grep -i -E -l "(^|\\s)${domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
|
||||||
"wc" ) grep -i -o -m 1 "/${domain}/" ${lists} 2>/dev/null;;
|
"wc" ) grep -i -o -m 1 "/${domain}/" ${lists} 2>/dev/null;;
|
||||||
* ) grep -i "${domain}" ${lists} /dev/null 2>/dev/null;;
|
* ) grep -i "${domain}" ${lists} /dev/null 2>/dev/null;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ "${options}" == "-h" ]] || [[ "${options}" == "--help" ]]; then
|
if [[ "${options}" == "-h" ]] || [[ "${options}" == "--help" ]]; then
|
||||||
echo "Usage: pihole -q [option] <domain>
|
echo "Usage: pihole -q [option] <domain>
|
||||||
Example: 'pihole -q -exact domain.com'
|
Example: 'pihole -q -exact domain.com'
|
||||||
Query the adlists for a specified domain
|
Query the adlists for a specified domain
|
||||||
|
|
||||||
|
@ -74,19 +74,19 @@ Options:
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -e "$adListsList" ]]; then
|
if [[ ! -e "$adListsList" ]]; then
|
||||||
echo -e "${COL_LIGHT_RED}The file $adListsList was not found${COL_NC}"
|
echo -e "${COL_LIGHT_RED}The file $adListsList was not found${COL_NC}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Handle valid options
|
# Handle valid options
|
||||||
if [[ "${options}" == *"-bp"* ]]; then
|
if [[ "${options}" == *"-bp"* ]]; then
|
||||||
exact="exact"; blockpage=true
|
exact="exact"; blockpage=true
|
||||||
else
|
else
|
||||||
[[ "${options}" == *"-adlist"* ]] && adlist=true
|
[[ "${options}" == *"-adlist"* ]] && adlist=true
|
||||||
[[ "${options}" == *"-all"* ]] && all=true
|
[[ "${options}" == *"-all"* ]] && all=true
|
||||||
if [[ "${options}" == *"-exact"* ]]; then
|
if [[ "${options}" == *"-exact"* ]]; then
|
||||||
exact="exact"; matchType="exact ${matchType}"
|
exact="exact"; matchType="exact ${matchType}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Strip valid options, leaving only the domain and invalid options
|
# Strip valid options, leaving only the domain and invalid options
|
||||||
|
@ -96,59 +96,59 @@ options=$(sed -E 's/ ?-(bp|adlists?|all|exact) ?//g' <<< "${options}")
|
||||||
# Handle remaining options
|
# Handle remaining options
|
||||||
# If $options contain non ASCII characters, convert to punycode
|
# If $options contain non ASCII characters, convert to punycode
|
||||||
case "${options}" in
|
case "${options}" in
|
||||||
"" ) str="No domain specified";;
|
"" ) str="No domain specified";;
|
||||||
*" "* ) str="Unknown query option specified";;
|
*" "* ) str="Unknown query option specified";;
|
||||||
*[![:ascii:]]* ) domainQuery=$(idn2 "${options}");;
|
*[![:ascii:]]* ) domainQuery=$(idn2 "${options}");;
|
||||||
* ) domainQuery="${options}";;
|
* ) domainQuery="${options}";;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ -n "${str:-}" ]]; then
|
if [[ -n "${str:-}" ]]; then
|
||||||
echo -e "${str}${COL_NC}\\nTry 'pihole -q --help' for more information."
|
echo -e "${str}${COL_NC}\\nTry 'pihole -q --help' for more information."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Scan Whitelist and Blacklist
|
# Scan Whitelist and Blacklist
|
||||||
lists="whitelist.txt blacklist.txt"
|
lists="whitelist.txt blacklist.txt"
|
||||||
mapfile -t results <<< "$(scanList "${domainQuery}" "${lists}" "${exact}")"
|
mapfile -t results <<< "$(scanList "${domainQuery}" "${lists}" "${exact}")"
|
||||||
if [[ -n "${results[*]}" ]]; then
|
if [[ -n "${results[*]}" ]]; then
|
||||||
wbMatch=true
|
wbMatch=true
|
||||||
# Loop through each result in order to print unique file title once
|
# Loop through each result in order to print unique file title once
|
||||||
for result in "${results[@]}"; do
|
for result in "${results[@]}"; do
|
||||||
fileName="${result%%.*}"
|
fileName="${result%%.*}"
|
||||||
if [[ -n "${blockpage}" ]]; then
|
if [[ -n "${blockpage}" ]]; then
|
||||||
echo "π ${result}"
|
echo "π ${result}"
|
||||||
exit 0
|
exit 0
|
||||||
elif [[ -n "${exact}" ]]; then
|
elif [[ -n "${exact}" ]]; then
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
||||||
else
|
else
|
||||||
# Only print filename title once per file
|
# Only print filename title once per file
|
||||||
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
||||||
fileName_prev="${fileName}"
|
fileName_prev="${fileName}"
|
||||||
fi
|
fi
|
||||||
echo " ${result#*:}"
|
echo " ${result#*:}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Scan Wildcards
|
# Scan Wildcards
|
||||||
if [[ -e "${wildcardlist}" ]]; then
|
if [[ -e "${wildcardlist}" ]]; then
|
||||||
# Determine all subdomains, domain and TLDs
|
# Determine all subdomains, domain and TLDs
|
||||||
mapfile -t wildcards <<< "$(processWildcards "${domainQuery}")"
|
mapfile -t wildcards <<< "$(processWildcards "${domainQuery}")"
|
||||||
for match in "${wildcards[@]}"; do
|
for match in "${wildcards[@]}"; do
|
||||||
# Search wildcard list for matches
|
# Search wildcard list for matches
|
||||||
mapfile -t results <<< "$(scanList "${match}" "${wildcardlist}" "wc")"
|
mapfile -t results <<< "$(scanList "${match}" "${wildcardlist}" "wc")"
|
||||||
if [[ -n "${results[*]}" ]]; then
|
if [[ -n "${results[*]}" ]]; then
|
||||||
if [[ -z "${wcMatch:-}" ]] && [[ -z "${blockpage}" ]]; then
|
if [[ -z "${wcMatch:-}" ]] && [[ -z "${blockpage}" ]]; then
|
||||||
wcMatch=true
|
wcMatch=true
|
||||||
echo " ${matchType^} found in ${COL_BOLD}Wildcards${COL_NC}:"
|
echo " ${matchType^} found in ${COL_BOLD}Wildcards${COL_NC}:"
|
||||||
fi
|
fi
|
||||||
case "${blockpage}" in
|
case "${blockpage}" in
|
||||||
true ) echo "π ${wildcardlist##*/}"; exit 0;;
|
true ) echo "π ${wildcardlist##*/}"; exit 0;;
|
||||||
* ) echo " *.${match}";;
|
* ) echo " *.${match}";;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get version sorted *.domains filenames (without dir path)
|
# Get version sorted *.domains filenames (without dir path)
|
||||||
|
@ -159,81 +159,81 @@ mapfile -t results <<< "$(scanList "${domainQuery}" "${lists[*]}" "${exact}")"
|
||||||
|
|
||||||
# Handle notices
|
# Handle notices
|
||||||
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
|
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
|
||||||
echo -e " ${INFO} No ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC} within the block lists"
|
echo -e " ${INFO} No ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC} within the block lists"
|
||||||
exit 0
|
exit 0
|
||||||
elif [[ -z "${results[*]}" ]]; then
|
elif [[ -z "${results[*]}" ]]; then
|
||||||
# Result found in WL/BL/Wildcards
|
# Result found in WL/BL/Wildcards
|
||||||
exit 0
|
exit 0
|
||||||
elif [[ -z "${all}" ]] && [[ "${#results[*]}" -ge 100 ]]; then
|
elif [[ -z "${all}" ]] && [[ "${#results[*]}" -ge 100 ]]; then
|
||||||
echo -e " ${INFO} Over 100 ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC}
|
echo -e " ${INFO} Over 100 ${exact/t/t }results found for ${COL_BOLD}${domainQuery}${COL_NC}
|
||||||
This can be overridden using the -all option"
|
This can be overridden using the -all option"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove unwanted content from non-exact $results
|
# Remove unwanted content from non-exact $results
|
||||||
if [[ -z "${exact}" ]]; then
|
if [[ -z "${exact}" ]]; then
|
||||||
# Delete lines starting with #
|
# Delete lines starting with #
|
||||||
# Remove comments after domain
|
# Remove comments after domain
|
||||||
# Remove hosts format IP address
|
# Remove hosts format IP address
|
||||||
mapfile -t results <<< "$(IFS=$'\n'; sed \
|
mapfile -t results <<< "$(IFS=$'\n'; sed \
|
||||||
-e "/:#/d" \
|
-e "/:#/d" \
|
||||||
-e "s/[ \\t]#.*//g" \
|
-e "s/[ \\t]#.*//g" \
|
||||||
-e "s/:.*[ \\t]/:/g" \
|
-e "s/:.*[ \\t]/:/g" \
|
||||||
<<< "${results[*]}")"
|
<<< "${results[*]}")"
|
||||||
# Exit if result was in a comment
|
# Exit if result was in a comment
|
||||||
[[ -z "${results[*]}" ]] && exit 0
|
[[ -z "${results[*]}" ]] && exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get adlist file content as array
|
# Get adlist file content as array
|
||||||
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
||||||
for adlistUrl in $(< "${adListsList}"); do
|
for adlistUrl in $(< "${adListsList}"); do
|
||||||
if [[ "${adlistUrl:0:4}" =~ (http|www.) ]]; then
|
if [[ "${adlistUrl:0:4}" =~ (http|www.) ]]; then
|
||||||
adlists+=("${adlistUrl}")
|
adlists+=("${adlistUrl}")
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Print "Exact matches for" title
|
# Print "Exact matches for" title
|
||||||
if [[ -n "${exact}" ]] && [[ -z "${blockpage}" ]]; then
|
if [[ -n "${exact}" ]] && [[ -z "${blockpage}" ]]; then
|
||||||
plural=""; [[ "${#results[*]}" -gt 1 ]] && plural="es"
|
plural=""; [[ "${#results[*]}" -gt 1 ]] && plural="es"
|
||||||
echo " ${matchType^}${plural} for ${COL_BOLD}${domainQuery}${COL_NC} found in:"
|
echo " ${matchType^}${plural} for ${COL_BOLD}${domainQuery}${COL_NC} found in:"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for result in "${results[@]}"; do
|
for result in "${results[@]}"; do
|
||||||
fileName="${result/:*/}"
|
fileName="${result/:*/}"
|
||||||
|
|
||||||
# Determine *.domains URL using filename's number
|
# Determine *.domains URL using filename's number
|
||||||
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
||||||
fileNum="${fileName/list./}"; fileNum="${fileNum%%.*}"
|
fileNum="${fileName/list./}"; fileNum="${fileNum%%.*}"
|
||||||
fileName="${adlists[$fileNum]}"
|
fileName="${adlists[$fileNum]}"
|
||||||
|
|
||||||
# Discrepency occurs when adlists has been modified, but Gravity has not been run
|
# Discrepency occurs when adlists has been modified, but Gravity has not been run
|
||||||
if [[ -z "${fileName}" ]]; then
|
if [[ -z "${fileName}" ]]; then
|
||||||
fileName="${COL_LIGHT_RED}(no associated adlists URL found)${COL_NC}"
|
fileName="${COL_LIGHT_RED}(no associated adlists URL found)${COL_NC}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "${blockpage}" ]]; then
|
if [[ -n "${blockpage}" ]]; then
|
||||||
echo "${fileNum} ${fileName}"
|
echo "${fileNum} ${fileName}"
|
||||||
elif [[ -n "${exact}" ]]; then
|
elif [[ -n "${exact}" ]]; then
|
||||||
echo " ${fileName}"
|
echo " ${fileName}"
|
||||||
else
|
|
||||||
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
|
||||||
count=""
|
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName}${COL_NC}:"
|
|
||||||
fileName_prev="${fileName}"
|
|
||||||
fi
|
|
||||||
: $((count++))
|
|
||||||
|
|
||||||
# Print matching domain if $max_count has not been reached
|
|
||||||
[[ -z "${all}" ]] && max_count="50"
|
|
||||||
if [[ -z "${all}" ]] && [[ "${count}" -ge "${max_count}" ]]; then
|
|
||||||
[[ "${count}" -gt "${max_count}" ]] && continue
|
|
||||||
echo " ${COL_GRAY}Over ${count} results found, skipping rest of file${COL_NC}"
|
|
||||||
else
|
else
|
||||||
echo " ${result#*:}"
|
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
||||||
|
count=""
|
||||||
|
echo " ${matchType^} found in ${COL_BOLD}${fileName}${COL_NC}:"
|
||||||
|
fileName_prev="${fileName}"
|
||||||
|
fi
|
||||||
|
: $((count++))
|
||||||
|
|
||||||
|
# Print matching domain if $max_count has not been reached
|
||||||
|
[[ -z "${all}" ]] && max_count="50"
|
||||||
|
if [[ -z "${all}" ]] && [[ "${count}" -ge "${max_count}" ]]; then
|
||||||
|
[[ "${count}" -gt "${max_count}" ]] && continue
|
||||||
|
echo " ${COL_GRAY}Over ${count} results found, skipping rest of file${COL_NC}"
|
||||||
|
else
|
||||||
|
echo " ${result#*:}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
done
|
done
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue