<?php
// 开启错误显示（调试用）
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 开启输出缓冲
ob_start();

// 配置域名池
$domains = [
    '8cag.com',
    '6cag.cc'
];

// 随机选择主域名
$targetDomain = $domains[array_rand($domains)];

// 生成8位随机字母和数字的二级前缀
function generateSubdomain() {
    return substr(str_shuffle('abcdefghijklmnopqrstuvwxyz123456789'), 0, 8);
}

// 生成随机二级域名
$randomSubdomain = generateSubdomain();

// 构建目标URL
$redirectUrl = 'https://' . $randomSubdomain . '.' . $targetDomain;

// 调试信息（仅在未点击时显示）
if (!isset($_GET['action']) || $_GET['action'] !== 'redirect') {
    // 显示调试信息
    echo "<!-- 调试信息 -->\n";
    echo "PHP版本: " . phpversion() . "\n";
    echo "目标URL: " . $redirectUrl . "\n";
    echo "当前脚本: " . __FILE__ . "\n";
    echo "服务器: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
}

// 检测是否有按钮点击参数
if (isset($_GET['action']) && $_GET['action'] === 'redirect') {
    // 清空输出缓冲
    ob_clean();
    
    // 检查headers是否已发送
    if (headers_sent($file, $line)) {
        die("Headers already sent in $file on line $line");
    }
    
    // 执行301跳转
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $redirectUrl");
    header("Cache-Control: no-cache, must-revalidate");
    exit;
}

// 如果没有点击按钮，显示HTML页面
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能路由 · 极速跳转</title>
    <!-- 引入 Font Awesome 图标库 (免费) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        /* ... 你的样式保持不变 ... */
        /* 为了简洁，样式省略，但你可以复制之前的美化样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: radial-gradient(circle at 10% 20%, rgb(0, 0, 0) 0%, rgb(64, 64, 64) 90.2%);
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
            padding: 24px;
            margin: 0;
            position: relative;
        }
        .container {
            position: relative;
            z-index: 1;
            background: rgba(18, 18, 24, 0.75);
            backdrop-filter: blur(18px) saturate(180%);
            -webkit-backdrop-filter: blur(18px) saturate(180%);
            border-radius: 48px;
            padding: 56px 48px 44px;
            max-width: 540px;
            width: 100%;
            box-shadow: 0 40px 80px -12px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(255, 255, 255, 0.06) inset;
            border: 1px solid rgba(255, 255, 255, 0.04);
            transition: transform 0.25s ease, box-shadow 0.4s ease;
            text-align: center;
        }
        /* ... 更多样式 ... */
        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 14px;
            width: 100%;
            padding: 18px 24px;
            background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
            color: #0f0f13;
            font-size: 19px;
            font-weight: 700;
            letter-spacing: 0.3px;
            border: none;
            border-radius: 60px;
            cursor: pointer;
            text-decoration: none;
            box-shadow: 0 8px 32px -4px rgba(251, 191, 36, 0.3), 0 0 0 1px rgba(251, 191, 36, 0.2) inset;
            transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
            position: relative;
            overflow: hidden;
        }
        /* 调试信息样式 */
        .debug-info {
            margin-top: 20px;
            padding: 12px;
            background: rgba(255, 0, 0, 0.1);
            border: 1px solid rgba(255, 0, 0, 0.2);
            border-radius: 8px;
            color: #ff6b6b;
            font-size: 12px;
            text-align: left;
            font-family: monospace;
            display: none;
        }
        .debug-info.show {
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 图标 -->
        <div class="icon-wrapper">
            <i class="fas fa-bolt"></i>
        </div>

        <h1>智能路由</h1>
        <p class="subtitle">点击下方按钮，立即跳转至动态生成的二级域名</p>

        <!-- 信息卡片 -->
        <div class="info-box">
            <div class="label"><i class="fas fa-database" style="margin-right:6px;"></i> 域名池</div>
            <div class="domain-pool">
                <span><?php echo htmlspecialchars($domains[0]); ?></span>
                <span><?php echo htmlspecialchars($domains[1]); ?></span>
            </div>

            <div class="divider-light"></div>

            <div class="info-row">
                <span class="label-sm"><i class="far fa-keyboard" style="margin-right:6px;"></i> 随机子域</span>
                <span class="value-sm"><?php echo htmlspecialchars($randomSubdomain); ?></span>
            </div>

            <div class="full-url">
                <i class="fas fa-arrow-right"></i> <?php echo htmlspecialchars($redirectUrl); ?>
            </div>
        </div>

        <!-- 主按钮 (人工点击) -->
        <a href="?action=redirect" class="btn">
            <span class="btn-text">立即进入</span>
            <i class="fas fa-arrow-right"></i>
        </a>

        <div class="footer">
            <i class="fas fa-shield-alt"></i> 点击即代表您将访问随机生成的二级域名 <i class="fas fa-shield-alt"></i>
        </div>

        <!-- 调试信息（默认隐藏，按需显示） -->
        <div class="debug-info show">
            <strong>🔍 调试信息：</strong><br>
            PHP版本: <?php echo phpversion(); ?><br>
            服务器: <?php echo $_SERVER['SERVER_SOFTWARE']; ?><br>
            当前文件: <?php echo __FILE__; ?><br>
            目标URL: <?php echo htmlspecialchars($redirectUrl); ?><br>
            <?php if (headers_sent($file, $line)): ?>
            ⚠️ Headers已在 <?php echo $file; ?> 第<?php echo $line; ?>行发送
            <?php else: ?>
            ✅ Headers未发送，可以正常跳转
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
<?php
// 结束输出缓冲
ob_end_flush();
?>