mirror of
https://github.com/pi-hole/pi-hole
synced 2025-01-03 04:30:55 +00:00
pihole -q should also scan gravity table
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
5457b2c6ea
commit
922ce7359c
78
advanced/Scripts/query.sh
Normal file → Executable file
78
advanced/Scripts/query.sh
Normal file → Executable file
@ -53,7 +53,6 @@ Example: 'pihole -q -exact domain.com'
|
|||||||
Query the adlists for a specified domain
|
Query the adlists for a specified domain
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-adlist Print the name of the block list URL
|
|
||||||
-exact Search the block lists for exact domain matches
|
-exact Search the block lists for exact domain matches
|
||||||
-all Return all query matches within a block list
|
-all Return all query matches within a block list
|
||||||
-h, --help Show this help dialog"
|
-h, --help Show this help dialog"
|
||||||
@ -64,7 +63,6 @@ fi
|
|||||||
if [[ "${options}" == *"-bp"* ]]; then
|
if [[ "${options}" == *"-bp"* ]]; then
|
||||||
exact="exact"; blockpage=true
|
exact="exact"; blockpage=true
|
||||||
else
|
else
|
||||||
[[ "${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}"
|
||||||
@ -99,10 +97,17 @@ scanDatabaseTable() {
|
|||||||
# Underscores are SQLite wildcards matching exactly one character. We obviously want to suppress this
|
# Underscores are SQLite wildcards matching exactly one character. We obviously want to suppress this
|
||||||
# behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched
|
# behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched
|
||||||
# as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores.
|
# as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores.
|
||||||
case "${type}" in
|
if [[ "${table}" == "gravity" ]]; then
|
||||||
"exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
|
case "${type}" in
|
||||||
* ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
"exact" ) querystr="SELECT gravity.domain,adlist.address FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE domain = '${domain}'";;
|
||||||
esac
|
* ) querystr="SELECT gravity.domain,adlist.address FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
||||||
|
esac
|
||||||
|
else
|
||||||
|
case "${type}" in
|
||||||
|
"exact" ) querystr="SELECT domain FROM ${table} WHERE domain = '${domain}'";;
|
||||||
|
* ) querystr="SELECT domain FROM ${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
# Send prepared query to gravity database
|
# Send prepared query to gravity database
|
||||||
result="$(sqlite3 "${gravityDBfile}" "${querystr}")" 2> /dev/null
|
result="$(sqlite3 "${gravityDBfile}" "${querystr}")" 2> /dev/null
|
||||||
@ -111,6 +116,11 @@ scanDatabaseTable() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ "${table}" == "gravity" ]]; then
|
||||||
|
echo "${result}"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
# Mark domain as having been white-/blacklist matched (global variable)
|
# Mark domain as having been white-/blacklist matched (global variable)
|
||||||
wbMatch=true
|
wbMatch=true
|
||||||
|
|
||||||
@ -170,32 +180,15 @@ scanRegexDatabaseTable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Scan Whitelist and Blacklist
|
# Scan Whitelist and Blacklist
|
||||||
scanDatabaseTable "${domainQuery}" "whitelist" "${exact}"
|
scanDatabaseTable "${domainQuery}" "vw_whitelist" "${exact}"
|
||||||
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
|
scanDatabaseTable "${domainQuery}" "vw_blacklist" "${exact}"
|
||||||
|
|
||||||
# Scan Regex table
|
# Scan Regex table
|
||||||
scanRegexDatabaseTable "${domainQuery}" "whitelist"
|
scanRegexDatabaseTable "${domainQuery}" "whitelist"
|
||||||
scanRegexDatabaseTable "${domainQuery}" "blacklist"
|
scanRegexDatabaseTable "${domainQuery}" "blacklist"
|
||||||
|
|
||||||
# Get version sorted *.domains filenames (without dir path)
|
# Query block lists
|
||||||
lists=("$(cd "$piholeDir" || exit 0; printf "%s\\n" -- *.domains | sort -V)")
|
mapfile -t results <<< "$(scanDatabaseTable "${domainQuery}" "gravity" "${exact}")"
|
||||||
|
|
||||||
# Query blocklists for occurences of domain
|
|
||||||
mapfile -t results <<< "$(scanList "${domainQuery}" "${lists[*]}" "${exact}")"
|
|
||||||
|
|
||||||
# Remove unwanted content from $results
|
|
||||||
# Each line in $results is formatted as such: [fileName]:[line]
|
|
||||||
# 1. Delete lines starting with #
|
|
||||||
# 2. Remove comments after domain
|
|
||||||
# 3. Remove hosts format IP address
|
|
||||||
# 4. Remove any lines that no longer contain the queried domain name (in case the matched domain name was in a comment)
|
|
||||||
esc_domain="${domainQuery//./\\.}"
|
|
||||||
mapfile -t results <<< "$(IFS=$'\n'; sed \
|
|
||||||
-e "/:#/d" \
|
|
||||||
-e "s/[ \\t]#.*//g" \
|
|
||||||
-e "s/:.*[ \\t]/:/g" \
|
|
||||||
-e "/${esc_domain}/!d" \
|
|
||||||
<<< "${results[*]}")"
|
|
||||||
|
|
||||||
# Handle notices
|
# Handle notices
|
||||||
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
|
if [[ -z "${wbMatch:-}" ]] && [[ -z "${wcMatch:-}" ]] && [[ -z "${results[*]}" ]]; then
|
||||||
@ -210,12 +203,6 @@ elif [[ -z "${all}" ]] && [[ "${#results[*]}" -ge 100 ]]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get adlist file content as array
|
|
||||||
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
|
||||||
# Retrieve source URLs from gravity database
|
|
||||||
mapfile -t adlists <<< "$(sqlite3 "${gravityDBfile}" "SELECT address FROM vw_adlist;" 2> /dev/null)"
|
|
||||||
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"
|
||||||
@ -223,28 +210,17 @@ if [[ -n "${exact}" ]] && [[ -z "${blockpage}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
for result in "${results[@]}"; do
|
for result in "${results[@]}"; do
|
||||||
fileName="${result/:*/}"
|
adlistAddress="${result/*|/}"
|
||||||
|
|
||||||
# Determine *.domains URL using filename's number
|
|
||||||
if [[ -n "${adlist}" ]] || [[ -n "${blockpage}" ]]; then
|
|
||||||
fileNum="${fileName/list./}"; fileNum="${fileNum%%.*}"
|
|
||||||
fileName="${adlists[$fileNum]}"
|
|
||||||
|
|
||||||
# Discrepency occurs when adlists has been modified, but Gravity has not been run
|
|
||||||
if [[ -z "${fileName}" ]]; then
|
|
||||||
fileName="${COL_LIGHT_RED}(no associated adlists URL found)${COL_NC}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "${blockpage}" ]]; then
|
if [[ -n "${blockpage}" ]]; then
|
||||||
echo "${fileNum} ${fileName}"
|
echo "${fileNum} ${adlistAddress}"
|
||||||
elif [[ -n "${exact}" ]]; then
|
elif [[ -n "${exact}" ]]; then
|
||||||
echo " ${fileName}"
|
echo " - ${adlistAddress}"
|
||||||
else
|
else
|
||||||
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
if [[ ! "${adlistAddress}" == "${adlistAddress_prev:-}" ]]; then
|
||||||
count=""
|
count=""
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName}${COL_NC}:"
|
echo " ${matchType^} found in ${COL_BOLD}${adlistAddress}${COL_NC}:"
|
||||||
fileName_prev="${fileName}"
|
adlistAddress_prev="${adlistAddress}"
|
||||||
fi
|
fi
|
||||||
: $((count++))
|
: $((count++))
|
||||||
|
|
||||||
@ -254,7 +230,7 @@ for result in "${results[@]}"; do
|
|||||||
[[ "${count}" -gt "${max_count}" ]] && continue
|
[[ "${count}" -gt "${max_count}" ]] && continue
|
||||||
echo " ${COL_GRAY}Over ${count} results found, skipping rest of file${COL_NC}"
|
echo " ${COL_GRAY}Over ${count} results found, skipping rest of file${COL_NC}"
|
||||||
else
|
else
|
||||||
echo " ${result#*:}"
|
echo " ${result/*|//}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user