#!/bin/bash
# Phase 0: Comprehensive .edu.cu domain discovery
# Uses crt.sh, DNS brute, and zone checks

OUTDIR="/tmp/uni-probe"
mkdir -p "$OUTDIR"

echo "=== CUBAN EDUCATION DOMAIN DISCOVERY ==="
echo "Date: $(date -u)"

# 1. Pull ALL .edu.cu certs from crt.sh
echo ""
echo "=== CRT.SH: %.edu.cu ==="
curl -s "https://crt.sh/?q=%25.edu.cu&output=json" 2>/dev/null | python3 -c "
import json,sys
try:
    data = json.load(sys.stdin)
    domains = set()
    for entry in data:
        name = entry.get('common_name','').strip().lower()
        if name and '.edu.cu' in name:
            # Handle wildcards
            name = name.replace('*.','')
            domains.add(name)
        # Also check name_value for SANs
        nv = entry.get('name_value','').strip().lower()
        for n in nv.split('\n'):
            n = n.strip().replace('*.','')
            if n and '.edu.cu' in n:
                domains.add(n)
    for d in sorted(domains):
        print(d)
except:
    print('ERROR: Failed to parse crt.sh response', file=sys.stderr)
" > "$OUTDIR/crtsh-edu-cu.txt" 2>/dev/null

echo "crt.sh domains found: $(wc -l < "$OUTDIR/crtsh-edu-cu.txt")"
cat "$OUTDIR/crtsh-edu-cu.txt"

# 2. Also check .cu educational domains (uci.cu, uh.cu, umcc.cu, etc.)
echo ""
echo "=== CRT.SH: Other .cu educational ==="
for pattern in "%.uci.cu" "%.uh.cu" "%.umcc.cu" "%.unica.cu" "%.cujae.edu.cu" "%.mes.gob.cu" "%.reduc.edu.cu" "%.sld.cu" "%.co.cu"; do
  curl -s "https://crt.sh/?q=${pattern}&output=json" 2>/dev/null | python3 -c "
import json,sys
try:
    data = json.load(sys.stdin)
    domains = set()
    for entry in data:
        for field in ['common_name','name_value']:
            for n in entry.get(field,'').strip().lower().split('\n'):
                n = n.strip().replace('*.','')
                if n and len(n) > 3:
                    domains.add(n)
    for d in sorted(domains):
        print(d)
except:
    pass
" 2>/dev/null
done | sort -u > "$OUTDIR/crtsh-other-cu.txt"

echo "Other .cu domains found: $(wc -l < "$OUTDIR/crtsh-other-cu.txt")"

# 3. Merge and dedupe all domains
cat "$OUTDIR/crtsh-edu-cu.txt" "$OUTDIR/crtsh-other-cu.txt" 2>/dev/null | sort -u > "$OUTDIR/all-cu-education.txt"
echo ""
echo "=== TOTAL UNIQUE DOMAINS: $(wc -l < "$OUTDIR/all-cu-education.txt") ==="
echo ""

# 4. Quick connectivity check on all domains
echo "=== CONNECTIVITY CHECK ==="
while read domain; do
  code=$(curl -sk -o /dev/null -w "%{http_code}" --connect-timeout 4 --max-time 7 "https://$domain/" 2>/dev/null)
  if [ "$code" = "000" ]; then
    code=$(curl -sk -o /dev/null -w "%{http_code}" --connect-timeout 4 --max-time 7 "http://$domain/" 2>/dev/null)
    proto="http"
  else
    proto="https"
  fi
  if [ "$code" != "000" ]; then
    echo "[LIVE] $domain ($proto, HTTP $code)"
    echo "$domain" >> "$OUTDIR/live-edu-domains.txt"
  fi
done < "$OUTDIR/all-cu-education.txt"

echo ""
echo "=== LIVE DOMAINS: $(wc -l < "$OUTDIR/live-edu-domains.txt" 2>/dev/null || echo 0) ==="
cat "$OUTDIR/live-edu-domains.txt" 2>/dev/null
