Implement pihole enable/disable using the API

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2024-07-16 10:55:28 +02:00
parent 7ca4b59b34
commit 897e23089c
No known key found for this signature in database
GPG key ID: 00135ACBD90B28DD
2 changed files with 57 additions and 89 deletions

View file

@ -1,23 +0,0 @@
#!/bin/bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2020 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
#
#
# The pihole disable command has the option to set a specified time before
# blocking is automatically re-enabled.
#
# Present script is responsible for the sleep & re-enable part of the job and
# is automatically terminated if it is still running when pihole is enabled by
# other means.
#
# This ensures that pihole ends up in the correct state after a sequence of
# commands suchs as: `pihole disable 30s; pihole enable; pihole disable`
readonly PI_HOLE_BIN_DIR="/usr/local/bin"
sleep "${1}"
"${PI_HOLE_BIN_DIR}"/pihole enable

123
pihole
View file

@ -19,9 +19,13 @@ PI_HOLE_BIN_DIR="/usr/local/bin"
readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE" readonly colfile="${PI_HOLE_SCRIPT_DIR}/COL_TABLE"
source "${colfile}" source "${colfile}"
utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh" readonly utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
source "${utilsfile}" source "${utilsfile}"
# Source api functions
readonly apifile="${PI_HOLE_SCRIPT_DIR}/api.sh"
source "${apifile}"
versionsfile="/etc/pihole/versions" versionsfile="/etc/pihole/versions"
if [ -f "${versionsfile}" ]; then if [ -f "${versionsfile}" ]; then
# Only source versionsfile if the file exits # Only source versionsfile if the file exits
@ -205,73 +209,60 @@ restartDNS() {
piholeEnable() { piholeEnable() {
if [[ "${2}" == "-h" ]] || [[ "${2}" == "--help" ]]; then if [[ "${2}" == "-h" ]] || [[ "${2}" == "--help" ]]; then
echo "Usage: pihole disable [time] echo "Usage: pihole enable/disable [time]
Example: 'pihole disable', or 'pihole disable 5m' Example: 'pihole enable', or 'pihole disable 5m'
Disable Pi-hole subsystems En- or disable Pi-hole subsystems
Time: Time:
#s Disable Pi-hole functionality for # second(s) #s En-/disable Pi-hole functionality for # second(s)
#m Disable Pi-hole functionality for # minute(s)" #m En-/disable Pi-hole functionality for # minute(s)"
exit 0 exit 0
elif [[ "${1}" == "0" ]]; then
# Disable Pi-hole
if ! getFTLConfigValue dns.blocking.active; then
echo -e " ${INFO} Blocking already disabled, nothing to do"
exit 0
fi
if [[ $# -gt 1 ]]; then
local error=false
if [[ "${2}" == *"s" ]]; then
tt=${2%"s"}
if [[ "${tt}" =~ ^-?[0-9]+$ ]];then
local str="Disabling blocking for ${tt} seconds"
echo -e " ${INFO} ${str}..."
local str="Blocking will be re-enabled in ${tt} seconds"
nohup "${PI_HOLE_SCRIPT_DIR}"/pihole-reenable.sh ${tt} </dev/null &>/dev/null &
else
local error=true
fi
elif [[ "${2}" == *"m" ]]; then
tt=${2%"m"}
if [[ "${tt}" =~ ^-?[0-9]+$ ]];then
local str="Disabling blocking for ${tt} minutes"
echo -e " ${INFO} ${str}..."
local str="Blocking will be re-enabled in ${tt} minutes"
tt=$((${tt}*60))
nohup "${PI_HOLE_SCRIPT_DIR}"/pihole-reenable.sh ${tt} </dev/null &>/dev/null &
else
local error=true
fi
elif [[ -n "${2}" ]]; then
local error=true
else
echo -e " ${INFO} Disabling blocking"
fi
if [[ ${error} == true ]];then
echo -e " ${COL_LIGHT_RED}Unknown format for delayed reactivation of the blocking!${COL_NC}"
echo -e " Try 'pihole disable --help' for more information."
exit 1
fi
local str="Pi-hole Disabled"
setFTLConfigValue dns.blocking.active false
fi
else
# Enable Pi-hole
killall -q pihole-reenable
if getFTLConfigValue dns.blocking.active; then
echo -e " ${INFO} Blocking already enabled, nothing to do"
exit 0
fi
echo -e " ${INFO} Enabling blocking"
local str="Pi-hole Enabled"
setFTLConfigValue dns.blocking.active true
fi fi
restartDNS reload-lists # Get timer
local tt="null"
if [[ $# -gt 1 ]]; then
local error=false
if [[ "${2}" == *"s" ]]; then
tt=${2%"s"}
if [[ ! "${tt}" =~ ^-?[0-9]+$ ]];then
local error=true
fi
elif [[ "${2}" == *"m" ]]; then
tt=${2%"m"}
if [[ "${tt}" =~ ^-?[0-9]+$ ]];then
tt=$((${tt}*60))
else
local error=true
fi
elif [[ -n "${2}" ]]; then
local error=true
fi
if [[ ${error} == true ]];then
echo -e " ${COL_LIGHT_RED}Unknown format for blocking timer!${COL_NC}"
echo -e " Try 'pihole disable --help' for more information."
exit 1
fi
fi
# Authenticate with the API
LoginAPI
# Send the request
data=$(PostFTLData "dns/blocking" "{ \"blocking\": ${1}, \"timer\": ${tt} }")
# Check the response
local extra=" forever"
local timer="$(echo "${data}"| jq --raw-output '.timer' )"
if [[ "${timer}" != "null" ]]; then
extra=" for ${timer}s"
fi
local str="Pi-hole $(echo "${data}" | jq --raw-output '.blocking')${extra}"
# Logout from the API
LogoutAPI
echo -e "${OVER} ${TICK} ${str}" echo -e "${OVER} ${TICK} ${str}"
} }
@ -548,8 +539,8 @@ case "${1}" in
"-r" | "reconfigure" ) ;; "-r" | "reconfigure" ) ;;
"-l" | "logging" ) ;; "-l" | "logging" ) ;;
"uninstall" ) ;; "uninstall" ) ;;
"enable" ) ;; "enable" ) need_root=0;;
"disable" ) ;; "disable" ) need_root=0;;
"-d" | "debug" ) ;; "-d" | "debug" ) ;;
"restartdns" ) ;; "restartdns" ) ;;
"-g" | "updateGravity" ) ;; "-g" | "updateGravity" ) ;;
@ -591,8 +582,8 @@ case "${1}" in
"-g" | "updateGravity" ) updateGravityFunc "$@";; "-g" | "updateGravity" ) updateGravityFunc "$@";;
"-l" | "logging" ) piholeLogging "$@";; "-l" | "logging" ) piholeLogging "$@";;
"uninstall" ) uninstallFunc;; "uninstall" ) uninstallFunc;;
"enable" ) piholeEnable 1;; "enable" ) piholeEnable true "$2";;
"disable" ) piholeEnable 0 "$2";; "disable" ) piholeEnable false "$2";;
"restartdns" ) restartDNS "$2";; "restartdns" ) restartDNS "$2";;
"reloaddns" ) restartDNS "reload";; "reloaddns" ) restartDNS "reload";;
"setpassword" ) SetWebPassword "$@";; "setpassword" ) SetWebPassword "$@";;