<?php
// 获取当前的 Unix 时间戳
$unixTimestamp = time();

// 转换为 Windows 时间戳
$currentTimestamp = ($unixTimestamp * 10000000) + 116444736000000000;

// 目标 URL
$loginUrl = 'http://60.214.107.205:20000/auth/login ';

// 用户名和密码
$username = 'dyzzyduq';
$password = 'Dj7021602';

// 获取 CSRF Token
function getCsrfToken($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        $error_msg = curl_error($ch);
        echo "cURL Error: " . $error_msg . PHP_EOL;
        return false;
    }

    preg_match('/<meta\s+name=["\']csrf-token["\']\s+content=["\'](.*?)["\']/i', $response, $matches);
    if (isset($matches[1])) {
        return $matches[1];
    } else {
        echo "未找到 CSRF Token。" . PHP_EOL;
        return false;
    }
}

// 发送登录请求
function sendLoginRequest($loginUrl, $username, $password, $csrfToken) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $loginUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

    // 设置请求头
    $headers = [
        'X-CSRF-TOKEN: ' . $csrfToken,
        'Content-Type: application/x-www-form-urlencoded',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'Referer: ' . $loginUrl,
    ];
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 设置 POST 数据
    $postData = [
        'username' => $username,
        'password' => $password,
        '_token' => $csrfToken,
        'timestamp' => time(), // 添加时间戳
    ];
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

    // 执行请求
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        $error_msg = curl_error($ch);
        echo "cURL Error: " . $error_msg . PHP_EOL;
        return;
    }

    // 输出响应内容
    echo "Response Content: ";
    echo $response;

    // 获取响应头
    curl_setopt($ch, CURLOPT_HEADER, true);
    $responseWithHeader = curl_exec($ch);
    if (curl_errno($ch)) {
        $error_msg = curl_error($ch);
        echo "cURL Error: " . $error_msg . PHP_EOL;
        return;
    }

    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($responseWithHeader, 0, $headerSize);
    $body = substr($responseWithHeader, $headerSize);

    // 输出响应头
    echo "Response Header:" . PHP_EOL;
    echo $header;

    // 检查登录是否成功
    if (strpos($response, '登录成功') !== false) {
        echo "登录成功！" . PHP_EOL;

        // 跳转到目标 URL
        header("Location: http://none.pw ");
        exit();
    } else {
        echo "登录失败，请检查用户名和密码。" . PHP_EOL;
    }

    curl_close($ch);
}

// 主函数
function main() {
    global $loginUrl, $username, $password;

    $csrfToken = getCsrfToken($loginUrl);
    if ($csrfToken === false) {
        return;
    }

    sendLoginRequest($loginUrl, $username, $password, $csrfToken);
}

main();
?>