From c00357663b424d5a88c90c288babb9f7301b367c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 12 Aug 2018 18:36:07 +0200 Subject: [PATCH 01/11] Reload pihole-FTL instead of restart the entire service on "pihole enable/disable" Signed-off-by: DL6ER --- pihole | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pihole b/pihole index 8be03f79..19f7bcb8 100755 --- a/pihole +++ b/pihole @@ -199,7 +199,7 @@ Time: fi fi - restartDNS + restartDNS reload echo -e "${OVER} ${TICK} ${str}" } From 20b946eae5ea27704666ed3ccc6a7018385233d2 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 12 Aug 2018 19:31:00 +0200 Subject: [PATCH 02/11] Instead of changing the dnsmasq configuration, we replace gravity.list and black.list by empty files for disabling. When pihole-FTL receives SIGHUP, it will clear its cache and reload all configured lists. If the files are empty, blocking will be disabled as no content to be blocked will be imported. Signed-off-by: DL6ER --- pihole | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pihole b/pihole index 19f7bcb8..a4fcadb3 100755 --- a/pihole +++ b/pihole @@ -10,7 +10,8 @@ # Please see LICENSE file for your rights under this license. readonly PI_HOLE_SCRIPT_DIR="/opt/pihole" -readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf" +readonly gravitylist="/etc/pihole/gravity.list" +readonly blacklist="/etc/pihole/black.list" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" source "${colfile}" @@ -146,10 +147,13 @@ Time: elif [[ "${1}" == "0" ]]; then # Disable Pi-hole - sed -i 's/^addn-hosts=\/etc\/pihole\/gravity.list/#addn-hosts=\/etc\/pihole\/gravity.list/' /etc/dnsmasq.d/01-pihole.conf - sed -i 's/^addn-hosts=\/etc\/pihole\/black.list/#addn-hosts=\/etc\/pihole\/black.list/' /etc/dnsmasq.d/01-pihole.conf - if [[ -e "$wildcardlist" ]]; then - mv "$wildcardlist" "/etc/pihole/wildcard.list" + if [[ -e "${gravitylist}" ]]; then + mv "${gravitylist}" "${gravitylist}.bck" + echo "" > "${gravitylist}" + fi + if [[ -e "${blacklist}" ]]; then + mv "${blacklist}" "${blacklist}.bck" + echo "" > "${blacklist}" fi if [[ $# > 1 ]]; then local error=false @@ -193,9 +197,11 @@ Time: echo -e " ${INFO} Enabling blocking" local str="Pi-hole Enabled" - sed -i 's/^#addn-hosts/addn-hosts/' /etc/dnsmasq.d/01-pihole.conf - if [[ -e "/etc/pihole/wildcard.list" ]]; then - mv "/etc/pihole/wildcard.list" "$wildcardlist" + if [[ -e "${gravitylist}.bck" ]]; then + mv "${gravitylist}.bck" "${gravitylist}" + fi + if [[ -e "${blacklist}" ]]; then + mv "${blacklist}.bck" "${blacklist}" fi fi From 6cde066eddebcb0cf069e74386dc673a1c5a883a Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 13:43:14 +0200 Subject: [PATCH 03/11] Have gravity detect the presence of files like gravity.list.bck and update this file if present (assuming blocking is disabled) Signed-off-by: DL6ER --- gravity.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gravity.sh b/gravity.sh index 026cd4a4..5624b659 100755 --- a/gravity.sh +++ b/gravity.sh @@ -73,6 +73,19 @@ if [[ -r "${piholeDir}/pihole.conf" ]]; then echo -e " ${COL_LIGHT_RED}Ignoring overrides specified within pihole.conf! ${COL_NC}" fi +# Determine if Pi-hole blocking is disabled +# If this is the case, we want to update +# gravity.list.bck and black.list.bck instead of +# gravity.list and black.list +detect_pihole_blocking_status() { + if [[ -e "${adList}.bck" ]]; then + adList="${adList}.bck" + fi + if [[ -e "${blackList}.bck" ]]; then + blackList="${blackList}.bck" + fi +} + # Determine if DNS resolution is available before proceeding gravity_CheckDNSResolutionAvailable() { local lookupDomain="pi.hole" @@ -621,6 +634,8 @@ if [[ "${forceDelete:-}" == true ]]; then echo -e "${OVER} ${TICK} ${str}" fi +detect_pihole_blocking_status + # Determine which functions to run if [[ "${skipDownload}" == false ]]; then # Gravity needs to download blocklists From d8abc1d2666d725e58da52bcb2e8191ff7b3b0db Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 13:49:47 +0200 Subject: [PATCH 04/11] Storing BLOCKING=true/false in setupVars.conf Signed-off-by: DL6ER --- pihole | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pihole b/pihole index a4fcadb3..e08e026a 100755 --- a/pihole +++ b/pihole @@ -191,6 +191,8 @@ Time: fi local str="Pi-hole Disabled" + sed -i "/BLOCKING=/d" "${setupVars}" + echo "BLOCKING=true" >> "${setupVars}" fi else # Enable Pi-hole @@ -203,6 +205,8 @@ Time: if [[ -e "${blacklist}" ]]; then mv "${blacklist}.bck" "${blacklist}" fi + sed -i "/BLOCKING=/d" "${setupVars}" + echo "BLOCKING=false" >> "${setupVars}" fi restartDNS reload From b011adc4535c6397ae7e7d198d964be0ab70bc58 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 14:19:59 +0200 Subject: [PATCH 05/11] Use new setupVars BLOCKING variable to determine which file to update via gravity Signed-off-by: DL6ER --- gravity.sh | 7 ++++--- pihole | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gravity.sh b/gravity.sh index 5624b659..3ccd5a5d 100755 --- a/gravity.sh +++ b/gravity.sh @@ -78,11 +78,12 @@ fi # gravity.list.bck and black.list.bck instead of # gravity.list and black.list detect_pihole_blocking_status() { - if [[ -e "${adList}.bck" ]]; then + if [[ "${BLOCKING}" == false ]]; then + echo -e " ${INFO} Pi-hole blocking is disabled" adList="${adList}.bck" - fi - if [[ -e "${blackList}.bck" ]]; then blackList="${blackList}.bck" + else + echo -e " ${INFO} Pi-hole blocking is enabled" fi } diff --git a/pihole b/pihole index e08e026a..8733c4af 100755 --- a/pihole +++ b/pihole @@ -12,6 +12,7 @@ readonly PI_HOLE_SCRIPT_DIR="/opt/pihole" readonly gravitylist="/etc/pihole/gravity.list" readonly blacklist="/etc/pihole/black.list" +readonly setupVars="/etc/pihole/setupVars.conf" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" source "${colfile}" @@ -192,7 +193,7 @@ Time: local str="Pi-hole Disabled" sed -i "/BLOCKING=/d" "${setupVars}" - echo "BLOCKING=true" >> "${setupVars}" + echo "BLOCKING=false" >> "${setupVars}" fi else # Enable Pi-hole @@ -206,7 +207,7 @@ Time: mv "${blacklist}.bck" "${blacklist}" fi sed -i "/BLOCKING=/d" "${setupVars}" - echo "BLOCKING=false" >> "${setupVars}" + echo "BLOCKING=true" >> "${setupVars}" fi restartDNS reload From 4e4d6b5d1f7a54c11502d37837bfc8b6afc5fe09 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 15:01:46 +0200 Subject: [PATCH 06/11] Adjust "pihole status" command Signed-off-by: DL6ER --- pihole | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pihole b/pihole index 8733c4af..2fcdbfbb 100755 --- a/pihole +++ b/pihole @@ -268,16 +268,17 @@ statusFunc() { return 0 fi - # Determine if Pi-hole's addn-hosts configs are commented out - addnConfigs=$(grep -i "addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) + # Determine if Pi-hole's blocking is enabled - if [[ "${addnConfigs}" =~ "#" ]]; then + addnConfigs=$? + + if grep -q "BLOCKING=false" /etc/pihole/setupVars.conf; then # A config is commented out case "${1}" in "web") echo 0;; *) echo -e " ${CROSS} Pi-hole blocking is Disabled";; esac - elif [[ -n "${addnConfigs}" ]]; then + elif grep -q "BLOCKING=true" /etc/pihole/setupVars.conf; then # Configs are set case "${1}" in "web") echo 1;; @@ -289,9 +290,8 @@ statusFunc() { "web") echo 99;; *) echo -e " ${INFO} No hosts file linked to dnsmasq, adding it in enabled state";; esac - # Add addn-host= to dnsmasq - echo "addn-hosts=/etc/pihole/gravity.list" >> /etc/dnsmasq.d/01-pihole.conf - restartDNS + # Enable blocking + pihole enable fi } From 337cc5ca187118e87c0200cede812a2c8f3239d7 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 17:22:10 +0200 Subject: [PATCH 07/11] BLOCKING -> BLOCKING_ENABLED Signed-off-by: DL6ER --- gravity.sh | 2 +- pihole | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gravity.sh b/gravity.sh index 3ccd5a5d..b5694b76 100755 --- a/gravity.sh +++ b/gravity.sh @@ -78,7 +78,7 @@ fi # gravity.list.bck and black.list.bck instead of # gravity.list and black.list detect_pihole_blocking_status() { - if [[ "${BLOCKING}" == false ]]; then + if [[ "${BLOCKING_ENABLED}" == false ]]; then echo -e " ${INFO} Pi-hole blocking is disabled" adList="${adList}.bck" blackList="${blackList}.bck" diff --git a/pihole b/pihole index 2fcdbfbb..b4e4b0b8 100755 --- a/pihole +++ b/pihole @@ -192,8 +192,8 @@ Time: fi local str="Pi-hole Disabled" - sed -i "/BLOCKING=/d" "${setupVars}" - echo "BLOCKING=false" >> "${setupVars}" + sed -i "/BLOCKING_ENABLED=/d" "${setupVars}" + echo "BLOCKING_ENABLED=false" >> "${setupVars}" fi else # Enable Pi-hole @@ -206,8 +206,8 @@ Time: if [[ -e "${blacklist}" ]]; then mv "${blacklist}.bck" "${blacklist}" fi - sed -i "/BLOCKING=/d" "${setupVars}" - echo "BLOCKING=true" >> "${setupVars}" + sed -i "/BLOCKING_ENABLED=/d" "${setupVars}" + echo "BLOCKING_ENABLED=true" >> "${setupVars}" fi restartDNS reload @@ -272,13 +272,13 @@ statusFunc() { addnConfigs=$? - if grep -q "BLOCKING=false" /etc/pihole/setupVars.conf; then + if grep -q "BLOCKING_ENABLED=false" /etc/pihole/setupVars.conf; then # A config is commented out case "${1}" in "web") echo 0;; *) echo -e " ${CROSS} Pi-hole blocking is Disabled";; esac - elif grep -q "BLOCKING=true" /etc/pihole/setupVars.conf; then + elif grep -q "BLOCKING_ENABLED=true" /etc/pihole/setupVars.conf; then # Configs are set case "${1}" in "web") echo 1;; From f794018e0cb0847b1a65818986c1e72b3c9199d0 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 18:17:14 +0200 Subject: [PATCH 08/11] Acknowledge temporary file if disabled for "pihole -b" command Signed-off-by: DL6ER --- advanced/Scripts/list.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/advanced/Scripts/list.sh b/advanced/Scripts/list.sh index 87e4ab44..55e07222 100755 --- a/advanced/Scripts/list.sh +++ b/advanced/Scripts/list.sh @@ -13,6 +13,10 @@ basename=pihole piholeDir=/etc/"${basename}" whitelist="${piholeDir}"/whitelist.txt blacklist="${piholeDir}"/blacklist.txt +if [[ "${BLOCKING_ENABLED}" == false ]]; then + blacklist="${blacklist}.bck" +fi + readonly regexlist="/etc/pihole/regex.list" reload=false addmode=true From 165affc39b9d787d0e9ab41005a7f8d15d26b840 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 13 Aug 2018 18:53:31 +0200 Subject: [PATCH 09/11] list.sh writes into blacklist.txt not black.list so does not need to account for a possibly changed list location Signed-off-by: DL6ER --- advanced/Scripts/list.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/advanced/Scripts/list.sh b/advanced/Scripts/list.sh index 55e07222..c1d95aae 100755 --- a/advanced/Scripts/list.sh +++ b/advanced/Scripts/list.sh @@ -13,9 +13,6 @@ basename=pihole piholeDir=/etc/"${basename}" whitelist="${piholeDir}"/whitelist.txt blacklist="${piholeDir}"/blacklist.txt -if [[ "${BLOCKING_ENABLED}" == false ]]; then - blacklist="${blacklist}.bck" -fi readonly regexlist="/etc/pihole/regex.list" reload=false From 0ebd68f17fe934e0a59967e3e700b58c839e0d57 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 14 Aug 2018 09:55:16 +0200 Subject: [PATCH 10/11] Review comments Signed-off-by: DL6ER --- pihole | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pihole b/pihole index b4e4b0b8..80b395a9 100755 --- a/pihole +++ b/pihole @@ -203,7 +203,7 @@ Time: if [[ -e "${gravitylist}.bck" ]]; then mv "${gravitylist}.bck" "${gravitylist}" fi - if [[ -e "${blacklist}" ]]; then + if [[ -e "${blacklist}.bck" ]]; then mv "${blacklist}.bck" "${blacklist}" fi sed -i "/BLOCKING_ENABLED=/d" "${setupVars}" @@ -253,8 +253,6 @@ Options: } statusFunc() { - local addnConfigs - # Determine if service is running on port 53 (Cr: https://superuser.com/a/806331) if (echo > /dev/tcp/127.0.0.1/53) >/dev/null 2>&1; then if [[ "${1}" != "web" ]]; then @@ -269,9 +267,6 @@ statusFunc() { fi # Determine if Pi-hole's blocking is enabled - - addnConfigs=$? - if grep -q "BLOCKING_ENABLED=false" /etc/pihole/setupVars.conf; then # A config is commented out case "${1}" in From f20e4ddf3bc62effe28e9b3c0d79b07301158a01 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 30 Aug 2018 22:28:15 +0200 Subject: [PATCH 11/11] Change message for unknown state to something more useful Signed-off-by: DL6ER --- pihole | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pihole b/pihole index 8a9cd2f3..4f76c98d 100755 --- a/pihole +++ b/pihole @@ -283,7 +283,7 @@ statusFunc() { # No configs were found case "${1}" in "web") echo 99;; - *) echo -e " ${INFO} No hosts file linked to dnsmasq, adding it in enabled state";; + *) echo -e " ${INFO} Pi-hole blocking will be enabled";; esac # Enable blocking pihole enable