<?php
// فایل debug_list.php
// اطلاعات دقیق پنل را وارد کنید
$panel_url = "https://netfreedomvpn.ram.villux.ir/ram";
$username = "XwVfSkmLlq"; // یوزرنیم واقعی
$password = "yIbwYvBSKevLlGYnhq"; // پسورد واقعی

echo "<h2>Testing Inbound List Fetch</h2>";

// 1. لاگین
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$panel_url/login");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(["username" => $username, "password" => $password]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// شبیه‌سازی مرورگر (خیلی مهم برای 3x-ui)
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// استخراج کوکی
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$cookies = [];
foreach ($matches[1] as $item) {
    parse_str($item, $c);
    $cookies = array_merge($cookies, $c);
}
$cookieStr = "";
foreach ($cookies as $k => $v) {
    $cookieStr .= "$k=$v; ";
}

echo "Login Status: <b>$http_code</b><br>";
echo "Cookie: <textarea style='width:100%;height:50px'>$cookieStr</textarea><hr>";

if (empty($cookieStr)) die("❌ لاگین ناموفق بود. کوکی دریافت نشد.");

// 2. دریافت لیست (تست مسیرهای مختلف)
$paths = [
    "/panel/inbound/list",
    "/xui/inbound/list"
];

foreach ($paths as $path) {
    $full_url = $panel_url . $path;
    echo "<h3>Testing URL: $full_url</h3>";

    curl_setopt($ch, CURLOPT_URL, $full_url);
    curl_setopt($ch, CURLOPT_POST, true); // پنل‌های جدید POST میخواهند
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{}"); // بادی خالی
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Cookie: $cookieStr",
        "Accept: application/json",
        "Content-Type: application/json",
        "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    ]);

    $list_res = curl_exec($ch);
    $list_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    echo "HTTP Code: <b>$list_code</b><br>";
    echo "Response Length: " . strlen($list_res) . "<br>";
    echo "Response Body: <pre>" . htmlspecialchars(substr($list_res, 0, 500)) . "...</pre>";

    $json = json_decode($list_res);
    if (is_object($json) && isset($json->success)) {
        echo "<h3 style='color:green'>✅ موفقیت آمیز! مسیر درست این است: $path</h3>";
    } else {
        echo "<span style='color:red'>❌ شکست</span>";
    }
    echo "<hr>";
}
curl_close($ch);
