From 8e4fc27168a424a0a628cb27ed5967958ee09c0b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Aug 2024 10:06:48 +0200 Subject: [PATCH 1/3] Add pihole api [endpoint] callback suitable for local API requests Signed-off-by: DL6ER --- advanced/Scripts/api.sh | 63 ++++++++++++++++++++++++++++++++++------- pihole | 2 ++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/advanced/Scripts/api.sh b/advanced/Scripts/api.sh index 5843c585..4776222b 100755 --- a/advanced/Scripts/api.sh +++ b/advanced/Scripts/api.sh @@ -146,19 +146,22 @@ GetFTLData() { response=$(curl -skS -w "%{http_code}" -X GET "${API_URL}$1" -H "Accept: application/json" -H "sid: ${SID}" ) # status are the last 3 characters - status=$(printf %s "${response#"${response%???}"}") + status="${response#"${response%???}"}" # data is everything from response without the last 3 characters - data=$(printf %s "${response%???}") + data="${response%???}" - if [ "${status}" = 200 ]; then - # response OK - printf %s "${data}" - elif [ "${status}" = 000 ]; then - # connection lost - echo "000" - elif [ "${status}" = 401 ]; then - # unauthorized - echo "401" + if [ "${2}" = "raw" ]; then + # return the raw response + echo "${response}" + else + # return only the data + if [ "${status}" = 200 ]; then + # response OK + echo "${data}" + else + # connection lost + echo "${status}" + fi fi } @@ -226,3 +229,41 @@ secretRead() { # restore original terminal settings stty "${stty_orig}" } + +apiFunc() { + local data response status status_col + + # Authenticate with the API + LoginAPI + + echo "Requesting: ${COL_PURPLE}GET ${COL_CYAN}${API_URL}${COL_YELLOW}$1${COL_NC}" + echo "" + + # Get the data from the API + response=$(GetFTLData "$1" raw) + + # status are the last 3 characters + status="${response#"${response%???}"}" + # data is everything from response without the last 3 characters + data="${response%???}" + + # Output the status (200 -> green, else red) + if [ "${status}" = 200 ]; then + status_col="${COL_GREEN}" + else + status_col="${COL_RED}" + fi + echo "Status: ${status_col}${status}${COL_NC}" + + # Output the data. Format it with jq if available and data is actually JSON. + # Otherwise just print it + echo "Data:" + if command -v jq >/dev/null && echo "${data}" | jq . >/dev/null 2>&1; then + echo "${data}" | jq . + else + echo "${data}" + fi + + # Delete the session + LogoutAPI +} diff --git a/pihole b/pihole index 4bb7d5e5..8a9aa297 100755 --- a/pihole +++ b/pihole @@ -550,6 +550,7 @@ case "${1}" in "updatechecker" ) ;; "arpflush" ) ;; "-t" | "tail" ) ;; + "api" ) need_root=0;; * ) helpFunc;; esac @@ -591,5 +592,6 @@ case "${1}" in "updatechecker" ) shift; updateCheckFunc "$@";; "arpflush" ) arpFunc "$@";; "-t" | "tail" ) tailFunc "$2";; + "api" ) apiFunc "$2";; * ) helpFunc;; esac From b304562c8ebbb9ecf3a678bd06be2ffe72e90161 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Aug 2024 10:37:58 +0200 Subject: [PATCH 2/3] Add session details to API command output Signed-off-by: DL6ER --- advanced/Scripts/api.sh | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/advanced/Scripts/api.sh b/advanced/Scripts/api.sh index 4776222b..43c91d69 100755 --- a/advanced/Scripts/api.sh +++ b/advanced/Scripts/api.sh @@ -88,6 +88,9 @@ LoginAPI() { # Exit early if authentication is not needed if [ "${needAuth}" = false ]; then + if [ "${1}" = "verbose" ]; then + echo "API Authentication: Not needed" + fi return fi @@ -95,8 +98,15 @@ LoginAPI() { if [ -r /etc/pihole/cli_pw ]; then password=$(cat /etc/pihole/cli_pw) + if [ "${1}" = "verbose" ]; then + echo "API Authentication: Trying to use CLI password" + fi + # Try to authenticate using the CLI password - Authentication + Authentication "${1}" + + elif [ "${1}" = "verbose" ]; then + echo "API Authentication: CLI password not available" fi @@ -109,7 +119,7 @@ LoginAPI() { secretRead; printf '\n' # Try to authenticate again - Authentication + Authentication "${1}" done } @@ -124,6 +134,14 @@ Authentication() { # obtain validity and session ID from session response validSession=$(echo "${sessionResponse}"| jq .session.valid 2>/dev/null) SID=$(echo "${sessionResponse}"| jq --raw-output .session.sid 2>/dev/null) + + if [ "${1}" = "verbose" ]; then + if [ "${validSession}" = true ]; then + echo "API Authentication: ${COL_GREEN}Success${COL_NC}" + else + echo "API Authentication: ${COL_RED}Failed${COL_NC}" + fi + fi } LogoutAPI() { @@ -134,10 +152,12 @@ LogoutAPI() { deleteResponse=$(curl -skS -o /dev/null -w "%{http_code}" -X DELETE "${API_URL}auth" -H "Accept: application/json" -H "sid: ${SID}") case "${deleteResponse}" in - "401") printf "%b" "Logout attempt without a valid session. Unauthorized!\n";; - esac; + "401") echo "Logout attempt without a valid session. Unauthorized!";; + "204") if [ "${1}" = "verbose" ]; then echo "API Logout: ${COL_GREEN}Success${COL_NC} (session deleted)"; fi;; + esac; + elif [ "${1}" = "verbose" ]; then + echo "API Logout: ${COL_GREEN}Success${COL_NC} (no valid session)" fi - } GetFTLData() { @@ -234,7 +254,8 @@ apiFunc() { local data response status status_col # Authenticate with the API - LoginAPI + LoginAPI verbose + echo "" echo "Requesting: ${COL_PURPLE}GET ${COL_CYAN}${API_URL}${COL_YELLOW}$1${COL_NC}" echo "" @@ -265,5 +286,5 @@ apiFunc() { fi # Delete the session - LogoutAPI + LogoutAPI verbose } From e8582f774099928741351fcc961f407977c663e6 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 26 Aug 2024 19:44:42 +0200 Subject: [PATCH 3/3] Add pihole -h and autocompletion entry Signed-off-by: DL6ER --- advanced/bash-completion/pihole | 2 +- pihole | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/advanced/bash-completion/pihole b/advanced/bash-completion/pihole index 112e64ab..674eeea4 100644 --- a/advanced/bash-completion/pihole +++ b/advanced/bash-completion/pihole @@ -7,7 +7,7 @@ _pihole() { case "${prev}" in "pihole") - opts="allow allow-regex allow-wild deny checkout debug disable enable flush help logging query reconfigure regex restartdns status tail uninstall updateGravity updatePihole version wildcard arpflush" + opts="allow allow-regex allow-wild deny checkout debug disable enable flush help logging query reconfigure regex restartdns status tail uninstall updateGravity updatePihole version wildcard arpflush api" COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) ;; "allow"|"deny"|"wildcard"|"regex"|"allow-regex"|"allow-wild") diff --git a/pihole b/pihole index 8a9aa297..ed4dd67a 100755 --- a/pihole +++ b/pihole @@ -484,6 +484,7 @@ Debugging Options: -t, tail [arg] View the live output of the Pi-hole log. Add an optional argument to filter the log (regular expressions are supported) + api Query the Pi-hole API at Options: