Skip to content

Allow specification of the exact API URL via new option --api #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions padd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,34 @@ TestAPIAvailability() {

local chaos_api_list authResponse cmdResult digReturnCode authStatus authData apiAvailable

# Query the API URLs from FTL using CHAOS TXT
# The result is a space-separated enumeration of full URLs
# e.g., "http://localhost:80/api" or "https://domain.com:443/api"
if [ -z "${SERVER}" ] || [ "${SERVER}" = "localhost" ] || [ "${SERVER}" = "127.0.0.1" ]; then
# --server was not set or set to local, assuming we're running locally
cmdResult="$(dig +short chaos txt local.api.ftl @localhost 2>&1; echo $?)"
# Check if an API location was specified with --api
if [ -n "${API_LOCATION}" ]; then
# The list of available API URLs is just the provided URL
chaos_api_list="${API_LOCATION}"
else
# --server was set, try to get response from there
cmdResult="$(dig +short chaos txt domain.api.ftl @"${SERVER}" 2>&1; echo $?)"
fi
# Query the API URLs from FTL using CHAOS TXT
# The result is a space-separated enumeration of full URLs
# e.g., "http://localhost:80/api" or "https://domain.com:443/api"
if [ -z "${SERVER}" ] || [ "${SERVER}" = "localhost" ] || [ "${SERVER}" = "127.0.0.1" ]; then
# --server was not set or set to local, assuming we're running locally
cmdResult="$(dig +short chaos txt local.api.ftl @localhost 2>&1; echo $?)"
else
# --server was set, try to get response from there
cmdResult="$(dig +short chaos txt domain.api.ftl @"${SERVER}" 2>&1; echo $?)"
fi

# Gets the return code of the dig command (last line)
# We can't use${cmdResult##*$'\n'*} here as $'..' is not POSIX
digReturnCode="$(echo "${cmdResult}" | tail -n 1)"
# Gets the return code of the dig command (last line)
# We can't use${cmdResult##*$'\n'*} here as $'..' is not POSIX
digReturnCode="$(echo "${cmdResult}" | tail -n 1)"

if [ ! "${digReturnCode}" = "0" ]; then
# If the query was not successful
moveXOffset; echo "API not available. Please check server address and connectivity"
exit 1
else
# Dig returned 0 (success), so get the actual response (first line)
chaos_api_list="$(echo "${cmdResult}" | head -n 1)"
if [ ! "${digReturnCode}" = "0" ]; then
# If the query was not successful
moveXOffset; echo "API not available. Please check server address and connectivity"
exit 1
else
# Dig returned 0 (success), so get the actual response (first line)
chaos_api_list="$(echo "${cmdResult}" | head -n 1)"
fi
fi

# Iterate over space-separated list of URLs
Expand Down Expand Up @@ -291,7 +297,7 @@ Authenticate() {

if [ -z "${sessionResponse}" ]; then
moveXOffset; echo "No response from FTL server. Please check connectivity and use the options to set the API URL"
moveXOffset; echo "Usage: $0 [--server <domain|IP>]"
moveXOffset; echo "Usage: $0 [--server <domain|IP>] or [--api <API URL>]"
exit 1
fi
# obtain validity, session ID and sessionMessage from session response
Expand Down Expand Up @@ -1817,6 +1823,7 @@ DisplayHelp() {
::: --yoff [num] set the y-offset, reference is the upper left corner, disables auto-centering
:::
::: --server <DOMAIN|IP> domain or IP of your Pi-hole (default: localhost)
::: --api <API URL> API URL location of your Pi-hole (example: https://pi.hole/api/)
::: --secret <password> your Pi-hole's password, required to access the API
::: --2fa <2fa> your Pi-hole's 2FA code, if 2FA is enabled
::: --runonce display output once and exit
Expand Down Expand Up @@ -1921,13 +1928,20 @@ while [ "$#" -gt 0 ]; do
"--xoff" ) xOffset="$2"; xOffOrig="$2"; shift;;
"--yoff" ) yOffset="$2"; yOffOrig="$2"; shift;;
"--server" ) SERVER="$2"; shift;;
"--api" ) API_LOCATION="$2"; shift;;
"--secret" ) password="$2"; shift;;
"--2fa" ) totp="$2"; shift;;
* ) DisplayHelp; exit 1;;
esac
shift
done

if [ -n "$API_LOCATION" ] && [ -n "$SERVER" ]; then
moveXOffset; echo "Do not set --server and --api simultaneously."
moveXOffset; echo "Exiting."
exit 1
fi

if [ "${versionOnly}" ]; then
ShowVersion
fi
Expand Down