mirror of
https://github.com/pi-hole/pi-hole
synced 2025-01-03 12:40:56 +00:00
Minor tweaks
Mainly for consistency Signed-off-by: MMotti <matthew.w.motti@gmail.com>
This commit is contained in:
parent
b49c702f33
commit
7613e94ef6
@ -12,7 +12,6 @@
|
|||||||
# Globals
|
# Globals
|
||||||
piholeDir="/etc/pihole"
|
piholeDir="/etc/pihole"
|
||||||
gravityDBfile="${piholeDir}/gravity.db"
|
gravityDBfile="${piholeDir}/gravity.db"
|
||||||
regexlist="/etc/pihole/regex.list"
|
|
||||||
options="$*"
|
options="$*"
|
||||||
adlist=""
|
adlist=""
|
||||||
all=""
|
all=""
|
||||||
@ -23,7 +22,6 @@ matchType="match"
|
|||||||
colfile="/opt/pihole/COL_TABLE"
|
colfile="/opt/pihole/COL_TABLE"
|
||||||
source "${colfile}"
|
source "${colfile}"
|
||||||
|
|
||||||
# Scan an array of files for matching strings
|
|
||||||
# Scan an array of files for matching strings
|
# Scan an array of files for matching strings
|
||||||
scanList(){
|
scanList(){
|
||||||
# Escape full stops
|
# Escape full stops
|
||||||
@ -39,7 +37,12 @@ scanList(){
|
|||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
case "${type}" in
|
case "${type}" in
|
||||||
"exact" ) grep -i -E -l "(^|(?<!#)\\s)${esc_domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
|
"exact" ) grep -i -E -l "(^|(?<!#)\\s)${esc_domain}($|\\s|#)" ${lists} /dev/null 2>/dev/null;;
|
||||||
"rx" ) awk 'NR==FNR{regexps[$0]}{for (r in regexps)if($0 ~ r)print r}' <(echo "${lists}") <(echo "${domain}") 2>/dev/null;;
|
# Create array of regexps
|
||||||
|
# Iterate through each regexp and check whether it matches the domainQuery
|
||||||
|
# If it does, print the matching regexp and continue looping
|
||||||
|
# Input 1 - regexps | Input 2 - domainQuery
|
||||||
|
"regex" ) awk 'NR==FNR{regexps[$0]}{for (r in regexps)if($0 ~ r)print r}' \
|
||||||
|
<(echo "${lists}") <(echo "${domain}") 2>/dev/null;;
|
||||||
* ) grep -i "${esc_domain}" ${lists} /dev/null 2>/dev/null;;
|
* ) grep -i "${esc_domain}" ${lists} /dev/null 2>/dev/null;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
@ -97,9 +100,8 @@ scanDatabaseTable() {
|
|||||||
# 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
|
case "${type}" in
|
||||||
"exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
|
"exact" ) querystr="SELECT domain FROM vw_${table} WHERE domain = '${domain}'";;
|
||||||
"retrievetable" ) querystr="SELECT domain FROM vw_${table}";;
|
* ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
||||||
* ) querystr="SELECT domain FROM vw_${table} WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Send prepared query to gravity database
|
# Send prepared query to gravity database
|
||||||
@ -109,13 +111,6 @@ scanDatabaseTable() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If we are only retrieving the table
|
|
||||||
# Just output and return
|
|
||||||
if [[ "${type}" == "retrievetable" ]]; 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
|
||||||
|
|
||||||
@ -138,20 +133,19 @@ scanDatabaseTable "${domainQuery}" "whitelist" "${exact}"
|
|||||||
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
|
scanDatabaseTable "${domainQuery}" "blacklist" "${exact}"
|
||||||
|
|
||||||
# Scan Regex table
|
# Scan Regex table
|
||||||
regexlist=$(scanDatabaseTable "" "regex" "retrievetable")
|
mapfile -t regexlist <<< "$(sqlite3 "${gravityDBfile}" "SELECT domain FROM vw_regex" 2> /dev/null)"
|
||||||
|
# Split results over new line and store in a string
|
||||||
|
# ready for processing
|
||||||
|
str_regexlist=$(IFS=$'\n'; echo "${regexlist[*]}")
|
||||||
|
# If there are regexps in the DB
|
||||||
|
if [[ -n "${str_regexlist}" ]]; then
|
||||||
|
# Return any regexps that match the domainQuery
|
||||||
|
mapfile -t results <<< "$(scanList "${domainQuery}" "${str_regexlist}" "regex")"
|
||||||
|
|
||||||
if [[ -n "${regexlist}" ]]; then
|
# If there are matches to the domain query
|
||||||
# Return portion(s) of string that is found in the regex list
|
|
||||||
mapfile -t results <<< "$(scanList "${domainQuery}" "${regexlist}" "rx")"
|
|
||||||
|
|
||||||
# If a result is found
|
|
||||||
if [[ -n "${results[*]}" ]]; then
|
if [[ -n "${results[*]}" ]]; then
|
||||||
# Count the matches
|
|
||||||
regexCount=${#results[@]}
|
|
||||||
# Determine plural string
|
|
||||||
[[ $regexCount -gt 1 ]] && plu="es"
|
|
||||||
# Form output strings
|
# Form output strings
|
||||||
str="${COL_BOLD}${regexCount}${COL_NC} ${matchType}${plu:-} found in ${COL_BOLD}regex${COL_NC} table"
|
str="${matchType^} found in ${COL_BOLD}regex list${COL_NC}"
|
||||||
result="${COL_BOLD}$(IFS=$'\n'; echo "${results[*]}")${COL_NC}"
|
result="${COL_BOLD}$(IFS=$'\n'; echo "${results[*]}")${COL_NC}"
|
||||||
|
|
||||||
if [[ -z "${blockpage}" ]]; then
|
if [[ -z "${blockpage}" ]]; then
|
||||||
@ -160,7 +154,7 @@ if [[ -n "${regexlist}" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
case "${blockpage}" in
|
case "${blockpage}" in
|
||||||
true ) echo "π ${regexlist##*/}"; exit 0;;
|
true ) echo "π regex list"; exit 0;;
|
||||||
* ) awk '{print " "$0}' <<< "${result}";;
|
* ) awk '{print " "$0}' <<< "${result}";;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user