<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CFX Server Info</title>
    <style>
        /* General Reset */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        /* Body and Background */
        body {
            font-family: 'Inter', sans-serif;
            background: #0f0f0f;
            color: #e0e0e0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        /* Container */
        .container {
            background: #1e1e1e;
            border-radius: 16px;
            padding: 40px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            max-width: 600px;
            width: 100%;
            text-align: center;
            animation: fadeIn 0.8s ease;
        }

        /* Typography */
        h1 {
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 16px;
            color: #ffffff;
            animation: fadeInDown 0.8s ease;
        }

        p {
            font-size: 1rem;
            color: #b0b0b0;
            margin-bottom: 24px;
            animation: fadeInUp 0.8s ease;
        }

        /* Input Field */
        input[type="text"] {
            width: 100%;
            padding: 12px 16px;
            font-size: 1rem;
            border: 2px solid #333;
            border-radius: 12px;
            background: #2a2a2a;
            color: #e0e0e0;
            outline: none;
            transition: border-color 0.3s, box-shadow 0.3s;
            animation: fadeInUp 0.8s ease;
        }

        input[type="text"]:focus {
            border-color: #007bff;
            box-shadow: 0 0 8px rgba(0, 123, 255, 0.5);
        }

        /* Button */
        button {
            padding: 12px 24px;
            font-size: 1rem;
            font-weight: 600;
            background: #007bff;
            color: #ffffff;
            border: none;
            border-radius: 12px;
            cursor: pointer;
            transition: background 0.3s, transform 0.3s;
            margin-top: 16px;
            animation: fadeInUp 0.8s ease;
        }

        button:hover {
            background: #0056b3;
            transform: translateY(-2px);
        }

        /* Output Section */
        #output {
            margin-top: 24px;
            padding: 20px;
            border-radius: 12px;
            background: #2a2a2a;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
            text-align: left;
            animation: fadeIn 1s ease;
        }

        #output h2 {
            font-size: 1.5rem;
            font-weight: 700;
            margin-bottom: 16px;
            color: #ffffff;
        }

        #output p {
            font-size: 0.95rem;
            margin-bottom: 12px;
            color: #b0b0b0;
        }

        #output p strong {
            color: #ffffff;
        }

        /* Animations */
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: scale(0.95);
            }
            to {
                opacity: 1;
                transform: scale(1);
            }
        }

        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            h1 {
                font-size: 2rem;
            }
            .container {
                padding: 24px;
            }
            input[type="text"] {
                font-size: 0.9rem;
            }
            button {
                font-size: 0.9rem;
                padding: 10px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CFX Server Info</h1>
        <p>Enter the CFX link to retrieve server details:</p>
        <input type="text" id="cfxLink" placeholder="e.g., cfx.re/join/">
        <button onclick="getServerInfo()">Get Info</button>

        <div id="output">
            <p>Server details will appear here...</p>
        </div>
    </div>

    <script>
        async function getServerInfo() {
            const input = document.getElementById('cfxLink').value.trim();
            const output = document.getElementById('output');
            output.innerHTML = `<p>Fetching server information...</p>`;
            
            if (!input.startsWith('cfx.re/join/')) {
                output.innerHTML = `<p style="color: #ff4d4d;">Invalid CFX link. Please use a valid link like "cfx.re/join/".</p>`;
                return;
            }
    
            const code = input.split('/').pop();
            const apiUrl = `https://servers-frontend.fivem.net/api/servers/single/${code}`;
    
            try {
                const response = await fetch(apiUrl, {
                    headers: {
                        "User-Agent": "Mozilla/5.0"
                    }
                });
    
                if (response.status === 404) {
                    output.innerHTML = `<p style="color: #ff4d4d;">Invalid code or server not found.</p>`;
                    return;
                }
    
                const data = await response.json();
                const serverData = data.Data;
    
                if (!serverData) {
                    output.innerHTML = `<p style="color: #ff4d4d;">Failed to retrieve server data.</p>`;
                    return;
                }
    
                let connectEndPoint = serverData.connectEndPoints[0] || "Unknown";
                const hostname = serverData.hostname || "Unknown";
                const players = serverData.players?.length || 0;
                const maxPlayers = serverData.svMaxclients || "Unknown";
                const resources = serverData.resources?.length || 0;
                const serverVars = serverData.vars || {};
                const onesync = serverVars.onesync_enabled || "Unknown";
    
                // Resolve IP or handle custom domain endpoints
                let resolvedIP = connectEndPoint;
                if (connectEndPoint.startsWith('http://') || connectEndPoint.startsWith('https://')) {
                    const domain = new URL(connectEndPoint).hostname;
                    const dnsApi = `https://dns.google/resolve?name=${domain}`;
    
                    try {
                        const dnsResponse = await fetch(dnsApi);
                        if (dnsResponse.ok) {
                            const dnsData = await dnsResponse.json();
                            resolvedIP = dnsData.Answer?.[0]?.data || "Failed to resolve domain.";
                        } else {
                            resolvedIP = "Failed to resolve domain.";
                        }
                    } catch {
                        resolvedIP = "Failed to resolve domain.";
                    }
                }
    
                // Fetch IP location using ipinfo.io with your token
                let ipLocation = "Not available";
                if (resolvedIP !== "Unknown" && resolvedIP !== "Failed to resolve domain.") {
                    const ipLookupApi = `https://ipinfo.io/${resolvedIP.split(':')[0]}/json?token=b12527edb7df4f`;
                    try {
                        const ipResponse = await fetch(ipLookupApi);
                        if (ipResponse.ok) {
                            const ipData = await ipResponse.json();
                            ipLocation = `
                                ${ipData.city}, ${ipData.region}, ${ipData.country} 
                                (ISP: ${ipData.org})
                            `;
                        } else {
                            ipLocation = "Failed to fetch IP details.";
                        }
                    } catch {
                        ipLocation = "Failed to fetch IP details.";
                    }
                }
    
                output.innerHTML = `
                    <h2>Server Information</h2>
                    <p><strong>Connect EndPoint:</strong> ${connectEndPoint}</p>
                    <p><strong>Resolved IP:</strong> ${resolvedIP}</p>
                    <p><strong>Hostname:</strong> ${hostname}</p>
                    <p><strong>Players:</strong> ${players} / ${maxPlayers}</p>
                    <p><strong>Resources:</strong> ${resources}</p>
                    <p><strong>OneSync Enabled:</strong> ${onesync}</p>
                    <p><strong>IP Location:</strong> ${ipLocation}</p>
                `;
            } catch (error) {
                console.error(error);
                output.innerHTML = `<p style="color: #ff4d4d;">An error occurred while fetching server data. Please try again.</p>`;
            }
        }
    </script>
</body>
</html>