#!/usr/bin/env bash
set -u
set -o pipefail
if [[ $# -ne 1 ]]; then echo "Usage: $0 public-ip-or-fqdn" >&2; exit 2; fi
for tool in nc getent awk; do
    command -v "$tool" >/dev/null || { echo "Missing: $tool" >&2; exit 1; }
done
target=$(getent ahostsv4 "$1" | awk '!found {ip=$1; found=1} END {if(found) print ip}')
[[ -n "$target" ]] || { echo 'No IPv4 address found' >&2; exit 1; }
echo "Target IPv4: $target"
for port in 32111 43222 54333; do
    if nc -4 -z -v -w 1 "$target" "$port"; then
        echo "$port: TCP connected; router authorisation unknown"
    else
        echo "$port: timeout/refusal/local error; knock may have arrived"
    fi
    sleep 0.25
done
echo 'Sequence attempted; verify address list and WireGuard handshake.'
