Implement black- and whitelist searching with SQL statements. We use the ESCAPE clause in the LIKE query as the underscore "_" wildcard matches any single character but we want to suppress this behavior (underscores can be legitimate part of domains)
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
0bc112ce52
commit
3aa838bbe4
|
@ -102,29 +102,57 @@ if [[ -n "${str:-}" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Scan Whitelist and Blacklist
|
scanDatabaseTable() {
|
||||||
lists="whitelist.txt blacklist.txt"
|
local domain table type querystr result
|
||||||
mapfile -t results <<< "$(scanList "${domainQuery}" "${lists}" "${exact}")"
|
domain="${1}"
|
||||||
if [[ -n "${results[*]}" ]]; then
|
table="${2}"
|
||||||
wbMatch=true
|
type="${3:-}"
|
||||||
# Loop through each result in order to print unique file title once
|
|
||||||
for result in "${results[@]}"; do
|
# As underscores are legitimate parts of domains, we escape possible them when using the LIKE operator.
|
||||||
fileName="${result%%.*}"
|
# Underscores are a SQLite wildcard matching exactly one character. We obviously want to suppress this
|
||||||
if [[ -n "${blockpage}" ]]; then
|
# behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched
|
||||||
echo "π ${result}"
|
# as a literal underscore character.
|
||||||
exit 0
|
case "${type}" in
|
||||||
elif [[ -n "${exact}" ]]; then
|
"exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
* ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\'";;
|
||||||
else
|
esac
|
||||||
# Only print filename title once per file
|
|
||||||
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
# Send prepared query to gravity database
|
||||||
|
result="$(sqlite3 "${gravityDBfile}" "${querystr}")" 2> /dev/null
|
||||||
|
if [[ -n "${result}" ]]; then
|
||||||
|
# Prepend listname (separated by a colon) if we found at least one result
|
||||||
|
# and output result
|
||||||
|
results="$(sed "s/^/${table}:/g;" <<< "${result}")"
|
||||||
|
else
|
||||||
|
# Output empty string as the database query didn't return any result
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
mapfile -t results <<< "${results}"
|
||||||
|
if [[ -n "${results[*]}" ]]; then
|
||||||
|
wbMatch=true
|
||||||
|
# Loop through each result in order to print unique file title once
|
||||||
|
for result in "${results[@]}"; do
|
||||||
|
fileName="${result%%:*}"
|
||||||
|
if [[ -n "${blockpage}" ]]; then
|
||||||
|
echo "π ${result}"
|
||||||
|
exit 0
|
||||||
|
elif [[ -n "${exact}" ]]; then
|
||||||
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
||||||
fileName_prev="${fileName}"
|
else
|
||||||
|
# Only print filename title once per file
|
||||||
|
if [[ ! "${fileName}" == "${fileName_prev:-}" ]]; then
|
||||||
|
echo " ${matchType^} found in ${COL_BOLD}${fileName^}${COL_NC}"
|
||||||
|
fileName_prev="${fileName}"
|
||||||
|
fi
|
||||||
|
echo " ${result#*:}"
|
||||||
fi
|
fi
|
||||||
echo " ${result#*:}"
|
done
|
||||||
fi
|
fi
|
||||||
done
|
}
|
||||||
fi
|
|
||||||
|
# Scan Whitelist and Blacklist
|
||||||
|
scanDatabaseTable "${domainQuery}" "whitelist" "${exact}"
|
||||||
|
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
|
||||||
|
|
||||||
# Scan Wildcards
|
# Scan Wildcards
|
||||||
if [[ -e "${wildcardlist}" ]]; then
|
if [[ -e "${wildcardlist}" ]]; then
|
||||||
|
|
Loading…
Reference in New Issue