From 3cd1a5b1d56bcfaa653ff7cb865bf0931dba870b Mon Sep 17 00:00:00 2001 From: Promofaux Date: Sun, 24 Jan 2016 16:51:54 +0000 Subject: [PATCH] ... Helps if I add the changes to the repo file, and not the test file on my pi. --- advanced/Scripts/whitelist.sh | 54 +++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/advanced/Scripts/whitelist.sh b/advanced/Scripts/whitelist.sh index ce8d63e1..500d65df 100755 --- a/advanced/Scripts/whitelist.sh +++ b/advanced/Scripts/whitelist.sh @@ -76,12 +76,13 @@ function PopWhitelistFile(){ function AddDomain(){ #| sed 's/\./\\./g' bool=false - if $versbose; then - echo -n "::: Whitelisting $1...." - fi + grep -Ex -q "$1" $whitelist || bool=true if $bool; then #domain not found in the whitelist file, add it! + if $versbose; then + echo -n "::: Adding $1 to whitelist.txt..." + fi echo $1 >> $whitelist modifyHost=true if $versbose; then @@ -89,7 +90,7 @@ function AddDomain(){ fi else if $versbose; then - echo " already whitelisted! No need to add" + echo "::: $1 already exists in whitelist.txt, no need to add!" fi fi } @@ -105,9 +106,9 @@ function RemoveDomain(){ fi else #Domain is in the whitelist file, add to a temporary array and remove from whitelist file - if $versbose; then - echo "** Un-whitelisting $dom..." - fi + #if $versbose; then + #echo "::: Un-whitelisting $dom..." + #fi domToRemoveList=("${domToRemoveList[@]}" $1) modifyHost=true fi @@ -118,30 +119,46 @@ function ModifyHostFile(){ #remove domains in from hosts file if [[ -r $whitelist ]];then # Remove whitelist entries - numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l) + numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l) plural=; [[ "$numberOf" != "1" ]] && plural=s - echo "** Whitelisting a total of $numberOf domain${plural}..." - awk -F':' '{ print $1 }' $whitelist | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList + echo ":::" + echo -n "::: Modifying HOSTS file to whitelist $numberOf domain${plural}..." + awk -F':' '{print $1}' $whitelist | while read line; do echo "$piholeIP $line"; done > /etc/pihole/whitelist.tmp + awk -F':' '{print $1}' $whitelist | while read line; do echo "$piholeIPv6 $line"; done >> /etc/pihole/whitelist.tmp + echo "l" >> /etc/pihole/whitelist.tmp + grep -F -x -v -f /etc/pihole/whitelist.tmp /etc/pihole/gravity.list > /etc/pihole/gravity.tmp + rm /etc/pihole/gravity.list + mv /etc/pihole/gravity.tmp /etc/pihole/gravity.list + rm /etc/pihole/whitelist.tmp + echo " done!" + fi else #we need to add the removed domains to the hosts file + echo ":::" + echo "::: Modifying HOSTS file to un-whitelist domains..." for rdom in "${domToRemoveList[@]}" - do + do if [[ -n $piholeIPv6 ]];then - echo "**Blacklisting $rdom on IPv4 and IPv6" + echo -n "::: Un-whitelisting $rdom on IPv4 and IPv6..." echo $rdom | awk -v ipv4addr="$piholeIP" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> $adList + echo " done!" else - echo "**Blacklisting $rdom on IPv4" + echo -n "::: Un-whitelisting $rdom on IPv4" echo $rdom | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList - fi + echo " done!" + fi + echo -n "::: Removing $rdom from whitelist.txt..." echo $rdom| sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' $whitelist + echo " done!" done fi } function Reload() { # Reload hosts file - echo "** Refresh lists in dnsmasq..." + echo ":::" + echo -n "::: Refresh lists in dnsmasq..." dnsmasqPid=$(pidof dnsmasq) if [[ $dnsmasqPid ]]; then @@ -151,6 +168,7 @@ function Reload() { # service not running, start it up sudo service dnsmasq start fi + echo " done!" } ################################################### @@ -169,11 +187,11 @@ done PopWhitelistFile if $modifyHost || $force; then - echo "** Modifying Hosts File" - ModifyHostFile + ModifyHostFile else if $versbose; then - echo "** No changes need to be made" + echo ":::" + echo "::: No changes need to be made" exit 1 fi fi