#!/bin/bash
set -euo pipefail

# Show output live and save the full run.
exec > >(tee dnstest.txt) 2>&1

# Find Homebrew if installed but missing from PATH.
if ! command -v brew >/dev/null 2>&1; then
  for brew_path in /opt/homebrew/bin/brew /usr/local/bin/brew; do
    if [[ -x "$brew_path" ]]; then
      eval "$("$brew_path" shellenv)"
      break
    fi
  done
fi

# Install dnspyre only if missing.
if command -v dnspyre >/dev/null 2>&1; then
  echo "dnspyre already installed."
else
  if ! command -v brew >/dev/null 2>&1; then
    echo "Installing Homebrew; may request your Mac password."
    installer=$(mktemp)
    curl -fsSL \
      https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh \
      -o "$installer"
    /bin/bash "$installer"
    rm -f "$installer"

    for brew_path in /opt/homebrew/bin/brew /usr/local/bin/brew; do
      if [[ -x "$brew_path" ]]; then
        eval "$("$brew_path" shellenv)"
        break
      fi
    done
  fi

  echo "Installing dnspyre."
  brew install dnspyre
fi

command -v dnspyre >/dev/null 2>&1 || {
  echo "dnspyre installation failed."
  exit 1
}
dnspyre --version

# Download Majestic's ranking and extract 100 domains.
curl -fsSL https://downloads.majestic.com/majestic_million.csv \
  -o majestic_million.csv

awk -F, 'NR >= 2 && NR <= 101 {
  gsub(/"/, "", $3); print $3
}' majestic_million.csv > domains100.txt

[[ $(wc -l < domains100.txt) -eq 100 ]] || {
  echo "Need exactly 100 domains."
  exit 1
}

mkdir -p dns-results

echo "Domains used:"
cat domains100.txt

# Exactly 100 queries per server/protocol, with no repetitions.
common=(
  --type A
  --number 1
  --concurrency 1
  --rate-limit 100
  --connect 1s
  --read 1s
  --write 1s
  --request 1s
  --no-progress
  --no-color
)

test_server() {
  local server="$1"
  local skip_certificate_check="$2"
  local protocol report
  local args=()

  for protocol in UDP TCP DoT DoH DoQ; do
    case "$protocol" in
      UDP) args=(--server "$server:53") ;;
      TCP) args=(--server "$server:53" --tcp) ;;
      DoT) args=(--server "$server:853" --dot) ;;
      DoH) args=(--server "https://$server:443/dns-query"
                 --doh-protocol 2) ;;
      DoQ) args=(--server "quic://$server:853") ;;
    esac

    if [[ "$skip_certificate_check" == "yes" ]]; then
      case "$protocol" in
        DoT|DoH|DoQ) args+=(--insecure) ;;
      esac
    fi

    echo
    echo "===== Testing $server $protocol ====="
    report="dns-results/$server-$protocol.txt"

    if dnspyre "${common[@]}" "${args[@]}" @domains100.txt \
      > "$report" 2>&1; then
      cat "$report"
    else
      cat "$report"
      echo "Test failed: $server $protocol; continuing."
    fi
  done
}

# Test all nine IP addresses.
for suffix in 114 115 116 117 118 119 124 125 126; do
  test_server "203.190.10.$suffix" yes
done

# Test hostnames with TLS certificate validation enabled.
for server in high.kahfguard.com medium.kahfguard.com low.kahfguard.com; do
  test_server "$server" no
done

echo
echo "Finished. Full output: dnstest.txt"
echo "Individual reports: dns-results/"

