mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-28 17:13:17 +00:00
Big refactor of functions
This commit is contained in:
parent
6c24d5b633
commit
7213f46101
8 changed files with 276 additions and 295 deletions
38
advanced/Functions/git.funcs
Normal file
38
advanced/Functions/git.funcs
Normal 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!"
|
||||||
|
}
|
|
@ -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
|
44
advanced/Functions/pihole.funcs
Normal file
44
advanced/Functions/pihole.funcs
Normal 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"
|
||||||
|
}
|
|
@ -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 ))
|
||||||
|
|
|
@ -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,35 +26,25 @@ 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
|
||||||
|
@ -63,7 +55,7 @@ function HandleOther(){
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -78,11 +70,11 @@ function PopBlacklistFile(){
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -97,16 +89,15 @@ function AddDomain(){
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -117,25 +108,23 @@ function RemoveDomain(){
|
||||||
|
|
||||||
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
|
fi
|
||||||
else
|
else
|
||||||
|
|
||||||
echo ":::"
|
echo ":::"
|
||||||
for dom in "${domToRemoveList[@]}"
|
for dom in "${domToRemoveList[@]}"
|
||||||
do
|
do
|
||||||
#we need to remove the domains from the blacklist file and the host file
|
# We need to remove the domains from the blacklist file and the host file
|
||||||
echo "::: $dom"
|
echo "::: $dom"
|
||||||
echo -n "::: removing from HOSTS file..."
|
echo -n "::: removing from HOSTS file..."
|
||||||
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList
|
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList
|
||||||
|
@ -145,7 +134,6 @@ function ModifyHostFile(){
|
||||||
echo " done!"
|
echo " done!"
|
||||||
done
|
done
|
||||||
fi
|
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!"
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,34 +26,25 @@ 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
|
||||||
|
@ -62,7 +55,7 @@ function HandleOther(){
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -77,12 +70,12 @@ function PopWhitelistFile(){
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -99,19 +92,18 @@ 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
|
||||||
|
@ -119,7 +111,7 @@ function RemoveDomain(){
|
||||||
|
|
||||||
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)
|
||||||
|
@ -134,15 +126,14 @@ function ModifyHostFile(){
|
||||||
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 [[ -n $piholeIPv6 ]];then
|
if [[ IPv6 ]];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!"
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue