#!/bin/bash
export LC_ALL=C
echo "===== DoH ENDPOINT REACHABILITY ====="
try() {
  printf '  %-42s -> ' "$1"
  shift
  out=$(curl -sS -o /tmp/doh_body -w '%{http_code} %{time_total}s' --max-time 12 "$@" 2>&1)
  echo "$out | body: $(head -c 120 /tmp/doh_body 2>/dev/null | tr -d '\n')"
}

try "cloudflare-dns.com @1.1.1.1" --resolve cloudflare-dns.com:443:1.1.1.1 -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=lucasjow.us.ci&type=HTTPS'
try "cloudflare-dns.com @104.16.248.249" --resolve cloudflare-dns.com:443:104.16.248.249 -H 'accept: application/dns-json' 'https://cloudflare-dns.com/dns-query?name=lucasjow.us.ci&type=HTTPS'
try "dns.google @8.8.8.8" --resolve dns.google:443:8.8.8.8 'https://dns.google/resolve?name=lucasjow.us.ci&type=HTTPS'
try "dns.google @8.8.4.4" --resolve dns.google:443:8.8.4.4 'https://dns.google/resolve?name=lucasjow.us.ci&type=HTTPS'
try "dns.quad9.net @9.9.9.9" --resolve dns.quad9.net:443:9.9.9.9 -H 'accept: application/dns-json' 'https://dns.quad9.net:5053/dns-query?name=lucasjow.us.ci&type=HTTPS'

echo
echo "===== PLAIN DNS UDP (may be intercepted) ====="
command -v dig >/dev/null && dig +short +time=3 +tries=1 @1.1.1.1 HTTPS lucasjow.us.ci || echo "  dig not installed"
command -v nslookup >/dev/null && nslookup -type=HTTPS lucasjow.us.ci 1.1.1.1 2>&1 | head -6 || true
