diff --git a/advanced/Scripts/blacklist.sh b/advanced/Scripts/blacklist.sh index 8bcf22bf..d83179cf 100755 --- a/advanced/Scripts/blacklist.sh +++ b/advanced/Scripts/blacklist.sh @@ -9,7 +9,6 @@ # 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. - helpFunc() { echo "::: Immediately blacklists one or more domains in the hosts file" @@ -23,42 +22,23 @@ helpFunc() echo "::: -q, --quiet output is less verbose" echo "::: -h, --help Show this help dialog" echo "::: -l, --list Display your blacklisted domains" + echo "::: -c:/path/to/file location of config file" exit 1 } -if [[ $# = 0 ]]; then - helpFunc -fi - -#globals -basename=pihole -piholeDir=/etc/${basename} -adList=${piholeDir}/gravity.list -blacklist=${piholeDir}/blacklist.txt -reload=true -addmode=true -force=false -verbose=true - -domList=() -domToRemoveList=() - -piholeIPfile=/etc/pihole/piholeIP -piholeIPv6file=/etc/pihole/.useIPv6 - -if [[ -f ${piholeIPfile} ]];then - # If the file exists, it means it was exported from the installation script and we should use that value instead of detecting it in this script - piholeIP=$(cat ${piholeIPfile}) - #rm $piholeIPfile -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 - 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%/*} -fi - modifyHost=false + +HandleOther(){ + #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 + echo "::: $1 is not a valid argument or domain name" + else + domList=("${domList[@]}" ${validDomain}) + fi +} + # After setting defaults, check if there's local overrides if [[ -r ${piholeDir}/pihole.conf ]];then echo "::: Local calibration requested..." @@ -66,21 +46,6 @@ if [[ -r ${piholeDir}/pihole.conf ]];then fi -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) }') -fi - -HandleOther(){ - #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 - echo "::: $1 is not a valid argument or domain name" - else - domList=("${domList[@]}" ${validDomain}) - fi -} - PopBlacklistFile(){ #check blacklist file exists, and if not, create it if [[ ! -f ${blacklist} ]];then @@ -138,15 +103,11 @@ ModifyHostFile(){ #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 - 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 + plural=; [[ "$numberOf" != "1" ]] && plural=s + echo ":::" + echo -n "::: Modifying HOSTS file to blacklist $numberOf domain${plural}..." + sed "s/^/$piholeIP /g" ${blacklist} >> ${adList} + fi else echo ":::" for dom in "${domToRemoveList[@]}" @@ -201,6 +162,15 @@ DisplayBlist() { ################################################### +#globals +reload=true +addmode=true +force=false +verbose=true + +domList=() +domToRemoveList=() + for var in "$@" do case "$var" in @@ -208,12 +178,38 @@ do "-d" | "--delmode" ) addmode=false;; "-f" | "--force" ) force=true;; "-q" | "--quiet" ) verbose=false;; - "-h" | "--help" ) helpFunc;; + "-h" | "--help" ) helpFunc;; "-l" | "--list" ) DisplayBlist;; + -c* ) setupVars=$(echo $var | cut -d ":" -f2);; * ) HandleOther "$var";; esac done + +if [[ $# = 0 ]]; then + helpFunc +fi + +if [[ -z ${setupVars} ]] ; then + setupVars=/etc/pihole/setupVars.conf +fi +if [[ -f ${setupVars} ]];then + . ${setupVars} +else + echo "::: WARNING: ${setupVars} missing. Possible installation failure." + echo "::: Please run 'pihole -r', and choose the 'install' option to reconfigure." + exit 1 +fi + +if [[ -n ${IPv6_address} ]] ; then + piholeIP=${IPv6_address} +else + piholeIP=${IPv4_address} + piholeIP=$(echo "${piholeIP}" | cut -f1 -d"/") +fi + + + PopBlacklistFile if ${modifyHost} || ${force}; then diff --git a/advanced/Scripts/whitelist.sh b/advanced/Scripts/whitelist.sh index f715a2fd..ec1251dc 100755 --- a/advanced/Scripts/whitelist.sh +++ b/advanced/Scripts/whitelist.sh @@ -10,6 +10,15 @@ # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. +HandleOther(){ + #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 + echo "::: $1 is not a valid argument or domain name" + else + domList=("${domList[@]}" ${validDomain}) + fi +} helpFunc() { @@ -24,63 +33,18 @@ helpFunc() echo "::: -q, --quiet output is less verbose" echo "::: -h, --help Show this help dialog" echo "::: -l, --list Display your whitelisted domains" + echo "::: -c:/path/to/file location of config file" exit 1 } -if [[ $# = 0 ]]; then - helpFunc -fi - -#globals -basename=pihole -piholeDir=/etc/${basename} -adList=${piholeDir}/gravity.list -whitelist=${piholeDir}/whitelist.txt -reload=true -addmode=true -force=false -verbose=true - -domList=() -domToRemoveList=() - -piholeIPfile=/etc/pihole/piholeIP -piholeIPv6file=/etc/pihole/.useIPv6 - -if [[ -f ${piholeIPfile} ]];then - # If the file exists, it means it was exported from the installation script and we should use that value instead of detecting it in this script - piholeIP=$(cat ${piholeIPfile}) - #rm $piholeIPfile -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 - 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%/*} -fi - modifyHost=false # After setting defaults, check if there's local overrides if [[ -r ${piholeDir}/pihole.conf ]];then echo "::: Local calibration requested..." - . ${piholeDir}/pihole.conf + . ${piholeDir}/pihole.conf fi -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) }') -fi - -HandleOther(){ - #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 - echo "::: $1 is not a valid argument or domain name" - else - domList=("${domList[@]}" ${validDomain}) - fi -} - PopWhitelistFile(){ #check whitelist file exists, and if not, create it if [[ ! -f ${whitelist} ]];then @@ -141,44 +105,41 @@ ModifyHostFile(){ if ${addmode}; then #remove domains in from hosts file if [[ -r ${whitelist} ]];then - # Remove whitelist entries - numberOf=$(cat ${whitelist} | sed '/^\s*$/d' | wc -l) - plural=; [[ "$numberOf" != "1" ]] && plural=s - echo ":::" - echo -n "::: Modifying HOSTS file to whitelist $numberOf domain${plural}..." - awk -F':' '{print $1}' ${whitelist} | while read -r line; do echo "$piholeIP $line"; done > /etc/pihole/whitelist.tmp - awk -F':' '{print $1}' ${whitelist} | while read -r line; do echo "$piholeIPv6 $line"; done >> /etc/pihole/whitelist.tmp - echo "l" >> /etc/pihole/whitelist.tmp - grep -F -x -v -f ${piholeDir}/whitelist.tmp ${adList} > ${piholeDir}/gravity.tmp - rm ${adList} - mv ${piholeDir}/gravity.tmp ${adList} - rm ${piholeDir}/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 - if grep -q "$rdom" /etc/pihole/*.domains; then - echo "::: AdLists contain $rdom, re-adding block" - if [[ -n ${piholeIPv6} ]];then - echo -n "::: Restoring block for $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 -n "::: Restoring block for $rdom on IPv4..." - echo "$rdom" | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>${adList} + # Remove whitelist entries + numberOf=$(cat ${whitelist} | sed '/^\s*$/d' | wc -l) + plural=; [[ "$numberOf" != "1" ]] && plural=s + echo ":::" + echo -n "::: Modifying HOSTS file to whitelist $numberOf domain${plural}..." + awk -F':' '{print $1}' ${whitelist} | while read -r line; do echo "$piholeIP $line"; done > ${piholeDir}/whitelist.tmp + echo "l" >> ${piholeDir}/whitelist.tmp + grep -F -x -v -f ${piholeDir}/whitelist.tmp ${adList} > ${piholeDir}/gravity.tmp + rm ${adList} + mv ${piholeDir}/gravity.tmp ${adList} + rm ${piholeDir}/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 + if grep -q "$rdom" ${piholeDir}/*.domains; then + echo "::: AdLists contain $rdom, re-adding block" + if [[ -n ${piholeIPv6} ]];then + echo -n "::: Restoring block for $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 -n "::: Restoring block for $rdom on IPv4..." + echo "$rdom" | awk -v ipv4addr="$piholeIP" '{sub(/\r$/,""); print ipv4addr" "$0}' >>${adList} + echo " done!" + fi fi echo -n "::: Removing $rdom from $whitelist..." echo "$rdom" | sed 's/\./\\./g' | xargs -I {} perl -i -ne'print unless /'{}'(?!.)/;' ${whitelist} echo " done!" done - fi + fi } Reload() { @@ -218,19 +179,51 @@ DisplayWlist() { ################################################### +#globals +reload=true +addmode=true +force=false +verbose=true + +domList=() +domToRemoveList=() + for var in "$@" do case "$var" in - "-nr"| "--noreload" ) reload=false;; - "-d" | "--delmode" ) addmode=false;; - "-f" | "--force" ) force=true;; - "-q" | "--quiet" ) verbose=false;; - "-h" | "--help" ) helpFunc;; - "-l" | "--list" ) DisplayWlist;; - * ) HandleOther "$var";; + "-nr"| "--noreload" ) reload=false;shift;; + "-d" | "--delmode" ) addmode=false;shift;; + "-f" | "--force" ) force=true;shift;; + "-q" | "--quiet" ) verbose=false;shift;; + "-h" | "--help" ) helpFunc;shift;; + "-l" | "--list" ) DisplayWlist;shift;; + -c* ) setupVars=$(echo $var | cut -d ":" -f2);; + * ) HandleOther "${var}";; esac done + +if [[ $# = 0 ]]; then + helpFunc +fi +if [[ -z ${setupVars} ]] ; then + setupVars=/etc/pihole/setupVars.conf +fi +if [[ -f ${setupVars} ]];then + . ${setupVars} +else + echo "::: WARNING: ${setupVars} missing. Possible installation failure." + echo "::: Please run 'pihole -r', and choose the 'install' option to reconfigure." + exit 1 +fi + +if [[ -n ${IPv6_address} ]] ; then + piholeIP=${IPv6_address} +else + piholeIP=${IPv4_address} + piholeIP=$(echo "${piholeIP}" | cut -f1 -d"/") +fi + PopWhitelistFile if ${modifyHost} || ${force}; then @@ -245,4 +238,4 @@ fi if ${reload}; then Reload -fi \ No newline at end of file +fi diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 7c26019e..242ef5aa 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -24,7 +24,7 @@ setupVars=/etc/pihole/setupVars.conf webInterfaceGitUrl="https://github.com/pi-hole/AdminLTE.git" webInterfaceDir="/var/www/html/admin" -piholeGitUrl="https://github.com/pi-hole/pi-hole.git" +piholeGitUrl="https://github.com/tuplink/pi-hole.git" piholeFilesDir="/etc/.pihole" useUpdateVars=false @@ -58,7 +58,7 @@ else echo "::: Detecting the presence of the sudo utility for continuation of this install..." if [ -x "$(command -v sudo)" ];then echo "::: Utility sudo located." - exec curl -sSL https://install.pi-hole.net | sudo bash "$@" + exec sudo bash "$0" "$@" exit $? else echo "::: sudo is needed for the Web interface to run pihole commands. Please run this script as root and it will be automatically installed." @@ -796,6 +796,20 @@ finalExports() { echo "IPv6_address=${IPv6_address}" echo "piholeDNS1=${piholeDNS1}" echo "piholeDNS2=${piholeDNS2}" + echo "basename=pihole" + echo "piholeDir=/etc/\${basename}" + echo "adList=\${piholeDir}/gravity.list" + echo "justDomainsExtension=domains" + echo "matterAndLight=\${basename}.0.matterandlight.txt" + echo "supernova=\${basename}.1.supernova.txt" + echo "eventHorizon=\${basename}.2.eventHorizon.txt" + echo "accretionDisc=\${basename}.3.accretionDisc.txt" + echo "blacklist=/etc/pihole/blacklist.txt" + echo "whitelist=/etc/pihole/whitelist.txt" + echo "adListFile=/etc/pihole/adlists.list" + echo "adListDefault=/etc/pihole/adlists.default" + echo "whitelistScript=/opt/pihole/whitelist.sh" + echo "blacklistScript=/opt/pihole/blacklist.sh" }>> "${setupVars}" } diff --git a/gravity.sh b/gravity.sh index 3d9c05c4..5cf9bfb2 100755 --- a/gravity.sh +++ b/gravity.sh @@ -11,7 +11,6 @@ # (at your option) any later version. # Run this script as root or under sudo -echo ":::" helpFunc() { @@ -22,43 +21,11 @@ helpFunc() echo "::: Options:" echo "::: -f, --force Force lists to be downloaded, even if they don't need updating." echo "::: -h, --help Show this help dialog" + echo "::: -c:/path/to/file location of config file" exit 1 } -adListFile=/etc/pihole/adlists.list -adListDefault=/etc/pihole/adlists.default -whitelistScript=/opt/pihole/whitelist.sh -blacklistScript=/opt/pihole/blacklist.sh - -#Source the setupVars from install script for the IP -setupVars=/etc/pihole/setupVars.conf -if [[ -f ${setupVars} ]];then - . /etc/pihole/setupVars.conf -else - echo "::: WARNING: /etc/pihole/setupVars.conf missing. Possible installation failure." - echo "::: Please run 'pihole -r', and choose the 'reconfigure' option to reconfigure." - exit 1 -fi - -#Remove the /* from the end of the IPv4addr. -IPv4addr=${IPv4_address%/*} - -# Variables for various stages of downloading and formatting the list -basename=pihole -piholeDir=/etc/${basename} -adList=${piholeDir}/gravity.list -justDomainsExtension=domains -matterAndLight=${basename}.0.matterandlight.txt -supernova=${basename}.1.supernova.txt -eventHorizon=${basename}.2.eventHorizon.txt -accretionDisc=${basename}.3.accretionDisc.txt - -# Warn users still using pihole.conf that it no longer has any effect (I imagine about 2 people use it) -if [[ -r ${piholeDir}/pihole.conf ]];then - echo "::: pihole.conf file no longer supported. Over-rides in this file are ignored." -fi - ########################### # collapse - begin formation of pihole gravity_collapse() { @@ -199,9 +166,9 @@ gravity_Schwarzchild() { gravity_Blacklist(){ # Append blacklist entries if they exist echo -n "::: Running blacklist script to update HOSTS file...." - ${blacklistScript} -f -nr -q > /dev/null + ${blacklistScript} -f -nr -q -c:${setupVars}> /dev/null - numBlacklisted=$(wc -l < "/etc/pihole/blacklist.txt") + numBlacklisted=$(wc -l < "${blacklist}") plural=; [[ "$numBlacklisted" != "1" ]] && plural=s echo " $numBlacklisted domain${plural} blacklisted!" } @@ -221,8 +188,8 @@ gravity_Whitelist() { echo " done!" echo -n "::: Running whitelist script to update HOSTS file...." - ${whitelistScript} -f -nr -q "${urls[@]}" > /dev/null - numWhitelisted=$(wc -l < "/etc/pihole/whitelist.txt") + ${whitelistScript} -f -nr -q -c:${setupVars} "${urls[@]}" > /dev/null + numWhitelisted=$(wc -l < "${whitelist}") plural=; [[ "$numWhitelisted" != "1" ]] && plural=s echo " $numWhitelisted domain${plural} whitelisted!" } @@ -304,7 +271,7 @@ gravity_reload() { # Reload hosts file echo ":::" echo -n "::: Refresh lists in dnsmasq..." - + #ensure /etc/dnsmasq.d/01-pihole.conf is pointing at the correct list! #First escape forward slashes in the path: adList=${adList//\//\\\/} @@ -312,42 +279,72 @@ gravity_reload() { sed -i "s/^addn-hosts.*/addn-hosts=$adList/" /etc/dnsmasq.d/01-pihole.conf find "$piholeDir" -type f -exec chmod 666 {} \; - dnsmasqPid=$(pidof dnsmasq) + dnsmasqPid=$(pidof dnsmasq) if [[ ${dnsmasqPid} ]]; then # service already running - reload config if [ -x "$(command -v systemctl)" ]; then - systemctl restart dnsmasq - else - service dnsmasq restart - fi + systemctl restart dnsmasq + else + service dnsmasq restart + fi else # service not running, start it up if [ -x "$(command -v systemctl)" ]; then - systemctl start dnsmasq - else - service dnsmasq start - fi + systemctl start dnsmasq + else + service dnsmasq start + fi fi } - for var in "$@" do case "$var" in "-f" | "--force" ) forceGrav=true;; "-h" | "--help" ) helpFunc;; + -c* ) setupVars=$(echo $var | cut -d ":" -f2);; esac done +echo ":::" + +#Source the setupVars from install script for the IP +if [[ -z ${setupVars} ]] ; then + setupVars=/etc/pihole/setupVars.conf +fi +if [[ -f ${setupVars} ]];then + . ${setupVars} +else + echo "::: WARNING: ${setupVars} missing. Possible installation failure." + echo "::: Please run 'pihole -r', and choose the 'install' option to reconfigure." + exit 1 +fi + +#Remove the /* from the end of the IPv4addr. +IPv4addr=${IPv4_address%/*} + +# Warn users still using pihole.conf that it no longer has any effect (I imagine about 2 people use it) +if [[ -r ${piholeDir}/pihole.conf ]];then + echo "::: pihole.conf file no longer supported. Over-rides in this file are ignored." +fi + + + + + + if [[ ${forceGrav} == true ]]; then echo -n "::: Deleting exising list cache..." - rm /etc/pihole/list.* + for f in "${piholeDir}/list.*" ; do + # rm ${piholeDir}/list.${f} + echo "rm ${piholeDir}/list.${f}" + done echo " done!" fi #Overwrite adlists.default from /etc/.pihole in case any changes have been made. Changes should be saved in /etc/adlists.list -cp /etc/.pihole/adlists.default /etc/pihole/adlists.default +cp /etc/.pihole/adlists.default ${piholeDir}/adlists.default gravity_collapse gravity_spinup gravity_Schwarzchild