mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-24 15:13:42 +00:00
122 lines
4.7 KiB
Text
122 lines
4.7 KiB
Text
|
#!/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
|
||
|
# Exports variables for use with 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
|
||
|
# the Free Software Foundation, either version 2 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
|
||
|
# First check if the user running the script is root.
|
||
|
# If they are, they have root access and the SUDO variable will be set to nothing
|
||
|
if [[ $EUID -eq 0 ]];then
|
||
|
echo "You are root."
|
||
|
else
|
||
|
# sudo will be used during install and to restart services in gravity.sh
|
||
|
export SUDO="sudo"
|
||
|
fi
|
||
|
|
||
|
# Find the rows and columns
|
||
|
rows=$(tput lines)
|
||
|
columns=$(tput cols)
|
||
|
|
||
|
# Divide by two so the dialogs take up half of the screen, which looks nice.
|
||
|
r=$(( rows / 2 ))
|
||
|
c=$(( columns / 2 ))
|
||
|
|
||
|
# Find IP used to route to outside world
|
||
|
IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
|
||
|
IPv4addr=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
|
||
|
IPv4gw=$(ip route get 8.8.8.8 | awk '{print $3}')
|
||
|
|
||
|
# System files
|
||
|
export dnsmsqConfDir=/etc/dnsmasq.d
|
||
|
export lighttpConfDir=/etc/lighttpd
|
||
|
export cronFolder=/etc/cron.d
|
||
|
export dhcpcdFile=/etc/dhcpcd.conf
|
||
|
export dnsmasqConfigFile=$dnsmsqConfDir/01-pihole.conf
|
||
|
export lighttpdConfigFile=$lighttpConfDir/lighttpd.conf
|
||
|
export webRootDir=/var/www/html
|
||
|
export webRootPiholeDir=$webRootDir/pihole
|
||
|
export webRootAdminDir=$webRootDir/admin
|
||
|
export piholeIndexHtmlFile=$webRootPiholeDir/index.html
|
||
|
export adminIndexPhpFile=$webRootAdminDir/index.php
|
||
|
export logDir=/var/log
|
||
|
export webAccessLog=$logDir/lighttpd/access.log
|
||
|
export webErrorLog=$logDir/lighttpd/error.log
|
||
|
export piholeLog=$logDir/pihole.log
|
||
|
|
||
|
# Local Pi-hole directory
|
||
|
export basename=pihole
|
||
|
export piholeDir=/usr/local/etc/$basename
|
||
|
export piholeGitDir=/usr/local/etc/.$basename
|
||
|
|
||
|
# Scripts
|
||
|
export scriptDir=/usr/local/bin
|
||
|
export gravityScript=$scriptDir/gravity.sh
|
||
|
export chronometerScript=$scriptDir/chronometer.sh
|
||
|
export piholeLogFlushScript=$scriptDir/piholeLogFlush.sh
|
||
|
export blacklistScript=$scriptDir/blacklist.sh
|
||
|
export whitelistScript=$scriptDir/whitelist.sh
|
||
|
export piholeCron=$cronFolder/pihole
|
||
|
|
||
|
# Variables for various stages of downloading and formatting the list
|
||
|
export tmpLocation=/tmp
|
||
|
export adListFile=$piholeDir/adlists.list
|
||
|
export adListDefault=$piholeDir/gravity.list
|
||
|
export blacklist=$piholeDir/blacklist.txt
|
||
|
export whitelist=$piholeDir/whitelist.txt
|
||
|
export latentWhitelist=$piholeDir/latentWhitelist.txt
|
||
|
export justDomainsExtension=domains
|
||
|
export matter=$basename.0.matterandlight.txt
|
||
|
export supernova=$basename.1.supernova.txt
|
||
|
export eventHorizon=$basename.2.eventHorizon.txt
|
||
|
export accretionDisc=$basename.3.accretionDisc.txt
|
||
|
export tmpInstallLog=$tmpLocation/pihole-install.log
|
||
|
export instalLogLoc=$piholeDir/install.log
|
||
|
|
||
|
# These files are empty but checked for existence
|
||
|
export piholeInterfaceFile=$tmpLocation/.piholeInterface
|
||
|
export piholeIPv4static=$tmpLocation/.useIPv4
|
||
|
export piholeIPv4file=$piholeDir/.useIPv4
|
||
|
export piholeIPv6file=$piholeDir/.useIPv6
|
||
|
|
||
|
# This is checked during the initial install
|
||
|
if [[ -f $piholeIPv4static ]];then
|
||
|
# If the tmp file exists, it means it was created from the installation script and we should use that value instead of detecting it in this script
|
||
|
# This is because the installer asks to set a static IP address
|
||
|
# The file can be removed because once the address is set, it won't change
|
||
|
# But then make an invisible file to be contigious with IPv6
|
||
|
export piholeIPv4=$(cat $piholeIPv4static)
|
||
|
rm -f $piholeIPv4static
|
||
|
$SUDO touch $piholeIPv4file
|
||
|
else
|
||
|
# 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
|
||
|
export IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
|
||
|
export piholeIPv4CIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}')
|
||
|
export piholeIPv4=${piholeIPv4CIDR%/*}
|
||
|
fi
|
||
|
|
||
|
if [[ -f $piholeIPv6file ]];then
|
||
|
# If the file exists, then the user previously chose to use IPv6 in the automated installer
|
||
|
export piholeIPv6=$(ip -6 route get 2001:4860:4860::8888 | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }')
|
||
|
fi
|
||
|
|
||
|
# This is used in more than one script, so just make it a function so we don't repeat code
|
||
|
reloadHostsFile(){
|
||
|
dnsmasqPid=$(pidof dnsmasq)
|
||
|
|
||
|
if [[ $dnsmasqPid ]]; then
|
||
|
# The service already running - reload config
|
||
|
$SUDO kill -HUP $dnsmasqPid
|
||
|
else
|
||
|
# The service is not running so start it up
|
||
|
$SUDO service dnsmasq start
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
echo "Variables have been sourced."
|