Skip to content
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

Modify PADD to comply with the changes in FTL on the same branch #395

Merged
merged 3 commits into from
Jul 23, 2024
Merged
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
63 changes: 41 additions & 22 deletions padd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,22 @@ DeleteSession() {
}

Authenticate() {
sessionResponse="$(curl -skS -X POST "${API_URL}auth" --user-agent "PADD ${padd_version}" --data "{\"password\":\"${password}\"}" )"
sessionResponse="$(curl -skS -X POST "${API_URL}auth" --user-agent "PADD ${padd_version}" --data "{\"password\":\"${password}\"}" )"

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>]"
exit 1
fi
# 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)
# 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)
}

GetFTLData() {
local response
# get the data from querying the API as well as the http status code
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=$(printf %s "${response#"${response%???}"}")
Expand Down Expand Up @@ -390,34 +390,44 @@ GetSystemInformation() {

GetNetworkInformation() {
interfaces_raw=$(GetFTLData "network/interfaces")
gateway_raw=$(GetFTLData "network/gateway")
gateway_v4_iface=$(echo "${gateway_raw}" | jq -r '.gateway[] | select(.family == "inet") | .interface' | head -n 1)
v4_iface_data=$(echo "${interfaces_raw}" | jq --arg iface "${gateway_v4_iface}" '.interfaces[] | select(.name==$iface)' 2>/dev/null)
gateway_v6_iface=$(echo "${gateway_raw}" | jq -r '.gateway[] | select(.family == "inet6") | .interface' | head -n 1)
# Fallback: If there is no default IPv6 gateway, use the default IPv4
# gateway interface instead
if [ -z "${gateway_v6_iface}" ]; then
gateway_v6_iface="${gateway_v4_iface}"
fi
v6_iface_data=$(echo "${interfaces_raw}" | jq --arg iface "${gateway_v6_iface}" '.interfaces[] | select(.name==$iface)' 2>/dev/null)
config=$(GetFTLData "config")

# Get pi IPv4 address of the default interface
pi_ip4_addrs="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv4[]' 2>/dev/null | wc -w)"
if [ "${pi_ip4_addrs}" -eq 0 ]; then
pi_ip4_addrs="$(echo "${v4_iface_data}" | jq --raw-output '.addresses[] | select(.family=="inet") | .address' 2>/dev/null)"
if [ $(echo "${pi_ip4_addrs}" | wc -l) -eq 0 ]; then
# No IPv4 address available
pi_ip4_addr="N/A"
elif [ "${pi_ip4_addrs}" -eq 1 ]; then
elif [ $(echo "${pi_ip4_addrs}" | wc -l) -eq 1 ]; then
# One IPv4 address available
pi_ip4_addr="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv4[0]' 2>/dev/null)"
pi_ip4_addr=${pi_ip4_addrs}
else
# More than one IPv4 address available
pi_ip4_addr="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv4[0]' 2>/dev/null)+"
pi_ip4_addr="$(echo "${pi_ip4_addrs}" | head -n 1)+"
fi

# Get pi IPv6 address of the default interface
pi_ip6_addrs="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv6[]' 2>/dev/null | wc -w)"
if [ "${pi_ip6_addrs}" -eq 0 ]; then
pi_ip6_addrs="$(echo "${v6_iface_data}" | jq --raw-output '.addresses[] | select(.family=="inet6") | .address' 2>/dev/null)"
if [ $(echo "${pi_ip6_addrs}" | wc -l) -eq 0 ]; then
# No IPv6 address available
pi_ip6_addr="N/A"
ipv6_check_box=${check_box_bad}
elif [ "${pi_ip6_addrs}" -eq 1 ]; then
elif [ $(echo "${pi_ip6_addrs}" | wc -l) -eq 1 ]; then
# One IPv6 address available
pi_ip6_addr="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv6[0]' 2>/dev/null | cut -f1 -d "%" )"
pi_ip6_addr=${pi_ip6_addrs}
ipv6_check_box=${check_box_good}
else
# More than one IPv6 address available
pi_ip6_addr="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .ipv6[0]' 2>/dev/null | cut -f1 -d "%" )+"
pi_ip6_addr="$(echo "${pi_ip6_addrs}" | head -n 1)+"
ipv6_check_box=${check_box_good}
fi

Expand Down Expand Up @@ -449,7 +459,8 @@ GetNetworkInformation() {
dhcp_heatmap=${red_text}
dhcp_check_box=${check_box_bad}

GATEWAY="$(GetFTLData "network/gateway" | jq --raw-output .address 2>/dev/null)"
# Display the gateway address if DHCP is disabled
GATEWAY="$(echo "${gateway_raw}" | jq -r '.gateway[] | select(.family == "inet") | .address' | head -n 1)"
dhcp_info=" Router: ${GATEWAY}"
dhcp_ipv6_status="N/A"
dhcp_ipv6_heatmap=${yellow_text}
Expand Down Expand Up @@ -501,15 +512,23 @@ GetNetworkInformation() {
conditional_forwarding_heatmap=${red_text}
fi

# Default interface data
iface_name="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .name' 2>/dev/null)"
tx_bytes="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .tx.num' 2>/dev/null)"
tx_bytes_unit="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .tx.unit' 2>/dev/null)"
# Default interface data (use IPv4 interface - we cannot show both and assume they are the same)
iface_name="${gateway_v4_iface}"
tx_bytes="$(echo "${v4_iface_data}" | jq --raw-output '.stats.tx_bytes.value' 2>/dev/null)"
tx_bytes_unit="$(echo "${v4_iface_data}" | jq --raw-output '.stats.tx_bytes.unit' 2>/dev/null)"
tx_bytes=$(printf "%.1f %b" "${tx_bytes}" "${tx_bytes_unit}")

rx_bytes="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .rx.num' 2>/dev/null)"
rx_bytes_unit="$(echo "${interfaces_raw}" | jq --raw-output '.interfaces[] | select(.default==true) | .rx.unit' 2>/dev/null)"
rx_bytes="$(echo "${v4_iface_data}" | jq --raw-output '.stats.rx_bytes.value' 2>/dev/null)"
rx_bytes_unit="$(echo "${v4_iface_data}" | jq --raw-output '.stats.rx_bytes.unit' 2>/dev/null)"
rx_bytes=$(printf "%.1f %b" "${rx_bytes}" "${rx_bytes_unit}")

# If IPv4 and IPv6 interfaces are not the same, add a "*" to the interface
# name to highlight that there are two different interfaces and the
# displayed statistics are only for the IPv4 interface, while the IPv6
# address correctly corresponds to the default IPv6 interface
if [ ! "${gateway_v4_iface}" = "${gateway_v6_iface}" ]; then
iface_name="${iface_name}*"
fi
}

GetPiholeInformation() {
Expand Down
Loading