homeassistant-addon-repository/wireguard-client/rootfs/etc/services.d/api/run

49 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2023-10-27 11:30:54 +00:00
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Home Assistant Third Party Add-on: WireGuard Client
# Provides status of WireGuard broker.
# ==============================================================================
declare -a peers
declare endpoint
declare json
declare latest_handshake
declare line
declare peer
declare transfer_rx
declare transfer_tx
PORT=$(bashio::addon.port "80/tcp")
if [[ $PORT -eq 0 ]]; then exit; fi
while true; do
# Get information from wg
peers=()
declare count=1
while IFS=$'\t' read -r -a line; do
if [[ "${#line[@]}" -gt 6 ]]; then
endpoint="${line[3]}"
latest_handshake="$(exec date -d @"${line[5]}" 2>/dev/null || date -r "${line[5]}")"
transfer_rx="${line[6]}"
transfer_tx="${line[7]}"
peer=$(bashio::var.json \
'endpoint' "${endpoint}" \
'latest_handshake' "${latest_handshake}" \
'transfer_rx' "^${transfer_rx}" \
'transfer_tx' "^${transfer_tx}")
peers+=("peer_${count}" "^${peer}")
(( count++ ))
fi
done <<< "$(wg show all dump)"
# Build final json content
json="{}"
if [[ "${#peers[@]}" -ne 0 ]]; then
json=$(bashio::var.json "${peers[@]}")
fi
echo -e "HTTP/1.1 200 OK\r\nContent-type: application/json\r\n\r\n${json}" \
| nc -l -p $PORT > /dev/null
done