#!/bin/bash
echo "=== api.banco-central.org Fallback Probe ==="
echo "Started: $(date)"

echo "--- DNS ---"
dig +short api.banco-central.org A 2>/dev/null
dig +short banco-central.org A 2>/dev/null
dig +short www.banco-central.org A 2>/dev/null
dig +short banco-central.org NS 2>/dev/null
dig +short banco-central.org MX 2>/dev/null

echo ""
echo "--- Headers ---"
curl -ksI --connect-timeout 15 -m 20 "https://api.banco-central.org/" 2>/dev/null | head -15

echo ""
echo "--- Content ---"
curl -ks --connect-timeout 15 -m 20 "https://api.banco-central.org/" 2>/dev/null | head -c 500

echo ""
echo "--- Endpoints ---"
for ep in "/api" "/v1" "/v1/exchange-rate" "/v1/branches" "/v1/banks" "/health" "/docs" "/v1/statistics" "/v1/news" "/v1/legal"; do
  code=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}" --connect-timeout 10 -m 15 "https://api.banco-central.org${ep}" 2>/dev/null)
  echo "  [$code] $ep"
done

echo ""
echo "--- SSL Cert ---"
echo | openssl s_client -connect "api.banco-central.org:443" -servername "api.banco-central.org" 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName 2>/dev/null

echo ""
echo "--- HTTP fallback ---"
curl -ksI --connect-timeout 10 -m 15 "http://api.banco-central.org/" 2>/dev/null | head -10

echo ""
echo "--- www site ---"
curl -ksI --connect-timeout 10 -m 15 "https://www.banco-central.org/" 2>/dev/null | head -10
curl -ks --connect-timeout 10 -m 15 "https://www.banco-central.org/" 2>/dev/null | head -c 300

echo ""
echo "--- BC Auth check ---"
curl -ks --connect-timeout 10 -m 15 "https://api.bc.gob.cu/v1/auth/login" 2>/dev/null | head -c 200
echo ""
curl -ks --connect-timeout 10 -m 15 "https://api.bc.gob.cu/v1/auth/register" 2>/dev/null | head -c 200

echo ""
echo "Completed: $(date)"
