#!/bin/bash
echo "=== Havanatur API Infrastructure Probe ==="
echo "Started: $(date)"

apis=(
  "https://apihoteles.havanatursa.com/v1/"
  "https://apivuelos.havanatursa.com/v1/"
  "https://apivuelos.havanatursa.com/api/"
  "https://apiautos.havanatursa.com/"
  "https://apicasas.havanatursa.com/v1/"
  "https://apiexperiencias.havanatursa.com/v1/"
  "https://apitransfer.havanatursa.com/v1/"
  "https://apicustom.havanatursa.com/v1/"
  "https://apiseg.havanatursa.com/v1/"
  "https://api.grupocubanacan.com/v1/"
  "https://cdnturismo.havanatursa.com/"
  "https://cdncustom.havanatursa.com/"
)

for api in "${apis[@]}"; do
  echo ""
  echo "========== $api =========="

  # Headers
  curl -ksI --connect-timeout 15 -m 20 "$api" 2>/dev/null | head -15

  # Content
  echo "--- Content ---"
  curl -ks --connect-timeout 15 -m 30 "$api" 2>/dev/null | head -c 1000
  echo ""

  # Common endpoints
  base="${api%/}"
  for path in "/docs" "/swagger" "/health" "/hotels" "/destinations" "/search" "/config" "/version" "/auth/login"; do
    status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 10 -m 20 "${base}${path}" 2>/dev/null)
    code=$(echo $status | cut -d'|' -f1)
    size=$(echo $status | cut -d'|' -f2)
    ctype=$(echo $status | cut -d'|' -f3)
    if [ "$code" != "000" ]; then
      echo "  [$code] $path (${size}B) $ctype"
      if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 5000 ]; then
        curl -ks --connect-timeout 10 -m 20 "${base}${path}" 2>/dev/null | head -c 500
        echo ""
      fi
    fi
  done
done

echo ""
echo "========== tienda.peoplegoto.com =========="
curl -ksI --connect-timeout 10 -m 20 "https://tienda.peoplegoto.com/" 2>/dev/null | head -15
curl -ks --connect-timeout 10 -m 20 "https://tienda.peoplegoto.com/" 2>/dev/null | head -c 500

echo ""
echo "========== SSL Certs =========="
for domain in "apihoteles.havanatursa.com" "apivuelos.havanatursa.com" "apiseg.havanatursa.com" "api.grupocubanacan.com"; do
  echo "--- $domain ---"
  echo | openssl s_client -connect "$domain:443" -servername "$domain" 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName 2>/dev/null
done

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