<?php

header('Content-Type: text/plain; charset=utf-8');

$targets = [
    // Control: puertos normales web
    ['www.google.com', 443, 'HTTPS Google'],
    ['www.cloudflare.com', 443, 'HTTPS Cloudflare'],

    // SMTP Google Workspace
    ['smtp-relay.gmail.com', 587, 'Google SMTP Relay STARTTLS'],
    ['smtp-relay.gmail.com', 465, 'Google SMTP Relay SSL'],
    ['smtp.gmail.com', 587, 'Gmail SMTP STARTTLS'],
    ['smtp.gmail.com', 465, 'Gmail SMTP SSL'],
];

foreach ($targets as [$host, $port, $label]) {
    echo "====================================\n";
    echo "{$label}\n";
    echo "{$host}:{$port}\n";

    echo "DNS A:\n";
    $aRecords = dns_get_record($host, DNS_A);
    foreach ($aRecords as $record) {
        echo "- " . ($record['ip'] ?? '') . "\n";
    }

    echo "DNS AAAA:\n";
    $aaaaRecords = dns_get_record($host, DNS_AAAA);
    foreach ($aaaaRecords as $record) {
        echo "- " . ($record['ipv6'] ?? '') . "\n";
    }

    $errno = 0;
    $errstr = '';
    $start = microtime(true);
    $fp = @fsockopen($host, $port, $errno, $errstr, 10);
    $elapsed = round(microtime(true) - $start, 3);

    if ($fp) {
        echo "RESULTADO: OK conectado en {$elapsed}s\n";
        fclose($fp);
    } else {
        echo "RESULTADO: ERROR {$errno}: {$errstr} ({$elapsed}s)\n";
    }

    echo "\n";
}