<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>加载中</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
</head>
<body>
    <div style="margin-top: 10%; text-align: center;">
        <p id="loading-text">正在加载中。。。</p>
        <img src="load.gif"/>
    </div>
</body>

</html>

<?php
function checkUrlAvailability($url) {
    $options = [
        'http' => [
            'method' => 'HEAD',
            'timeout' => 3,
            'follow_location' => 0, // 不跟随重定向
            'ignore_errors' => true
        ],
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false
        ]
    ];
    
    $context = stream_context_create($options);
    
    try {
        $response = @file_get_contents($url, false, $context, 0, 1);
        if ($response === false) {
            return false;
        }
        
        // 从响应头中获取状态码
        if (isset($http_response_header)) {
            foreach ($http_response_header as $header) {
                if (preg_match('/HTTP\/\d\.\d\s+(\d+)/', $header, $matches)) {
                    $statusCode = (int)$matches[1];
                    return ($statusCode >= 200 && $statusCode < 400);
                }
            }
        }
        return false;
    } catch (Exception $e) {
        return false;
    }
}

$originUrls = [

    '2pem.8238.btxwp6.vip:3389',
    '2dxr.sph7.btxwp6.vip:3389',
    '1o93.y2rw.btxwp6.vip:3389',
    '2tlzz.rgd.btxwp6.vip:3389',
    'm78.zv3zw.btxwp6.vip:3389',
    'gox6.wtay.btxwp6.vip:3389',
    'oml.y9g7d.btxwp6.vip:3389',
    'qe06.xlcs.btxwp6.vip:3389',
    'dcm8.wxtv.btxwp6.vip:3389',
    '8fln1.mwz.btxwp6.vip:3389',
];

$maxAttempts = 10; // 减少尝试次数以避免长时间等待
$availableUrl = null;

// 先打乱数组顺序，实现随机尝试
shuffle($originUrls);

foreach ($originUrls as $urlPart) {
    $url = 'https://' . $urlPart;
    if (checkUrlAvailability($url)) {
        $availableUrl = $url;
        break;
    }
    // 更新页面显示
    echo '<script>document.getElementById("loading-text").innerHTML = "正在尝试线路: ' . htmlspecialchars($urlPart) . '";</script>';
    ob_flush();
    flush();
    usleep(500000); // 延迟0.5秒
}

if ($availableUrl) {
    // 使用JavaScript进行跳转，避免header已发送的问题
    echo '<script>window.location.href = "' . htmlspecialchars($availableUrl) . '";</script>';
    exit;
} else {
    echo '<script>document.getElementById("loading-text").innerHTML = "无法找到可用的链接，请访问6148.com";</script>';
    exit;
}
?>