mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-25 07:33:43 +00:00
Add pihole api [endpoint] callback suitable for local API requests (#5736)
This commit is contained in:
commit
c26b043c58
3 changed files with 82 additions and 17 deletions
|
@ -88,6 +88,9 @@ LoginAPI() {
|
||||||
|
|
||||||
# Exit early if authentication is not needed
|
# Exit early if authentication is not needed
|
||||||
if [ "${needAuth}" = false ]; then
|
if [ "${needAuth}" = false ]; then
|
||||||
|
if [ "${1}" = "verbose" ]; then
|
||||||
|
echo "API Authentication: Not needed"
|
||||||
|
fi
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -95,8 +98,15 @@ LoginAPI() {
|
||||||
if [ -r /etc/pihole/cli_pw ]; then
|
if [ -r /etc/pihole/cli_pw ]; then
|
||||||
password=$(cat /etc/pihole/cli_pw)
|
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
|
# Try to authenticate using the CLI password
|
||||||
Authentication
|
Authentication "${1}"
|
||||||
|
|
||||||
|
elif [ "${1}" = "verbose" ]; then
|
||||||
|
echo "API Authentication: CLI password not available"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,7 +119,7 @@ LoginAPI() {
|
||||||
secretRead; printf '\n'
|
secretRead; printf '\n'
|
||||||
|
|
||||||
# Try to authenticate again
|
# Try to authenticate again
|
||||||
Authentication
|
Authentication "${1}"
|
||||||
done
|
done
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -124,6 +134,14 @@ Authentication() {
|
||||||
# obtain validity and session ID from session response
|
# obtain validity and session ID from session response
|
||||||
validSession=$(echo "${sessionResponse}"| jq .session.valid 2>/dev/null)
|
validSession=$(echo "${sessionResponse}"| jq .session.valid 2>/dev/null)
|
||||||
SID=$(echo "${sessionResponse}"| jq --raw-output .session.sid 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() {
|
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}")
|
deleteResponse=$(curl -skS -o /dev/null -w "%{http_code}" -X DELETE "${API_URL}auth" -H "Accept: application/json" -H "sid: ${SID}")
|
||||||
|
|
||||||
case "${deleteResponse}" in
|
case "${deleteResponse}" in
|
||||||
"401") printf "%b" "Logout attempt without a valid session. Unauthorized!\n";;
|
"401") echo "Logout attempt without a valid session. Unauthorized!";;
|
||||||
esac;
|
"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
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GetFTLData() {
|
GetFTLData() {
|
||||||
|
@ -146,19 +166,22 @@ GetFTLData() {
|
||||||
response=$(curl -skS -w "%{http_code}" -X GET "${API_URL}$1" -H "Accept: application/json" -H "sid: ${SID}" )
|
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 are the last 3 characters
|
||||||
status=$(printf %s "${response#"${response%???}"}")
|
status="${response#"${response%???}"}"
|
||||||
# data is everything from response without the last 3 characters
|
# data is everything from response without the last 3 characters
|
||||||
data=$(printf %s "${response%???}")
|
data="${response%???}"
|
||||||
|
|
||||||
if [ "${status}" = 200 ]; then
|
if [ "${2}" = "raw" ]; then
|
||||||
# response OK
|
# return the raw response
|
||||||
printf %s "${data}"
|
echo "${response}"
|
||||||
elif [ "${status}" = 000 ]; then
|
else
|
||||||
# connection lost
|
# return only the data
|
||||||
echo "000"
|
if [ "${status}" = 200 ]; then
|
||||||
elif [ "${status}" = 401 ]; then
|
# response OK
|
||||||
# unauthorized
|
echo "${data}"
|
||||||
echo "401"
|
else
|
||||||
|
# connection lost
|
||||||
|
echo "${status}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,3 +249,42 @@ secretRead() {
|
||||||
# restore original terminal settings
|
# restore original terminal settings
|
||||||
stty "${stty_orig}"
|
stty "${stty_orig}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apiFunc() {
|
||||||
|
local data response status status_col
|
||||||
|
|
||||||
|
# Authenticate with the API
|
||||||
|
LoginAPI verbose
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
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 verbose
|
||||||
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ _pihole() {
|
||||||
|
|
||||||
case "${prev}" in
|
case "${prev}" in
|
||||||
"pihole")
|
"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}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
;;
|
;;
|
||||||
"allow"|"deny"|"wildcard"|"regex"|"allow-regex"|"allow-wild")
|
"allow"|"deny"|"wildcard"|"regex"|"allow-regex"|"allow-wild")
|
||||||
|
|
3
pihole
3
pihole
|
@ -484,6 +484,7 @@ Debugging Options:
|
||||||
-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
|
||||||
(regular expressions are supported)
|
(regular expressions are supported)
|
||||||
|
api <endpoint> Query the Pi-hole API at <endpoint>
|
||||||
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
@ -550,6 +551,7 @@ case "${1}" in
|
||||||
"updatechecker" ) ;;
|
"updatechecker" ) ;;
|
||||||
"arpflush" ) ;;
|
"arpflush" ) ;;
|
||||||
"-t" | "tail" ) ;;
|
"-t" | "tail" ) ;;
|
||||||
|
"api" ) need_root=0;;
|
||||||
* ) helpFunc;;
|
* ) helpFunc;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
@ -591,5 +593,6 @@ case "${1}" in
|
||||||
"updatechecker" ) shift; updateCheckFunc "$@";;
|
"updatechecker" ) shift; updateCheckFunc "$@";;
|
||||||
"arpflush" ) arpFunc "$@";;
|
"arpflush" ) arpFunc "$@";;
|
||||||
"-t" | "tail" ) tailFunc "$2";;
|
"-t" | "tail" ) tailFunc "$2";;
|
||||||
|
"api" ) apiFunc "$2";;
|
||||||
* ) helpFunc;;
|
* ) helpFunc;;
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in a new issue