Compare commits

..

4 Commits

Author SHA1 Message Date
RD WebDesign ac78066657 Fix the github suggestion mess
CodeQL / Analyze (pull_request) Has been cancelled
Test Supported Distributions / smoke-tests (pull_request) Has been cancelled
Test Supported Distributions / distro-test (alpine_3_21) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (alpine_3_22) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (centos_10) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (centos_9) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (debian_11) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (debian_12) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (debian_13) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (fedora_40) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (fedora_41) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (fedora_42) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (ubuntu_20) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (ubuntu_22) (pull_request) Has been cancelled
Test Supported Distributions / distro-test (ubuntu_24) (pull_request) Has been cancelled
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
2025-10-19 16:14:55 -03:00
RD WebDesign f4a395cb06 Apply suggestion from @MichaIng
Do not handle HTTP code "000" separately.
Use curl error messages for every error, including unknown/unexpected HTTP codes or non-HTTP errors.

Co-authored-by: MichaIng <micha@dietpi.com>
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
2025-10-19 14:48:54 -03:00
RD WebDesign 0cfc02cbab Apply suggestion from @MichaIng
Remove the STDERR mute from curl command.

The `-s` flag already mutes all curl errors, so STDERR would only contain something if curl itself (or a shared library) is damaged/missing.

Co-authored-by: MichaIng <micha@dietpi.com>
Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
2025-10-19 14:41:39 -03:00
RD WebDesign 4e191da1a0 Improve curl error message including exit code and error message
This commit replaces the 3 digits http_code returned by curl with the json
output. This output contains all returned values, including http_code,
exitcode and errormsg.

Using json format, the old http_error "000" string is formated as a number "0".

