#!/bin/bash
set -euo pipefail

/usr/bin/wg show wg0 dump | tail -n +2 | while IFS=$'\t' read -r public_key preshared endpoint allowed_ips latest_handshake received sent keepalive; do
    [[ -z "$public_key" ]] && continue
    if [[ "$endpoint" == "(none)" ]]; then endpoint=""; fi
    printf '{"public_key":"%s","latest_handshake_at":%s,"endpoint":"%s","received_bytes":%s,"sent_bytes":%s}\n' \
        "$public_key" \
        "${latest_handshake:-0}" \
        "$endpoint" \
        "${received:-0}" \
        "${sent:-0}"
done
