From 8e4fc27168a424a0a628cb27ed5967958ee09c0b Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 24 Aug 2024 10:06:48 +0200 Subject: [PATCH] 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