#!/bin/bash
echo "=== GAESA Tourism Network Probe ==="
echo "Started: $(date)"

# Domains from SSL SAN on havanatursa.com APIs
domains=(
  "cubanacansa.com"
  "grupocubanacan.com"
  "marinasgaviotasa.com"
  "okaturs.com"
  "cislapradera.com"
  "ofertastravel.com"
)

for domain in "${domains[@]}"; do
  echo ""
  echo "========== $domain =========="

  # DNS
  echo "--- DNS ---"
  dig +short "$domain" A 2>/dev/null
  dig +short "www.$domain" A 2>/dev/null
  dig +short "$domain" MX 2>/dev/null
  dig +short "$domain" NS 2>/dev/null

  # Main site
  echo "--- www.$domain ---"
  curl -ksI --connect-timeout 10 -m 20 "https://www.$domain/" 2>/dev/null | head -15
  curl -ks --connect-timeout 10 -m 20 "https://www.$domain/" 2>/dev/null | head -c 500
  echo ""

  # API subdomains
  for sub in "api" "apihoteles" "apivuelos" "apiautos" "apitransfer" "apiexperiencias" "apicasas" "apiseg" "cdn" "admin" "mail" "webmail"; do
    status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "https://${sub}.${domain}/" 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] ${sub}.${domain} (${size}B) $ctype"
      if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
        curl -ks --connect-timeout 8 -m 15 "https://${sub}.${domain}/" 2>/dev/null | head -c 300
        echo ""
      fi
    fi
  done

  # SSL cert
  echo "--- SSL Cert ---"
  echo | openssl s_client -connect "$domain:443" -servername "$domain" 2>/dev/null | openssl x509 -noout -subject -issuer -dates -ext subjectAltName 2>/dev/null
done

# Also probe Gaviota (GAESA tourism subsidiary)
echo ""
echo "========== gaviota-grupo.com =========="
echo "--- DNS ---"
dig +short "gaviota-grupo.com" A 2>/dev/null
dig +short "www.gaviota-grupo.com" A 2>/dev/null
echo "--- Headers ---"
curl -ksI --connect-timeout 10 -m 20 "https://www.gaviota-grupo.com/" 2>/dev/null | head -15
echo "--- Content ---"
curl -ks --connect-timeout 10 -m 20 "https://www.gaviota-grupo.com/" 2>/dev/null | head -c 500
echo ""
for sub in "api" "admin" "mail" "webmail" "booking" "reservas"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}" --connect-timeout 8 -m 15 "https://${sub}.gaviota-grupo.com/" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  if [ "$code" != "000" ]; then
    echo "  [$code] ${sub}.gaviota-grupo.com"
  fi
done

# Transfer API deep dump (unauthenticated endpoints)
echo ""
echo "========== Transfer API Deep Dump =========="
base="https://apitransfer.havanatursa.com/v1"
for ep in "/province" "/municipality" "/place" "/route" "/payment" "/rol" "/help" "/province-help" "/municipality-help" "/person-help" "/rol-help" "/_" "/?"; do
  echo "--- $ep ---"
  result=$(curl -ks --connect-timeout 10 -m 30 "${base}${ep}" 2>/dev/null)
  size=${#result}
  echo "  Size: ${size} bytes"
  echo "$result" | head -c 500
  echo ""
done

# Hotels API deeper probe
echo ""
echo "========== Hotels API Deeper =========="
base="https://apihoteles.havanatursa.com"
for ep in "/v1/hotels" "/v1/hotel" "/v1/destinations" "/v1/search" "/v1/availability" "/v1/booking" "/v1/payment" "/v1/room" "/v1/rooms" "/api/" "/api/v1/" "/api/hotels" "/health" "/api/health" "/v1/help"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

# Flights API deeper
echo ""
echo "========== Flights API Deeper =========="
base="https://apivuelos.havanatursa.com"
for ep in "/api/flights" "/api/airports" "/api/search" "/api/booking" "/api/airlines" "/api/routes" "/api/help" "/v1/flights" "/v1/airports" "/v1/help"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

# Experiences API
echo ""
echo "========== Experiences API Deeper =========="
base="https://apiexperiencias.havanatursa.com"
for ep in "/v1/experiences" "/v1/events" "/v1/categories" "/v1/excursions" "/v1/help" "/v1/search" "/api/" "/api/experiences"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

# Cars API deeper
echo ""
echo "========== Cars API Deeper =========="
base="https://apiautos.havanatursa.com"
for ep in "/cars" "/vehicles" "/search" "/brands" "/models" "/locations" "/booking" "/help" "/rental" "/api/" "/api/cars"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

# Security API
echo ""
echo "========== Security API =========="
base="https://apiseg.havanatursa.com"
for ep in "/v1/users" "/v1/roles" "/v1/permissions" "/v1/register" "/v1/login" "/v1/token" "/v1/help" "/api/" "/v1/auth/register"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

# Custom API
echo ""
echo "========== Custom API =========="
base="https://apicustom.havanatursa.com"
for ep in "/v1/packages" "/v1/custom" "/v1/help" "/v1/search" "/v1/products" "/v1/offers"; do
  status=$(curl -ks -o /dev/null -w "%{http_code}|%{size_download}|%{content_type}" --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null)
  code=$(echo $status | cut -d'|' -f1)
  size=$(echo $status | cut -d'|' -f2)
  if [ "$code" != "000" ] && [ "$code" != "404" ]; then
    echo "  [$code] $ep (${size}B)"
    if [ "$code" = "200" ] && [ "$size" -gt 0 ] && [ "$size" -lt 3000 ]; then
      curl -ks --connect-timeout 8 -m 15 "${base}${ep}" 2>/dev/null | head -c 500
      echo ""
    fi
  fi
done

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