1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-11-18 14:18:15 +00:00

Merge pull request #941 from DL6ER/caseinsensitive

Improvements for black-/whitelist scripts
This commit is contained in:
Mcat12 2016-12-04 14:33:10 -05:00 committed by GitHub
commit f98de3d5db

23
advanced/Scripts/list.sh Normal file → Executable file
View File

@ -50,9 +50,18 @@ EOM
exit 0 exit 0
} }
EscapeRegexp() {
# This way we may safely insert an arbitrary
# string in our regular expressions
echo $* | sed "s/[]\\.|$(){}?+*^]/\\\\&/g" | sed "s/\\//\\\\\//g"
}
HandleOther(){ HandleOther(){
# First, convert everything to lowercase
domain=$(sed -e "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/" <<< "$1")
#check validity of domain #check validity of domain
validDomain=$(echo "$1" | perl -ne'print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/') validDomain=$(perl -ne "print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/" <<< "$domain")
if [ -z "${validDomain}" ]; then if [ -z "${validDomain}" ]; then
echo "::: $1 is not a valid argument or domain name" echo "::: $1 is not a valid argument or domain name"
else else
@ -77,12 +86,12 @@ PoplistFile() {
} }
AddDomain() { AddDomain() {
list="$2" list="$2"
domain=$(EscapeRegexp "$1")
bool=true bool=true
#Is the domain in the list we want to add it to? #Is the domain in the list we want to add it to?
grep -Ex -q "$1" ${list} > /dev/null 2>&1 || bool=false grep -Ex -q "${domain}" ${list} > /dev/null 2>&1 || bool=false
if [[ "${bool}" == false ]]; then if [[ "${bool}" == false ]]; then
#domain not found in the whitelist file, add it! #domain not found in the whitelist file, add it!
@ -101,14 +110,16 @@ AddDomain() {
RemoveDomain() { RemoveDomain() {
list="$2" list="$2"
domain=$(EscapeRegexp "$1")
bool=true bool=true
#Is it in the other list? Logic follows that if its whitelisted it should not be blacklisted and vice versa #Is it in the list? Logic follows that if its whitelisted it should not be blacklisted and vice versa
grep -Ex -q "$1" ${list} > /dev/null 2>&1 || bool=false grep -Ex -q "${domain}" ${list} > /dev/null 2>&1 || bool=false
if [[ "${bool}" == true ]]; then if [[ "${bool}" == true ]]; then
# Remove it from the other one # Remove it from the other one
echo "::: Removing $1 from $list..." echo "::: Removing $1 from $list..."
echo "$1" | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' ${list} # /I flag: search case-insensitive
sed -i "/${domain}/Id" ${list}
reload=true reload=true
else else
if [[ "${verbose}" == true ]]; then if [[ "${verbose}" == true ]]; then