Signed-off-by: RD WebDesign <github@rdwebdesign.com.br>
2025-10-17 22:37:45 -03:00
9 changed files with 82 additions and 134 deletions
+3 -3
View File
@@ -29,12 +29,12 @@ jobs:
# Initializes the CodeQL tools for scanning. # Initializes the CodeQL tools for scanning.
- -
name: Initialize CodeQL name: Initialize CodeQL
uses: github/codeql-action/init@16140ae1a102900babc80a33c44059580f687047 #v4.30.9 uses: github/codeql-action/init@f443b600d91635bebf5b0d9ebc620189c0d6fba5 #v4.30.8
with: with:
languages: 'python' languages: 'python'
- -
name: Autobuild name: Autobuild
uses: github/codeql-action/autobuild@16140ae1a102900babc80a33c44059580f687047 #v4.30.9 uses: github/codeql-action/autobuild@f443b600d91635bebf5b0d9ebc620189c0d6fba5 #v4.30.8
- -
name: Perform CodeQL Analysis name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@16140ae1a102900babc80a33c44059580f687047 #v4.30.9 uses: github/codeql-action/analyze@f443b600d91635bebf5b0d9ebc620189c0d6fba5 #v4.30.8
+1 -1
View File
@@ -43,7 +43,7 @@ jobs:
ignore_words_file: .codespellignore ignore_words_file: .codespellignore
- name: Get editorconfig-checker - name: Get editorconfig-checker
uses: editorconfig-checker/action-editorconfig-checker@5ecdd656fe347c26f76b1b435b90e1d74fb5e787 # tag v2. is really out of date uses: editorconfig-checker/action-editorconfig-checker@1a41284d59c6fe7f1b21ddc4a2b36400a33dc1b4 # tag v2. is really out of date
- name: Run editorconfig-checker - name: Run editorconfig-checker
run: editorconfig-checker run: editorconfig-checker
+60 -18
View File
@@ -17,6 +17,11 @@ utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
# shellcheck source="./advanced/Scripts/utils.sh" # shellcheck source="./advanced/Scripts/utils.sh"
source "${utilsfile}" source "${utilsfile}"
# In case we're running at the same time as a system logrotate, use a
# separate logrotate state file to prevent stepping on each other's
# toes.
STATEFILE="/var/lib/logrotate/pihole"
# Determine database location # Determine database location
DBFILE=$(getFTLConfigValue "files.database") DBFILE=$(getFTLConfigValue "files.database")
if [ -z "$DBFILE" ]; then if [ -z "$DBFILE" ]; then
@@ -37,6 +42,25 @@ if [ -z "$WEBFILE" ]; then
WEBFILE="/var/log/pihole/webserver.log" WEBFILE="/var/log/pihole/webserver.log"
fi fi
# Helper function to handle log rotation for a single file
rotate_log() {
# This function copies x.log over to x.log.1
# and then empties x.log
# Note that moving the file is not an option, as
# dnsmasq would happily continue writing into the
# moved file (it will have the same file handler)
local logfile="$1"
if [[ "$*" != *"quiet"* ]]; then
echo -ne " ${INFO} Rotating ${logfile} ..."
fi
cp -p "${logfile}" "${logfile}.1"
echo " " > "${logfile}"
chmod 640 "${logfile}"
if [[ "$*" != *"quiet"* ]]; then
echo -e "${OVER} ${TICK} Rotated ${logfile} ..."
fi
}
# Helper function to handle log flushing for a single file # Helper function to handle log flushing for a single file
flush_log() { flush_log() {
local logfile="$1" local logfile="$1"
@@ -54,23 +78,41 @@ flush_log() {
fi fi
} }
# Manual flushing if [[ "$*" == *"once"* ]]; then
flush_log "${LOGFILE}" # Nightly logrotation
flush_log "${FTLFILE}" if command -v /usr/sbin/logrotate >/dev/null; then
flush_log "${WEBFILE}" # Logrotate once
if [[ "$*" != *"quiet"* ]]; then if [[ "$*" != *"quiet"* ]]; then
echo -ne " ${INFO} Flushing database, DNS resolution temporarily unavailable ..." echo -ne " ${INFO} Running logrotate ..."
fi fi
mkdir -p "${STATEFILE%/*}"
# Stop FTL to make sure it doesn't write to the database while we're deleting data /usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
service pihole-FTL stop else
# Handle rotation for each log file
# Delete most recent 24 hours from FTL's database, leave even older data intact (don't wipe out all history) rotate_log "${LOGFILE}"
deleted=$(pihole-FTL sqlite3 -ni "${DBFILE}" "DELETE FROM query_storage WHERE timestamp >= strftime('%s','now')-86400; select changes() from query_storage limit 1") rotate_log "${FTLFILE}"
rotate_log "${WEBFILE}"
# Restart FTL fi
service pihole-FTL restart else
if [[ "$*" != *"quiet"* ]]; then # Manual flushing
echo -e "${OVER} ${TICK} Deleted ${deleted} queries from long-term query database" flush_log "${LOGFILE}"
flush_log "${FTLFILE}"
flush_log "${WEBFILE}"
if [[ "$*" != *"quiet"* ]]; then
echo -ne " ${INFO} Flushing database, DNS resolution temporarily unavailable ..."
fi
# Stop FTL to make sure it doesn't write to the database while we're deleting data
service pihole-FTL stop
# Delete most recent 24 hours from FTL's database, leave even older data intact (don't wipe out all history)
deleted=$(pihole-FTL sqlite3 -ni "${DBFILE}" "DELETE FROM query_storage WHERE timestamp >= strftime('%s','now')-86400; select changes() from query_storage limit 1")
# Restart FTL
service pihole-FTL restart
if [[ "$*" != *"quiet"* ]]; then
echo -e "${OVER} ${TICK} Deleted ${deleted} queries from long-term query database"
fi
fi fi
-72
View File
@@ -1,72 +0,0 @@
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2025 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# Rotate Pi-hole's log file
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.
colfile="/opt/pihole/COL_TABLE"
# shellcheck source="./advanced/Scripts/COL_TABLE"
source ${colfile}
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
# shellcheck source="./advanced/Scripts/utils.sh"
source "${utilsfile}"
# In case we're running at the same time as a system logrotate, use a
# separate logrotate state file to prevent stepping on each other's
# toes.
STATEFILE="/var/lib/logrotate/pihole"
# Determine log file location
LOGFILE=$(getFTLConfigValue "files.log.dnsmasq")
if [ -z "$LOGFILE" ]; then
LOGFILE="/var/log/pihole/pihole.log"
fi
FTLFILE=$(getFTLConfigValue "files.log.ftl")
if [ -z "$FTLFILE" ]; then
FTLFILE="/var/log/pihole/FTL.log"
fi
WEBFILE=$(getFTLConfigValue "files.log.webserver")
if [ -z "$WEBFILE" ]; then
WEBFILE="/var/log/pihole/webserver.log"
fi
# Helper function to handle log rotation for a single file
rotate_log() {
# This function copies x.log over to x.log.1
# and then empties x.log
# Note that moving the file is not an option, as
# dnsmasq would happily continue writing into the
# moved file (it will have the same file handler)
local logfile="$1"
if [[ "$*" != *"quiet"* ]]; then
echo -ne " ${INFO} Rotating ${logfile} ..."
fi
cp -p "${logfile}" "${logfile}.1"
echo " " > "${logfile}"
chmod 640 "${logfile}"
if [[ "$*" != *"quiet"* ]]; then
echo -e "${OVER} ${TICK} Rotated ${logfile} ..."
fi
}
# Nightly logrotation
if command -v /usr/sbin/logrotate >/dev/null; then
# Logrotate once
if [[ "$*" != *"quiet"* ]]; then
echo -ne " ${INFO} Running logrotate ..."
fi
mkdir -p "${STATEFILE%/*}"
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
else
# Handle rotation for each log file
rotate_log "${LOGFILE}"
rotate_log "${FTLFILE}"
rotate_log "${WEBFILE}"
fi
+1 -1
View File
@@ -24,7 +24,7 @@
# The flush script will use logrotate if available # The flush script will use logrotate if available
# parameter "once": logrotate only once (default is twice) # parameter "once": logrotate only once (default is twice)
# parameter "quiet": don't print messages # parameter "quiet": don't print messages
00 00 * * * root PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole logrotate quiet 00 00 * * * root PATH="$PATH:/usr/sbin:/usr/local/bin/" pihole flush once quiet
@reboot root /usr/sbin/logrotate --state /var/lib/logrotate/pihole /etc/pihole/logrotate @reboot root /usr/sbin/logrotate --state /var/lib/logrotate/pihole /etc/pihole/logrotate
+1 -1
View File
@@ -84,7 +84,7 @@ webInterfaceDir="${webroot}/admin"
piholeGitUrl="https://github.com/pi-hole/pi-hole.git" piholeGitUrl="https://github.com/pi-hole/pi-hole.git"
PI_HOLE_LOCAL_REPO="/etc/.pihole" PI_HOLE_LOCAL_REPO="/etc/.pihole"
# List of pihole scripts, stored in an array # List of pihole scripts, stored in an array
PI_HOLE_FILES=(list piholeDebug piholeLogFlush piholeLogRotate setupLCD update version gravity uninstall webpage) PI_HOLE_FILES=(list piholeDebug piholeLogFlush setupLCD update version gravity uninstall webpage)
# This directory is where the Pi-hole scripts will be installed # This directory is where the Pi-hole scripts will be installed
PI_HOLE_INSTALL_DIR="/opt/pihole" PI_HOLE_INSTALL_DIR="/opt/pihole"
PI_HOLE_CONFIG_DIR="/etc/pihole" PI_HOLE_CONFIG_DIR="/etc/pihole"
+10 -6
View File
@@ -611,7 +611,7 @@ compareLists() {
# Download specified URL and perform checks on HTTP status and file content # Download specified URL and perform checks on HTTP status and file content
gravity_DownloadBlocklistFromUrl() { gravity_DownloadBlocklistFromUrl() {
local url="${1}" adlistID="${2}" saveLocation="${3}" compression="${4}" gravity_type="${5}" domain="${6}" local url="${1}" adlistID="${2}" saveLocation="${3}" compression="${4}" gravity_type="${5}" domain="${6}"
local listCurlBuffer str httpCode success="" ip customUpstreamResolver="" local listCurlBuffer str curlJson httpCode curlErrorMsg="" curlExitCode="" success="" ip customUpstreamResolver=""
local file_path permissions ip_addr port blocked=false download=true local file_path permissions ip_addr port blocked=false download=true
# modifiedOptions is an array to store all the options used to check if the adlist has been changed upstream # modifiedOptions is an array to store all the options used to check if the adlist has been changed upstream
local modifiedOptions=() local modifiedOptions=()
@@ -750,14 +750,19 @@ gravity_DownloadBlocklistFromUrl() {
# Check for allowed protocols # Check for allowed protocols
if [[ $url != "http"* && $url != "https"* && $url != "file"* && $url != "ftp"* && $url != "ftps"* && $url != "sftp"* ]]; then if [[ $url != "http"* && $url != "https"* && $url != "file"* && $url != "ftp"* && $url != "ftps"* && $url != "sftp"* ]]; then
echo -e "${OVER} ${CROSS} ${str} Invalid protocol specified. Ignoring list." echo -e "${OVER} ${CROSS} ${str} Invalid protocol specified. Ignoring list."
echo -e " Ensure your URL starts with a valid protocol like http:// , https:// or file:// ." echo -e "Ensure your URL starts with a valid protocol like http:// , https:// or file:// ."
download=false download=false
fi fi
if [[ "${download}" == true ]]; then if [[ "${download}" == true ]]; then
httpCode=$(curl --connect-timeout ${curl_connect_timeout} -s -L ${compression:+${compression}} ${customUpstreamResolver:+${customUpstreamResolver}} "${modifiedOptions[@]}" -w "%{http_code}" "${url}" -o "${listCurlBuffer}" 2>/dev/null) curlJson=$(curl --connect-timeout ${curl_connect_timeout} -s -L ${compression:+${compression}} ${customUpstreamResolver:+${customUpstreamResolver}} "${modifiedOptions[@]}" -w "%{json}" "${url}" -o "${listCurlBuffer}")
fi fi
# Retrieve the HTTP code, exit code and error message returned by curl command
httpCode=$(echo "${curlJson}" | jq '.http_code')
curlErrorMsg=$(echo "${curlJson}" | jq '.errormsg')
curlExitCode=$(echo "${curlJson}" | jq '.exitcode')
case $url in case $url in
# Did we "download" a local file? # Did we "download" a local file?
"file"*) "file"*)
@@ -780,7 +785,6 @@ gravity_DownloadBlocklistFromUrl() {
echo -e "${OVER} ${TICK} ${str} No changes detected" echo -e "${OVER} ${TICK} ${str} No changes detected"
success=true success=true
;; ;;
"000") echo -e "${OVER} ${CROSS} ${str} Connection Refused" ;;
"403") echo -e "${OVER} ${CROSS} ${str} Forbidden" ;; "403") echo -e "${OVER} ${CROSS} ${str} Forbidden" ;;
"404") echo -e "${OVER} ${CROSS} ${str} Not found" ;; "404") echo -e "${OVER} ${CROSS} ${str} Not found" ;;
"408") echo -e "${OVER} ${CROSS} ${str} Time-out" ;; "408") echo -e "${OVER} ${CROSS} ${str} Time-out" ;;
@@ -789,7 +793,7 @@ gravity_DownloadBlocklistFromUrl() {
"504") echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Gateway)" ;; "504") echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Gateway)" ;;
"521") echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)" ;; "521") echo -e "${OVER} ${CROSS} ${str} Web Server Is Down (Cloudflare)" ;;
"522") echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)" ;; "522") echo -e "${OVER} ${CROSS} ${str} Connection Timed Out (Cloudflare)" ;;
*) echo -e "${OVER} ${CROSS} ${str} ${url} (${httpCode})" ;; *) echo -e "${OVER} ${CROSS} ${str} Failure (exit_code=${COL_RED}${curlExitCode}${COL_NC} Msg: ${COL_CYAN}${curlErrorMsg}${COL_NC})" ;;
esac esac
;; ;;
esac esac
@@ -1130,7 +1134,7 @@ fi
if [[ "${forceDelete:-}" == true ]]; then if [[ "${forceDelete:-}" == true ]]; then
str="Deleting existing list cache" str="Deleting existing list cache"
echo -ne " ${INFO} ${str}..." echo -ne "${INFO} ${str}..."
rm "${listsCacheDir}/list.*" 2>/dev/null || true rm "${listsCacheDir}/list.*" 2>/dev/null || true
echo -e "${OVER} ${TICK} ${str}" echo -e "${OVER} ${TICK} ${str}"
+2 -13
View File
@@ -100,12 +100,9 @@ Available commands and options:
-c Include a Pi-hole database integrity check -c Include a Pi-hole database integrity check
.br .br
\fB-f, flush\fR [quite] \fB-f, flush\fR
.br .br
Flush the Pi-hole log and last 24h from the query database Flush the Pi-hole log
.br
quite Suppress output
.br .br
\fB-r, repair\fR \fB-r, repair\fR
@@ -245,14 +242,6 @@ Available commands and options:
verbose Show authentication and status messages verbose Show authentication and status messages
.br .br
\fBlogrotate\fR [quite]
.br
Rotate Pi-hole's log files
.br
quite Suppress output
.br
.SH "EXAMPLE" .SH "EXAMPLE"
Some usage examples Some usage examples
+4 -19
View File
@@ -92,13 +92,8 @@ debugFunc() {
} }
flushFunc() { flushFunc() {
# unsupported in docker because it requires restarting FTL "${PI_HOLE_SCRIPT_DIR}"/piholeLogFlush.sh "$@"
if [ -n "${DOCKER_VERSION}" ]; then exit 0
unsupportedFunc
else
"${PI_HOLE_SCRIPT_DIR}"/piholeLogFlush.sh "$@"
exit 0
fi
} }
# Deprecated function, should be removed in the future # Deprecated function, should be removed in the future
@@ -110,11 +105,6 @@ arpFunc() {
exit 0 exit 0
} }
logrotateFunc() {
"${PI_HOLE_SCRIPT_DIR}"/piholeLogRotate.sh "$@"
exit 0
}
networkFlush() { networkFlush() {
shift shift
"${PI_HOLE_SCRIPT_DIR}"/piholeNetworkFlush.sh "$@" "${PI_HOLE_SCRIPT_DIR}"/piholeNetworkFlush.sh "$@"
@@ -512,8 +502,7 @@ Debugging Options:
-d, debug Start a debugging session -d, debug Start a debugging session
Add '-c' or '--check-database' to include a Pi-hole database integrity check Add '-c' or '--check-database' to include a Pi-hole database integrity check
Add '-a' to automatically upload the log to tricorder.pi-hole.net Add '-a' to automatically upload the log to tricorder.pi-hole.net
-f, flush Flush the Pi-hole logs and last 24h from the query database -f, flush Flush the Pi-hole log
Add 'quiet' to suppress output messages
-r, repair Repair Pi-hole subsystems -r, repair Repair Pi-hole subsystems
-t, tail [arg] View the live output of the Pi-hole log. -t, tail [arg] View the live output of the Pi-hole log.
Add an optional argument to filter the log Add an optional argument to filter the log
@@ -546,9 +535,7 @@ Options:
checkout Switch Pi-hole subsystems to a different GitHub branch checkout Switch Pi-hole subsystems to a different GitHub branch
Add '-h' for more info on checkout usage Add '-h' for more info on checkout usage
networkflush Flush information stored in Pi-hole's network tables networkflush Flush information stored in Pi-hole's network tables
Add '--arp' to additionally flush the ARP table Add '--arp' to additionally flush the ARP table ";
logrotate Rotate Pi-hole's log files
Add 'quiet' to suppress output messages";
exit 0 exit 0
} }
@@ -591,7 +578,6 @@ case "${1}" in
"arpflush" ) need_root=true;; # Deprecated, use networkflush instead "arpflush" ) need_root=true;; # Deprecated, use networkflush instead
"networkflush" ) need_root=true;; "networkflush" ) need_root=true;;
"-t" | "tail" ) need_root=true;; "-t" | "tail" ) need_root=true;;
"logrotate" ) need_root=true;;
* ) helpFunc;; * ) helpFunc;;
esac esac
@@ -627,6 +613,5 @@ case "${1}" in
"arpflush" ) arpFunc "$@";; # Deprecated, use networkflush instead "arpflush" ) arpFunc "$@";; # Deprecated, use networkflush instead
"networkflush" ) networkFlush "$@";; "networkflush" ) networkFlush "$@";;
"-t" | "tail" ) tailFunc "$2";; "-t" | "tail" ) tailFunc "$2";;
"logrotate" ) logrotateFunc "$@";;
* ) helpFunc;; * ) helpFunc;;
esac esac