<?php
header('Content-Type: application/json; charset=utf-8');

// Fungsi ambil IP publik device dari API eksternal
function getPublicIP() {
    $ch = curl_init("https://api.ipify.org?format=json");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);
    return $data['ip'] ?? 'UNKNOWN';
}

try {
    $ip = getPublicIP();
    echo json_encode([
        "success" => $ip !== 'UNKNOWN',
        "ip" => $ip
    ]);
} catch (Exception $ex) {
    echo json_encode([
        "success" => false,
        "ip" => "UNKNOWN",
        "message" => $ex->getMessage()
    ]);
}
