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
# Network-wide ad blocking via your Raspberry Pi
# 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
# 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 /etc/pihole/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"
}
# Requires pihole.funcs
backupLegacyPihole() {
# 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
}
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() {
# Create logfiles if necessary
echo ":::"
@ -488,7 +410,6 @@ installPihole() {
getGitFiles
installScripts
installConfigs
#installWebAdmin
CreateLogFile
installPiholeWeb
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
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Blacklists domains
# Provides variables for Pi-hole
#
# 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
@ -17,9 +17,17 @@ webInterfaceGitUrl="https://github.com/pi-hole/AdminLTE.git"
webInterfaceDir="/var/www/html/admin"
piholeGitUrl="https://github.com/pi-hole/pi-hole.git"
piholeFilesDir="/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 ))
c=$(( columns / 2 ))

View file

@ -10,6 +10,8 @@
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
source /etc/pihole/Functions/pihole.funcs
if [[ $# = 0 ]]; then
echo "Immediately blacklists one or more domains in the hosts file"
echo " "
@ -24,35 +26,25 @@ if [[ $# = 0 ]]; then
fi
#globals
blacklist=/etc/pihole/blacklist.txt
adList=/etc/pihole/gravity.list
reload=true
addmode=true
force=false
versbose=true
domList=()
domToRemoveList=()
piholeIPfile=/tmp/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6
IPv6=false
# 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)}')
piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
piholeIP=${piholeIPCIDR%/*}
piholeIP=${IPv4addr%/*}
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
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
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/')
if [ -z "$validDomain" ]; then
@ -63,7 +55,7 @@ function HandleOther(){
}
function PopBlacklistFile(){
#check blacklist file exists, and if not, create it
# Check blacklist file exists, and if not, create it
if [[ ! -f $blacklist ]];then
touch $blacklist
fi
@ -78,11 +70,11 @@ function PopBlacklistFile(){
}
function AddDomain(){
#| sed 's/\./\\./g'
#| sed 's/\./\\./g'
bool=false
grep -Ex -q "$1" $blacklist || bool=true
if $bool; then
#domain not found in the blacklist file, add it!
# Domain not found in the blacklist file, add it!
if $versbose; then
echo -n "::: Adding $1 to blacklist file..."
fi
@ -97,16 +89,15 @@ function AddDomain(){
}
function RemoveDomain(){
bool=false
grep -Ex -q "$1" $blacklist || bool=true
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
echo "::: $1 is NOT blacklisted! No need to remove"
fi
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
echo "::: Un-blacklisting $dom..."
fi
@ -117,25 +108,23 @@ function RemoveDomain(){
function ModifyHostFile(){
if $addmode; then
#add domains to the hosts file
# Add domains to the hosts file
if [[ -r $blacklist ]];then
numberOf=$(cat $blacklist | sed '/^\s*$/d' | wc -l)
plural=; [[ "$numberOf" != "1" ]] && plural=s
echo ":::"
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
else
cat $blacklist | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>$adList
fi
fi
else
echo ":::"
for dom in "${domToRemoveList[@]}"
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 -n "::: removing from HOSTS file..."
echo $dom | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /[^.]'{}'(?!.)/;' $adList
@ -145,7 +134,6 @@ function ModifyHostFile(){
echo " done!"
done
fi
}
function Reload() {
@ -156,10 +144,10 @@ function Reload() {
dnsmasqPid=$(pidof dnsmasq)
if [[ $dnsmasqPid ]]; then
# service already running - reload config
# Service already running - reload config
sudo kill -HUP $dnsmasqPid
else
# service not running, start it up
# Service not running, start it up
sudo service dnsmasq start
fi
echo " done!"

View file

@ -10,18 +10,16 @@
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
source /etc/pihole/Functions/pihole.var
#Functions##############################################################################################################
piLog="/var/log/pihole.log"
gravity="/etc/pihole/gravity.list"
today=$(date "+%b %e")
function CalcBlockedDomains(){
CheckIPv6
if [ -e "$gravity" ]; then
if [ -e adList ]; then
#Are we IPV6 or IPV4?
if [[ -n $piholeIPv6 ]];then
if [[ IPv6 ]];then
#We are IPV6
blockedDomainsTotal=$(wc -l /etc/pihole/gravity.list | awk '{print $1/2}')
else
@ -42,7 +40,7 @@ function CalcQueriesToday(){
}
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)
else
blockedToday="Err."
@ -62,10 +60,9 @@ function CalcPercentBlockedToday(){
}
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
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
}

View file

@ -10,6 +10,8 @@
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
source /etc/pihole/Functions/pihole.funcs
if [[ $# = 0 ]]; then
echo "Immediately whitelists one or more domains in the hosts file"
echo " "
@ -24,34 +26,25 @@ if [[ $# = 0 ]]; then
fi
#globals
whitelist=/etc/pihole/whitelist.txt
adList=/etc/pihole/gravity.list
reload=true
addmode=true
force=false
versbose=true
domList=()
domToRemoveList=()
piholeIPfile=/tmp/piholeIP
piholeIPv6file=/etc/pihole/.useIPv6
IPv6=false
# 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)}')
piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
piholeIP=${piholeIPCIDR%/*}
piholeIP=${IPv4addr%/*}
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
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
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/')
if [ -z "$validDomain" ]; then
@ -62,7 +55,7 @@ function HandleOther(){
}
function PopWhitelistFile(){
#check whitelist file exists, and if not, create it
# Check whitelist file exists, and if not, create it
if [[ ! -f $whitelist ]];then
touch $whitelist
fi
@ -77,12 +70,12 @@ function PopWhitelistFile(){
}
function AddDomain(){
#| sed 's/\./\\./g'
#| sed 's/\./\\./g'
bool=false
grep -Ex -q "$1" $whitelist || bool=true
if $bool; then
#domain not found in the whitelist file, add it!
# Domain not found in the whitelist file, add it!
if $versbose; then
echo -n "::: Adding $1 to whitelist.txt..."
fi
@ -99,19 +92,18 @@ function AddDomain(){
}
function RemoveDomain(){
bool=false
grep -Ex -q "$1" $whitelist || bool=true
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
echo "::: $1 is NOT whitelisted! No need to remove"
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
# Domain is in the whitelist file, add to a temporary array and remove from whitelist file
if $versbose; then
echo "::: Un-whitelisting $dom..."
fi
domToRemoveList=("${domToRemoveList[@]}" $1)
modifyHost=true
fi
@ -119,7 +111,7 @@ function RemoveDomain(){
function ModifyHostFile(){
if $addmode; then
#remove domains in from hosts file
# Remove domains in from hosts file
if [[ -r $whitelist ]];then
# Remove whitelist entries
numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l)
@ -134,15 +126,14 @@ function ModifyHostFile(){
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
# 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
if [[ -n $piholeIPv6 ]];then
if [[ IPv6 ]];then
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!"

View file

@ -10,21 +10,15 @@
# the Free Software Foundation, either version 2 of the License, or
# (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
if [ ! -f /etc/pihole/pihole.var ]; then
curl -o $HOME/piholeInstall/pihole.vars https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/automated%20install/pihole.vars
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
source $HOME/piholeInstall/pihole.funcs
source $HOME/piholeInstall/install.funcs
source $HOME/piholeInstall/git.funcs
###########Begin Script
RootCheck
@ -33,7 +27,7 @@ if [ ! -d /etc/pihole ];then
$SUDO mkdir -p /etc/pihole/
fi
#Display the welcom dialogs
#Display the welcome dialogs
welcomeDialogs
# Just back up the original Pi-hole right away since it won't take long and it gets it out of the way