mirror of
https://github.com/pi-hole/pi-hole.git
synced 2024-11-25 07:33:43 +00:00
Iterate over ports, skip redirected ports
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
a6565bf9a1
commit
974fea592d
1 changed files with 43 additions and 21 deletions
|
@ -25,26 +25,48 @@ TestAPIAvailability() {
|
||||||
ports="$(pihole-FTL --config webserver.port)"
|
ports="$(pihole-FTL --config webserver.port)"
|
||||||
port="${ports%%,*}"
|
port="${ports%%,*}"
|
||||||
|
|
||||||
# if the port ends with an "s", it is a secure connection
|
# Iterate over comma separated list of ports
|
||||||
if [ "${port#"${port%?}"}" = "s" ]; then
|
while [ "${port}" != "${ports}" ]; do
|
||||||
# remove the "s" from the port
|
# if the port ends with an "s", it is a secure connection
|
||||||
API_PROT="https"
|
if [ "${port#"${port%?}"}" = "s" ]; then
|
||||||
API_PORT="${port%?}"
|
# remove the "s" from the port
|
||||||
elif [ "${port#"${port%?}"}" = "r" ]; then
|
API_PROT="https"
|
||||||
# if the port ends in "r", it is a redirect
|
API_PORT="${port%?}"
|
||||||
API_PROT="http"
|
elif [ "${port#"${port%?}"}" = "r" ]; then
|
||||||
# remove the "r" from the port
|
# Ignore this port
|
||||||
API_PORT="${port%?}"
|
API_PORT="0"
|
||||||
else
|
else
|
||||||
API_PROT="http"
|
API_PROT="http"
|
||||||
API_PORT="${port}"
|
API_PORT="${port}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
API_URL="${API_PROT}://localhost:${API_PORT}/api"
|
if [ ! "${API_PORT}" = "0" ]; then
|
||||||
availabilityResonse=$(curl -skSL -o /dev/null -w "%{http_code}" "${API_URL}/auth")
|
# If the port is of form "ip:port", we need to remove everything before
|
||||||
|
# the last ":" in the string, e.g., "[::]:80" -> "80"
|
||||||
|
if [ "${API_PORT#*:}" != "${API_PORT}" ]; then
|
||||||
|
API_PORT="${API_PORT##*:}"
|
||||||
|
fi
|
||||||
|
|
||||||
# test if http status code was 200 (OK), 308 (redirect, we follow) 401 (authentication required)
|
API_URL="${API_PROT}://localhost:${API_PORT}/api"
|
||||||
if [ ! "${availabilityResonse}" = 200 ] && [ ! "${availabilityResonse}" = 308 ] && [ ! "${availabilityResonse}" = 401 ]; then
|
availabilityResonse=$(curl -skS -o /dev/null -w "%{http_code}" "${API_URL}/auth")
|
||||||
|
|
||||||
|
# test if http status code was 200 (OK), 308 (redirect, we follow) 401 (authentication required)
|
||||||
|
if [ ! "${availabilityResonse}" = 200 ] && [ ! "${availabilityResonse}" = 308 ] && [ ! "${availabilityResonse}" = 401 ]; then
|
||||||
|
API_PORT="0"
|
||||||
|
else
|
||||||
|
# API is available at this port/protocol combination
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# remove the first port from the list
|
||||||
|
ports="${ports#*,}"
|
||||||
|
# get the next port
|
||||||
|
port="${ports%%,*}"
|
||||||
|
done
|
||||||
|
|
||||||
|
# if API_PORT is 0, no working API port was found
|
||||||
|
if [ "${API_PORT}" = "0" ]; then
|
||||||
echo "API not available at: ${API_URL}"
|
echo "API not available at: ${API_URL}"
|
||||||
echo "Exiting."
|
echo "Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -71,7 +93,7 @@ Authenthication() {
|
||||||
}
|
}
|
||||||
|
|
||||||
LoginAPI() {
|
LoginAPI() {
|
||||||
sessionResponse="$(curl -skSL -X POST "${API_URL}/auth" --user-agent "Pi-hole cli " --data "{\"password\":\"${password}\"}" )"
|
sessionResponse="$(curl -skS -X POST "${API_URL}/auth" --user-agent "Pi-hole cli " --data "{\"password\":\"${password}\"}" )"
|
||||||
|
|
||||||
if [ -z "${sessionResponse}" ]; then
|
if [ -z "${sessionResponse}" ]; then
|
||||||
echo "No response from FTL server. Please check connectivity"
|
echo "No response from FTL server. Please check connectivity"
|
||||||
|
@ -87,7 +109,7 @@ DeleteSession() {
|
||||||
# SID is not null (successful authenthication only), delete the session
|
# SID is not null (successful authenthication only), delete the session
|
||||||
if [ "${validSession}" = true ] && [ ! "${SID}" = null ]; then
|
if [ "${validSession}" = true ] && [ ! "${SID}" = null ]; then
|
||||||
# Try to delete the session. Omit the output, but get the http status code
|
# Try to delete the session. Omit the output, but get the http status code
|
||||||
deleteResponse=$(curl -skSL -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
|
||||||
"200") printf "%b" "A session that was not created cannot be deleted (e.g., empty API password).\n";;
|
"200") printf "%b" "A session that was not created cannot be deleted (e.g., empty API password).\n";;
|
||||||
|
@ -101,7 +123,7 @@ DeleteSession() {
|
||||||
GetFTLData() {
|
GetFTLData() {
|
||||||
local data response status
|
local data response status
|
||||||
# get the data from querying the API as well as the http status code
|
# get the data from querying the API as well as the http status code
|
||||||
response=$(curl -skSL -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=$(printf %s "${response#"${response%???}"}")
|
||||||
|
|
Loading…
Reference in a new issue