Big refactor of functions

This commit is contained in:
Mcat12 2016-03-13 21:34:22 -04:00
parent 6c24d5b633
commit 7213f46101
8 changed files with 276 additions and 295 deletions

View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Provides functions related to Git
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
is_repo() {
# If the directory does not have a .git folder it is not a repo
echo -n "::: Checking $1 is a repo..."
if [ -d "$1/.git" ]; then
echo " OK!"
return 1
fi
echo " not found!!"
return 0
}
make_repo() {
# Remove the non-repod interface and clone the interface
echo -n "::: Cloning $2 into $1..."
$SUDO rm -rf $1
$SUDO git clone -q "$2" "$1" > /dev/null & spinner $!
echo " done!"
}
update_repo() {
# Pull the latest commits
echo -n "::: Updating repo in $1..."
cd "$1"
$SUDO git pull -q > /dev/null & spinner $!
echo " done!"
}

View file

@ -3,45 +3,14 @@
# (c) 2015, 2016 by Jacob Salmela # (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi # Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net # http://pi-hole.net
# Blacklists domains # Provides functions related to the Pi-hole's installation
# #
# Pi-hole is free software: you can redistribute it and/or modify # Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or # the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
source /etc/pihole/pihole.var # Requires pihole.funcs
RootCheck(){
echo ":::"
if [[ $EUID -eq 0 ]];then
echo "::: You are root."
else
echo "::: sudo will be used for the install."
# Check if it is actually installed
# If it isn't, exit because the install cannot complete
if [[ $(dpkg-query -s sudo) ]];then
export SUDO="sudo"
else
echo "::: Please install sudo or run this as root."
exit 1
fi
fi
}
spinner() {
local pid=$1
spin='-\|/'
i=0
while $SUDO kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\b${spin:$i:1}"
sleep .1
done
printf "\b"
}
backupLegacyPihole() { backupLegacyPihole() {
# This function detects and backups the pi-hole v1 files. It will not do anything to the current version files. # This function detects and backups the pi-hole v1 files. It will not do anything to the current version files.
@ -376,53 +345,6 @@ checkForDependencies() {
done done
} }
getGitFiles() {
# Setup git repos for base files and web admin
echo ":::"
echo "::: Checking for existing base files..."
if is_repo $piholeFilesDir; then
make_repo $piholeFilesDir $piholeGitUrl
else
update_repo $piholeFilesDir
fi
echo ":::"
echo "::: Checking for existing web interface..."
if is_repo $webInterfaceDir; then
make_repo $webInterfaceDir $webInterfaceGitUrl
else
update_repo $webInterfaceDir
fi
}
is_repo() {
# If the directory does not have a .git folder it is not a repo
echo -n "::: Checking $1 is a repo..."
if [ -d "$1/.git" ]; then
echo " OK!"
return 1
fi
echo " not found!!"
return 0
}
make_repo() {
# Remove the non-repod interface and clone the interface
echo -n "::: Cloning $2 into $1..."
$SUDO rm -rf $1
$SUDO git clone -q "$2" "$1" > /dev/null & spinner $!
echo " done!"
}
update_repo() {
# Pull the latest commits
echo -n "::: Updating repo in $1..."
cd "$1"
$SUDO git pull -q > /dev/null & spinner $!
echo " done!"
}
CreateLogFile() { CreateLogFile() {
# Create logfiles if necessary # Create logfiles if necessary
echo ":::" echo ":::"
@ -488,7 +410,6 @@ installPihole() {
getGitFiles getGitFiles
installScripts installScripts
installConfigs installConfigs
#installWebAdmin
CreateLogFile CreateLogFile
installPiholeWeb installPiholeWeb
installCron installCron

View file

@ -0,0 +1,44 @@
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Provides common functions
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
source pihole.var
RootCheck(){
echo ":::"
if [[ $EUID -eq 0 ]];then
echo "::: You are root."
else
echo "::: sudo will be used for the install."
# Check if it is actually installed
# If it isn't, exit because the install cannot complete
if [[ $(dpkg-query -s sudo) ]];then
export SUDO="sudo"
else
echo "::: Please install sudo or run this as root."
exit 1
fi
fi
}
spinner() {
local pid=$1
spin='-\|/'
i=0
while $SUDO kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\b${spin:$i:1}"
sleep .1
done
printf "\b"
}

View file

@ -3,7 +3,7 @@
# (c) 2015, 2016 by Jacob Salmela # (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi # Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net # http://pi-hole.net
# Blacklists domains # Provides variables for Pi-hole
# #
# Pi-hole is free software: you can redistribute it and/or modify # Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -17,9 +17,17 @@ webInterfaceGitUrl="https://github.com/pi-hole/AdminLTE.git"
webInterfaceDir="/var/www/html/admin" webInterfaceDir="/var/www/html/admin"
piholeGitUrl="https://github.com/pi-hole/pi-hole.git" piholeGitUrl="https://github.com/pi-hole/pi-hole.git"
piholeFilesDir="/etc/.pihole" piholeFilesDir="/etc/.pihole"
piholeInstallDir="/etc/pihole" piholeInstallDir="/etc/pihole"
piLog="/var/log/pihole.log"
whitelist=/etc/pihole/whitelist.txt
blacklist=/etc/pihole/blacklist.txt
adList=/etc/pihole/gravity.list
piholeIPfile=/tmp/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6
r=$(( rows / 2 )) r=$(( rows / 2 ))
c=$(( columns / 2 )) c=$(( columns / 2 ))

View file

@ -10,6 +10,8 @@
# the Free Software Foundation, either version 2 of the License, or # the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
source /etc/pihole/Functions/pihole.funcs
if [[ $# = 0 ]]; then if [[ $# = 0 ]]; then
echo "Immediately blacklists one or more domains in the hosts file" echo "Immediately blacklists one or more domains in the hosts file"
echo " " echo " "
@ -24,128 +26,114 @@ if [[ $# = 0 ]]; then
fi fi
#globals #globals
blacklist=/etc/pihole/blacklist.txt
adList=/etc/pihole/gravity.list
reload=true reload=true
addmode=true addmode=true
force=false force=false
versbose=true versbose=true
domList=() domList=()
domToRemoveList=() domToRemoveList=()
IPv6=false
piholeIPfile=/tmp/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6
# Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script # Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script
IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') piholeIP=${IPv4addr%/*}
piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
piholeIP=${piholeIPCIDR%/*}
modifyHost=false modifyHost=false
if [[ -f piholeIPv6file ]];then
if [[ -f $piholeIPv6file ]];then
# If the file exists, then the user previously chose to use IPv6 in the automated installer # If the file exists, then the user previously chose to use IPv6 in the automated installer
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') IPv6=true
fi fi
function HandleOther(){ function HandleOther(){
#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=$(echo $1 | perl -ne'print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/')
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
domList=("${domList[@]}" $validDomain) domList=("${domList[@]}" $validDomain)
fi fi
} }
function PopBlacklistFile(){ function PopBlacklistFile(){
#check blacklist file exists, and if not, create it # Check blacklist file exists, and if not, create it
if [[ ! -f $blacklist ]];then if [[ ! -f $blacklist ]];then
touch $blacklist touch $blacklist
fi fi
for dom in "${domList[@]}" for dom in "${domList[@]}"
do do
if $addmode; then if $addmode; then
AddDomain $dom AddDomain $dom
else else
RemoveDomain $dom RemoveDomain $dom
fi fi
done done
} }
function AddDomain(){ function AddDomain(){
#| sed 's/\./\\./g' #| sed 's/\./\\./g'
bool=false bool=false
grep -Ex -q "$1" $blacklist || bool=true grep -Ex -q "$1" $blacklist || bool=true
if $bool; then if $bool; then
#domain not found in the blacklist file, add it! # Domain not found in the blacklist file, add it!
if $versbose; then if $versbose; then
echo -n "::: Adding $1 to blacklist file..." echo -n "::: Adding $1 to blacklist file..."
fi fi
echo $1 >> $blacklist echo $1 >> $blacklist
modifyHost=true modifyHost=true
echo " done!" echo " done!"
else else
if $versbose; then if $versbose; then
echo "::: $1 already exists in blacklist.txt! No need to add" echo "::: $1 already exists in blacklist.txt! No need to add"
fi fi
fi fi
} }
function RemoveDomain(){ function RemoveDomain(){
bool=false
bool=false grep -Ex -q "$1" $blacklist || bool=true
grep -Ex -q "$1" $blacklist || bool=true if $bool; then
if $bool; then # Domain is not in the blacklist file, no need to Remove
#Domain is not in the blacklist file, no need to Remove if $versbose; then
if $versbose; then echo "::: $1 is NOT blacklisted! No need to remove"
echo "::: $1 is NOT blacklisted! No need to remove" fi
fi else
else # Domain is in the blacklist file, add to a temporary array
#Domain is in the blacklist file, add to a temporary array if $versbose; then
if $versbose; then echo "::: Un-blacklisting $dom..."
echo "::: Un-blacklisting $dom..." fi
fi domToRemoveList=("${domToRemoveList[@]}" $1)
domToRemoveList=("${domToRemoveList[@]}" $1) modifyHost=true
modifyHost=true fi
fi
} }
function ModifyHostFile(){ function ModifyHostFile(){
if $addmode; then if $addmode; then
#add domains to the hosts file # Add domains to the hosts file
if [[ -r $blacklist ]];then if [[ -r $blacklist ]];then
numberOf=$(cat $blacklist | sed '/^\s*$/d' | wc -l) numberOf=$(cat $blacklist | sed '/^\s*$/d' | wc -l)
plural=; [[ "$numberOf" != "1" ]] && plural=s plural=; [[ "$numberOf" != "1" ]] && plural=s
echo ":::" echo ":::"
echo -n "::: Modifying HOSTS file to blacklist $numberOf domain${plural}..." echo -n "::: Modifying HOSTS file to blacklist $numberOf domain${plural}..."
if [[ -n $piholeIPv6 ]];then if [[ IPv6 ]];then
cat $blacklist | awk -v ipv4addr="$piholeIP" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> $adList cat $blacklist | awk -v ipv4addr="$piholeIP" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> $adList
else else
cat $blacklist | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList cat $blacklist | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList
fi fi
fi
else
echo ":::"
for dom in "${domToRemoveList[@]}"
do
#we need to remove the domains from the blacklist file and the host file
echo "::: $dom"
echo -n "::: removing from HOSTS file..."
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList
echo " done!"
echo -n "::: removing from blackist.txt..."
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' $blacklist
echo " done!"
done
fi fi
else
echo ":::"
for dom in "${domToRemoveList[@]}"
do
# We need to remove the domains from the blacklist file and the host file
echo "::: $dom"
echo -n "::: removing from HOSTS file..."
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList
echo " done!"
echo -n "::: removing from blackist.txt..."
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' $blacklist
echo " done!"
done
fi
} }
function Reload() { function Reload() {
@ -156,10 +144,10 @@ function Reload() {
dnsmasqPid=$(pidof dnsmasq) dnsmasqPid=$(pidof dnsmasq)
if [[ $dnsmasqPid ]]; then if [[ $dnsmasqPid ]]; then
# service already running - reload config # Service already running - reload config
sudo kill -HUP $dnsmasqPid sudo kill -HUP $dnsmasqPid
else else
# service not running, start it up # Service not running, start it up
sudo service dnsmasq start sudo service dnsmasq start
fi fi
echo " done!" echo " done!"
@ -169,13 +157,13 @@ function Reload() {
for var in "$@" for var in "$@"
do do
case "$var" in case "$var" in
"-nr"| "--noreload" ) reload=false;; "-nr"| "--noreload" ) reload=false;;
"-d" | "--delmode" ) addmode=false;; "-d" | "--delmode" ) addmode=false;;
"-f" | "--force" ) force=true;; "-f" | "--force" ) force=true;;
"-q" | "--quiet" ) versbose=false;; "-q" | "--quiet" ) versbose=false;;
* ) HandleOther $var;; * ) HandleOther $var;;
esac esac
done done
PopBlacklistFile PopBlacklistFile
@ -183,8 +171,8 @@ PopBlacklistFile
if $modifyHost || $force; then if $modifyHost || $force; then
ModifyHostFile ModifyHostFile
else else
if $versbose; then if $versbose; then
echo "::: No changes need to be made" echo "::: No changes need to be made"
fi fi
exit 1 exit 1
fi fi

View file

@ -10,18 +10,16 @@
# the Free Software Foundation, either version 2 of the License, or # the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
source /etc/pihole/Functions/pihole.var
#Functions############################################################################################################## #Functions##############################################################################################################
piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list"
today=$(date "+%b %e") today=$(date "+%b %e")
function CalcBlockedDomains(){ function CalcBlockedDomains(){
CheckIPv6 CheckIPv6
if [ -e "$gravity" ]; then if [ -e adList ]; then
#Are we IPV6 or IPV4? #Are we IPV6 or IPV4?
if [[ -n $piholeIPv6 ]];then if [[ IPv6 ]];then
#We are IPV6 #We are IPV6
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}') blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}')
else else
@ -42,7 +40,7 @@ function CalcQueriesToday(){
} }
function CalcblockedToday(){ function CalcblockedToday(){
if [ -e "$piLog" ] && [ -e "$gravity" ];then if [ -e "$piLog" ] && [ -e adList ];then
blockedToday=$(cat $piLog | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l) blockedToday=$(cat $piLog | awk '/\/etc\/pihole\/gravity.list/ && !/address/ {print $6}' | wc -l)
else else
blockedToday="Err." blockedToday="Err."
@ -62,10 +60,9 @@ function CalcPercentBlockedToday(){
} }
function CheckIPv6(){ function CheckIPv6(){
piholeIPv6file="/etc/pihole/.useIPv6" if [[ -f piholeIPv6file ]];then
if [[ -f $piholeIPv6file ]];then
# If the file exists, then the user previously chose to use IPv6 in the automated installer # If the file exists, then the user previously chose to use IPv6 in the automated installer
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') IPv6=true
fi fi
} }

View file

@ -10,6 +10,8 @@
# the Free Software Foundation, either version 2 of the License, or # the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
source /etc/pihole/Functions/pihole.funcs
if [[ $# = 0 ]]; then if [[ $# = 0 ]]; then
echo "Immediately whitelists one or more domains in the hosts file" echo "Immediately whitelists one or more domains in the hosts file"
echo " " echo " "
@ -24,74 +26,65 @@ if [[ $# = 0 ]]; then
fi fi
#globals #globals
whitelist=/etc/pihole/whitelist.txt
adList=/etc/pihole/gravity.list
reload=true reload=true
addmode=true addmode=true
force=false force=false
versbose=true versbose=true
domList=() domList=()
domToRemoveList=() domToRemoveList=()
IPv6=false
piholeIPfile=/tmp/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6
# Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script # Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script
IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') piholeIP=${IPv4addr%/*}
piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
piholeIP=${piholeIPCIDR%/*}
modifyHost=false modifyHost=false
if [[ -f piholeIPv6file ]];then
if [[ -f $piholeIPv6file ]];then
# If the file exists, then the user previously chose to use IPv6 in the automated installer # If the file exists, then the user previously chose to use IPv6 in the automated installer
piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }') IPv6=true
fi fi
function HandleOther(){ function HandleOther(){
#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=$(echo $1 | perl -ne'print if /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b/')
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
domList=("${domList[@]}" $validDomain) domList=("${domList[@]}" $validDomain)
fi fi
} }
function PopWhitelistFile(){ function PopWhitelistFile(){
#check whitelist file exists, and if not, create it # Check whitelist file exists, and if not, create it
if [[ ! -f $whitelist ]];then if [[ ! -f $whitelist ]];then
touch $whitelist touch $whitelist
fi fi
for dom in "${domList[@]}" for dom in "${domList[@]}"
do do
if $addmode; then if $addmode; then
AddDomain $dom AddDomain $dom
else else
RemoveDomain $dom RemoveDomain $dom
fi fi
done done
} }
function AddDomain(){ function AddDomain(){
#| sed 's/\./\\./g' #| sed 's/\./\\./g'
bool=false bool=false
grep -Ex -q "$1" $whitelist || bool=true grep -Ex -q "$1" $whitelist || bool=true
if $bool; then if $bool; then
#domain not found in the whitelist file, add it! # Domain not found in the whitelist file, add it!
if $versbose; then if $versbose; then
echo -n "::: Adding $1 to whitelist.txt..." echo -n "::: Adding $1 to whitelist.txt..."
fi fi
echo $1 >> $whitelist echo $1 >> $whitelist
modifyHost=true modifyHost=true
if $versbose; then if $versbose; then
echo " done!" echo " done!"
fi fi
else else
if $versbose; then if $versbose; then
echo "::: $1 already exists in whitelist.txt, no need to add!" echo "::: $1 already exists in whitelist.txt, no need to add!"
fi fi
@ -99,63 +92,61 @@ function AddDomain(){
} }
function RemoveDomain(){ function RemoveDomain(){
bool=false
bool=false grep -Ex -q "$1" $whitelist || bool=true
grep -Ex -q "$1" $whitelist || bool=true if $bool; then
if $bool; then # Domain is not in the whitelist file, no need to Remove
#Domain is not in the whitelist file, no need to Remove if $versbose; then
if $versbose; then echo "::: $1 is NOT whitelisted! No need to remove"
echo "::: $1 is NOT whitelisted! No need to remove" fi
fi else
else # Domain is in the whitelist file, add to a temporary array and remove from whitelist file
#Domain is in the whitelist file, add to a temporary array and remove from whitelist file if $versbose; then
#if $versbose; then echo "::: Un-whitelisting $dom..."
#echo "::: Un-whitelisting $dom..." fi
#fi domToRemoveList=("${domToRemoveList[@]}" $1)
domToRemoveList=("${domToRemoveList[@]}" $1) modifyHost=true
modifyHost=true fi
fi
} }
function ModifyHostFile(){ function ModifyHostFile(){
if $addmode; then if $addmode; then
#remove domains in from hosts file # Remove domains in from hosts file
if [[ -r $whitelist ]];then if [[ -r $whitelist ]];then
# Remove whitelist entries # Remove whitelist entries
numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l) numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l)
plural=; [[ "$numberOf" != "1" ]] && plural=s plural=; [[ "$numberOf" != "1" ]] && plural=s
echo ":::" echo ":::"
echo -n "::: Modifying HOSTS file to whitelist $numberOf domain${plural}..." 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 "$piholeIP $line"; done > /etc/pihole/whitelist.tmp
awk -F':' '{print $1}' $whitelist | while read line; do echo "$piholeIPv6 $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 echo "l" >> /etc/pihole/whitelist.tmp
grep -F -x -v -f /etc/pihole/whitelist.tmp /etc/pihole/gravity.list > /etc/pihole/gravity.tmp grep -F -x -v -f /etc/pihole/whitelist.tmp /etc/pihole/gravity.list > /etc/pihole/gravity.tmp
rm /etc/pihole/gravity.list rm /etc/pihole/gravity.list
mv /etc/pihole/gravity.tmp /etc/pihole/gravity.list mv /etc/pihole/gravity.tmp /etc/pihole/gravity.list
rm /etc/pihole/whitelist.tmp rm /etc/pihole/whitelist.tmp
echo " done!" echo " done!"
fi
fi else
else # We need to add the removed domains to the hosts file
#we need to add the removed domains to the hosts file echo ":::"
echo ":::" echo "::: Modifying HOSTS file to un-whitelist domains..."
echo "::: Modifying HOSTS file to un-whitelist domains..." for rdom in "${domToRemoveList[@]}"
for rdom in "${domToRemoveList[@]}" do
do if [[ IPv6 ]];then
if [[ -n $piholeIPv6 ]];then echo -n "::: Un-whitelisting $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 $rdom | awk -v ipv4addr="$piholeIP" -v ipv6addr="$piholeIPv6" '{sub(/\r$/,""); print ipv4addr" "$0"\n"ipv6addr" "$0}' >> $adList echo " done!"
echo " done!" else
else echo -n "::: Un-whitelisting $rdom on IPv4"
echo -n "::: Un-whitelisting $rdom on IPv4" echo $rdom | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList
echo $rdom | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList echo " done!"
echo " done!" fi
fi echo -n "::: Removing $rdom from whitelist.txt..."
echo -n "::: Removing $rdom from whitelist.txt..." echo $rdom| sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' $whitelist
echo $rdom| sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' $whitelist echo " done!"
echo " done!" done
done fi
fi
} }
function Reload() { function Reload() {
@ -178,24 +169,24 @@ function Reload() {
for var in "$@" for var in "$@"
do do
case "$var" in case "$var" in
"-nr"| "--noreload" ) reload=false;; "-nr"| "--noreload" ) reload=false;;
"-d" | "--delmode" ) addmode=false;; "-d" | "--delmode" ) addmode=false;;
"-f" | "--force" ) force=true;; "-f" | "--force" ) force=true;;
"-q" | "--quiet" ) versbose=false;; "-q" | "--quiet" ) versbose=false;;
* ) HandleOther $var;; * ) HandleOther $var;;
esac esac
done done
PopWhitelistFile PopWhitelistFile
if $modifyHost || $force; then if $modifyHost || $force; then
ModifyHostFile ModifyHostFile
else else
if $versbose; then if $versbose; then
echo ":::" echo ":::"
echo "::: No changes need to be made" echo "::: No changes need to be made"
exit 1 exit 1
fi fi
fi fi

View file

@ -10,21 +10,15 @@
# the Free Software Foundation, either version 2 of the License, or # the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# Get files, as they might have changed
curl -o $HOME/piholeInstall/pihole.var https://raw.githubusercontent.com/pi-hole/pi-hole/master/advanced/Functions/pihole.var
curl -o $HOME/piholeInstall/pihole.funcs https://raw.githubusercontent.com/pi-hole/pi-hole/master/advanced/Functions/pihole.funcs
curl -o $HOME/piholeInstall/install.funcs https://raw.githubusercontent.com/pi-hole/pi-hole/master/advanced/Functions/install.funcs
curl -o $HOME/piholeInstall/git.funcs https://raw.githubusercontent.com/pi-hole/pi-hole/master/advanced/Functions/git.funcs
#Check if we have the function and variable files source $HOME/piholeInstall/pihole.funcs
if [ ! -f /etc/pihole/pihole.var ]; then source $HOME/piholeInstall/install.funcs
curl -o $HOME/piholeInstall/pihole.vars https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/automated%20install/pihole.vars source $HOME/piholeInstall/git.funcs
source $HOME/piholeInstall/pihole.var
else
source /etc/pihole/pihole.var
fi
if [ ! -f /etc/pihole/pihole.funcs ]; then
curl -o $HOME/piholeInstall/pihole.funcs https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/automated%20install/pihole.funcs
source $HOME/piholeInstall/pihole.funcs
else
source /etc/pihole/pihole.funcs
fi
###########Begin Script ###########Begin Script
RootCheck RootCheck
@ -33,7 +27,7 @@ if [ ! -d /etc/pihole ];then
$SUDO mkdir -p /etc/pihole/ $SUDO mkdir -p /etc/pihole/
fi fi
#Display the welcom dialogs #Display the welcome dialogs
welcomeDialogs welcomeDialogs
# Just back up the original Pi-hole right away since it won't take long and it gets it out of the way # Just back up the original Pi-hole right away since it won't take long and it gets it out of the way