Reduce code duplication and add special handling of | character as it might appear in regex filter string
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
e5d1cb5a2e
commit
788cd78321
|
@ -21,9 +21,11 @@ wildcard=false
|
||||||
domList=()
|
domList=()
|
||||||
|
|
||||||
listType=""
|
listType=""
|
||||||
|
listname=""
|
||||||
|
sqlitekey=""
|
||||||
|
|
||||||
colfile="/opt/pihole/COL_TABLE"
|
# shellcheck source=/opt/pihole/COL_TABLE
|
||||||
source ${colfile}
|
source "/opt/pihole/COL_TABLE"
|
||||||
|
|
||||||
|
|
||||||
helpFunc() {
|
helpFunc() {
|
||||||
|
@ -61,7 +63,7 @@ EscapeRegexp() {
|
||||||
# string in our regular expressions
|
# string in our regular expressions
|
||||||
# This sed is intentionally executed in three steps to ease maintainability
|
# This sed is intentionally executed in three steps to ease maintainability
|
||||||
# The first sed removes any amount of leading dots
|
# The first sed removes any amount of leading dots
|
||||||
echo $* | sed 's/^\.*//' | sed "s/[]\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
echo "$@" | sed 's/^\.*//' | sed "s/[]\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleOther() {
|
HandleOther() {
|
||||||
|
@ -86,11 +88,28 @@ HandleOther() {
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessDomainList() {
|
ProcessDomainList() {
|
||||||
|
if [[ "${listType}" == "regex" ]]; then
|
||||||
|
# Regex filter list
|
||||||
|
listname="regex filters"
|
||||||
|
sqlitekey="filter"
|
||||||
|
else
|
||||||
|
# Whitelist / Blacklist
|
||||||
|
listname="${listType}"
|
||||||
|
sqlitekey="domain"
|
||||||
|
fi
|
||||||
|
|
||||||
for dom in "${domList[@]}"; do
|
for dom in "${domList[@]}"; do
|
||||||
# Logic: If addmode then add to desired list and remove from the other; if delmode then remove from desired list but do not add to the other
|
# Format domain into regex filter if requested
|
||||||
|
if [[ "${wildcard}" == true ]]; then
|
||||||
|
dom="(^|\\.)${dom//\./\\.}$"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Logic: If addmode then add to desired list and remove from the other; if delmode then remove from desired list but do not add to the othe
|
||||||
if ${addmode}; then
|
if ${addmode}; then
|
||||||
AddDomain "${dom}" "${listType}"
|
AddDomain "${dom}" "${listType}"
|
||||||
RemoveDomain "${dom}" "${listAlt}"
|
if [[ ! "${listType}" == "regex" ]]; then
|
||||||
|
RemoveDomain "${dom}" "${listAlt}"
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
RemoveDomain "${dom}" "${listType}"
|
RemoveDomain "${dom}" "${listType}"
|
||||||
fi
|
fi
|
||||||
|
@ -98,22 +117,10 @@ ProcessDomainList() {
|
||||||
}
|
}
|
||||||
|
|
||||||
AddDomain() {
|
AddDomain() {
|
||||||
local domain list listname sqlitekey num
|
local domain list num
|
||||||
domain="$1"
|
domain="$1"
|
||||||
list="$2"
|
list="$2"
|
||||||
|
|
||||||
if [[ "${list}" == "regex" ]]; then
|
|
||||||
listname="regex filters"
|
|
||||||
sqlitekey="filter"
|
|
||||||
if [[ "${wildcard}" == true ]]; then
|
|
||||||
domain="(^|\\.)${domain//\./\\.}$"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# Whitelist / Blacklist
|
|
||||||
listname="${list}list"
|
|
||||||
sqlitekey="domain"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Is the domain in the list we want to add it to?
|
# Is the domain in the list we want to add it to?
|
||||||
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM ${list} WHERE ${sqlitekey} = \"${domain}\";")"
|
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM ${list} WHERE ${sqlitekey} = \"${domain}\";")"
|
||||||
|
|
||||||
|
@ -135,22 +142,10 @@ AddDomain() {
|
||||||
}
|
}
|
||||||
|
|
||||||
RemoveDomain() {
|
RemoveDomain() {
|
||||||
local domain list listname sqlitekey num
|
local domain list num
|
||||||
domain="$1"
|
domain="$1"
|
||||||
list="$2"
|
list="$2"
|
||||||
|
|
||||||
if [[ "${list}" == "regex" ]]; then
|
|
||||||
listname="regex filters"
|
|
||||||
sqlitekey="filter"
|
|
||||||
if [[ "${wildcard}" == true ]]; then
|
|
||||||
domain="(^|\\.)${domain//\./\\.}$"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
# Whitelist / Blacklist
|
|
||||||
listname="${list}list"
|
|
||||||
sqlitekey="domain"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Is the domain in the list we want to remove it from?
|
# Is the domain in the list we want to remove it from?
|
||||||
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM ${list} WHERE ${sqlitekey} = \"${domain}\";")"
|
num="$(sqlite3 "${gravityDBfile}" "SELECT COUNT(*) FROM ${list} WHERE ${sqlitekey} = \"${domain}\";")"
|
||||||
|
|
||||||
|
@ -172,7 +167,7 @@ RemoveDomain() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Displaylist() {
|
Displaylist() {
|
||||||
local domain list listname count status
|
local list listname count num_pipes domain enabled status
|
||||||
|
|
||||||
if [[ "${listType}" == "regex" ]]; then
|
if [[ "${listType}" == "regex" ]]; then
|
||||||
listname="regex filters list"
|
listname="regex filters list"
|
||||||
|
@ -189,13 +184,20 @@ Displaylist() {
|
||||||
count=1
|
count=1
|
||||||
while IFS= read -r line
|
while IFS= read -r line
|
||||||
do
|
do
|
||||||
domain="$(cut -d'|' -f1 <<< "${line}")"
|
# Count number of pipes seen in this line
|
||||||
enabled="$(cut -d'|' -f2 <<< "${line}")"
|
# This is necessary because we can only detect the pipe separating the fields
|
||||||
|
# from the end backwards as the domain (which is the first field) may contain
|
||||||
|
# pipe symbols as they are perfectly valid regex filter control characters
|
||||||
|
num_pipes="$(grep -c "^" <<< "$(grep -o "|" <<< "${line}")")"
|
||||||
|
|
||||||
|
domain="$(cut -d'|' -f"-$((num_pipes-2))" <<< "${line}")"
|
||||||
|
enabled="$(cut -d'|' -f$((num_pipes-1)) <<< "${line}")"
|
||||||
if [[ "${enabled}" -eq 1 ]]; then
|
if [[ "${enabled}" -eq 1 ]]; then
|
||||||
status="enabled"
|
status="enabled"
|
||||||
else
|
else
|
||||||
status="disabled"
|
status="disabled"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo " ${count}: ${domain} (${status})"
|
echo " ${count}: ${domain} (${status})"
|
||||||
count=$((count+1))
|
count=$((count+1))
|
||||||
done <<< "${data}"
|
done <<< "${data}"
|
||||||
|
|
Loading…
Reference in New Issue