Emulate wildcard blacklisting via -wild and move regex to -regex

Signed-off-by: Mcat12 <newtoncat12@yahoo.com>
This commit is contained in:
Mcat12 2018-07-08 14:37:33 -04:00
parent 61f0cbe10a
commit c5bb404d6a
No known key found for this signature in database
GPG key ID: ABB8FC9789AF524D
3 changed files with 30 additions and 22 deletions

View file

@ -17,6 +17,7 @@ readonly regexlist="/etc/pihole/regex.list"
reload=false reload=false
addmode=true addmode=true
verbose=true verbose=true
wildcard=false
domList=() domList=()
@ -68,7 +69,7 @@ HandleOther() {
# Check validity of domain (don't check for regex entries) # Check validity of domain (don't check for regex entries)
if [[ "${#domain}" -le 253 ]]; then if [[ "${#domain}" -le 253 ]]; then
if [[ "${listMain}" == "${regexlist}" ]]; then if [[ "${listMain}" == "${regexlist}" && "${wildcard}" == false ]]; then
validDomain="${domain}" validDomain="${domain}"
else else
validDomain=$(grep -P "^((-|_)*[a-z\\d]((-|_)*[a-z\\d])*(-|_)*)(\\.(-|_)*([a-z\\d]((-|_)*[a-z\\d])*))*$" <<< "${domain}") # Valid chars check validDomain=$(grep -P "^((-|_)*[a-z\\d]((-|_)*[a-z\\d])*(-|_)*)(\\.(-|_)*([a-z\\d]((-|_)*[a-z\\d])*))*$" <<< "${domain}") # Valid chars check
@ -135,6 +136,9 @@ AddDomain() {
elif [[ "${list}" == "${regexlist}" ]]; then elif [[ "${list}" == "${regexlist}" ]]; then
[[ -z "${type}" ]] && type="--wildcard-only" [[ -z "${type}" ]] && type="--wildcard-only"
bool=true bool=true
[[ "${wildcard}" == true ]] && domain="((^)|(\\.))${domain}$"
# Is the domain in the list? # Is the domain in the list?
# Search only for exactly matching lines # Search only for exactly matching lines
grep -Fx "${domain}" "${regexlist}" > /dev/null 2>&1 || bool=false grep -Fx "${domain}" "${regexlist}" > /dev/null 2>&1 || bool=false
@ -179,6 +183,8 @@ RemoveDomain() {
fi fi
elif [[ "${list}" == "${regexlist}" ]]; then elif [[ "${list}" == "${regexlist}" ]]; then
[[ -z "${type}" ]] && type="--wildcard-only" [[ -z "${type}" ]] && type="--wildcard-only"
[[ "${wildcard}" == true ]] && domain="((^)|(\\.))${domain}$"
bool=true bool=true
# Is it in the list? # Is it in the list?
grep -Fx "${domain}" "${regexlist}" > /dev/null 2>&1 || bool=false grep -Fx "${domain}" "${regexlist}" > /dev/null 2>&1 || bool=false
@ -236,7 +242,8 @@ for var in "$@"; do
case "${var}" in case "${var}" in
"-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";; "-w" | "whitelist" ) listMain="${whitelist}"; listAlt="${blacklist}";;
"-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";; "-b" | "blacklist" ) listMain="${blacklist}"; listAlt="${whitelist}";;
"-wild" | "wildcard" ) listMain="${regexlist}";; "-wild" | "wildcard" ) listMain="${regexlist}"; wildcard=true;;
"-regex" | "regex" ) listMain="${regexlist}";;
"-nr"| "--noreload" ) reload=false;; "-nr"| "--noreload" ) reload=false;;
"-d" | "--delmode" ) addmode=false;; "-d" | "--delmode" ) addmode=false;;
"-q" | "--quiet" ) verbose=false;; "-q" | "--quiet" ) verbose=false;;

View file

@ -5,7 +5,7 @@ Pi-hole : A black-hole for internet advertisements
.br .br
.SH "SYNOPSIS" .SH "SYNOPSIS"
\fBpihole\fR (\fB-w\fR|\fB-b\fR|\fB-wild\fR) [options] domain(s) \fBpihole\fR (\fB-w\fR|\fB-b\fR|\fB-wild\fR|\fB-regex\fR) [options] domain(s)
.br .br
\fBpihole -a\fR \fB-p\fR password \fBpihole -a\fR \fB-p\fR password
.br .br
@ -68,7 +68,12 @@ Available commands and options:
\fB-wild, wildcard\fR [options] [<domain1> <domain2 ...>] \fB-wild, wildcard\fR [options] [<domain1> <domain2 ...>]
.br .br
Add or removes specified regex pattern to the regex blacklist Add or removes specified domain to the wildcard blacklist
.br
\fB-regex, regex\fR [options] [<regex1> <regex2 ...>]
.br
Add or removes specified regex filter to the regex blacklist
.br .br
(Whitelist/Blacklist manipulation options): (Whitelist/Blacklist manipulation options):
@ -277,7 +282,11 @@ Some usage examples
.br .br
\fBpihole -b -d noads.example.com\fR Remove "noads.example.com" from blacklist \fBpihole -b -d noads.example.com\fR Remove "noads.example.com" from blacklist
.br .br
\fBpihole -wild ^example.*$\fR Add "^example.*$" as a regex pattern - would block all domains starting with "example" \fBpihole -wild "^example.*$"\fR Add "^example.*$" as a regex pattern - would
block all domains starting with "example"
.br
\fBpihole -regex "ad.*\.example\.com$"\fR Add "ad.*\.example\.com$" to the regex
blacklist - would block all subdomains of example.com which start with "ad"
.br .br
Changing the Web Interface password Changing the Web Interface password

22
pihole
View file

@ -33,17 +33,7 @@ webpageFunc() {
exit 0 exit 0
} }
whitelistFunc() { listFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
exit 0
}
blacklistFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
exit 0
}
wildcardFunc() {
"${PI_HOLE_SCRIPT_DIR}"/list.sh "$@" "${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
exit 0 exit 0
} }
@ -403,7 +393,8 @@ Add '-h' after specific commands for more information on usage
Whitelist/Blacklist Options: Whitelist/Blacklist Options:
-w, whitelist Whitelist domain(s) -w, whitelist Whitelist domain(s)
-b, blacklist Blacklist domain(s) -b, blacklist Blacklist domain(s)
-wild, wildcard Regex blacklist domain(s) -wild, wildcard Wildcard blacklist domain(s)
-regex, regex Regex blacklist domains(s)
Add '-h' for more info on whitelist/blacklist usage Add '-h' for more info on whitelist/blacklist usage
Debugging Options: Debugging Options:
@ -445,9 +436,10 @@ fi
# Handle redirecting to specific functions based on arguments # Handle redirecting to specific functions based on arguments
case "${1}" in case "${1}" in
"-w" | "whitelist" ) whitelistFunc "$@";; "-w" | "whitelist" ) listFunc "$@";;
"-b" | "blacklist" ) blacklistFunc "$@";; "-b" | "blacklist" ) listFunc "$@";;
"-wild" | "wildcard" ) wildcardFunc "$@";; "-wild" | "wildcard" ) listFunc "$@";;
"-regex" | "regex" ) listFunc "$@";;
"-d" | "debug" ) debugFunc "$@";; "-d" | "debug" ) debugFunc "$@";;
"-f" | "flush" ) flushFunc "$@";; "-f" | "flush" ) flushFunc "$@";;
"-up" | "updatePihole" ) updatePiholeFunc "$@";; "-up" | "updatePihole" ) updatePiholeFunc "$@";;