diff --git a/api/endpoints/network.php b/api/endpoints/network.php index 0fa2066..63dc96a 100644 --- a/api/endpoints/network.php +++ b/api/endpoints/network.php @@ -35,41 +35,39 @@ $action = $action ?? 'status'; $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; if ($action === 'scan') { - $liveHosts = scanSubnet(LOCAL_SUBNET, 8); - $arp = getArpTable(); - $known = JarvisDB::query('SELECT * FROM network_devices'); - $knownMap = []; - foreach ($known as $d) $knownMap[$d['ip']] = $d; + // JARVIS runs on DigitalOcean — cannot reach 10.48.200.x directly. + // Scan is delegated to the PVE1 agent (jarvis-netscan.sh runs nmap locally). + // This endpoint: queues the scan command to PVE1, returns current DB state immediately. - $devices = []; - foreach ($liveHosts as $ip) { - $mac = $arp[$ip] ?? null; - $known_dev = $knownMap[$ip] ?? null; - $nsOut = shell_exec('timeout 1 nslookup ' . escapeshellarg($ip) . ' 2>/dev/null | grep "name ="'); - $hostname = null; - if ($nsOut && preg_match('/name = (.+)\./', $nsOut, $nm)) { - $hostname = rtrim($nm[1], '.'); - } - $devices[] = [ - 'ip' => $ip, - 'mac' => $mac, - 'hostname' => $hostname, - 'alias' => $known_dev['alias'] ?? null, - 'type' => $known_dev['device_type'] ?? 'unknown', - 'status' => 'online', - ]; + // Queue netscan to PVE1 agent + $pve1 = JarvisDB::single( + "SELECT agent_id FROM registered_agents WHERE ip_address='10.48.200.90' AND status='online' LIMIT 1" + ); + $queued = false; + if ($pve1) { JarvisDB::execute( - 'INSERT INTO network_devices (ip, mac, hostname, status, last_seen) VALUES (?,?,?,\'online\',NOW()) - ON DUPLICATE KEY UPDATE mac=VALUES(mac), hostname=VALUES(hostname), status=\'online\', last_seen=NOW()', - [$ip, $mac, $hostname] + "INSERT INTO agent_commands (agent_id, command_type, command_data, status) VALUES (?,?,?,?)", + [$pve1['agent_id'], 'shell', json_encode(['command'=>'/usr/local/bin/jarvis-netscan.sh','allowed'=>true]), 'pending'] ); + $queued = true; } - foreach ($knownMap as $ip => $dev) { - if (!in_array($ip, $liveHosts)) { - JarvisDB::execute('UPDATE network_devices SET status=\'offline\' WHERE ip=?', [$ip]); - } - } - echo json_encode(['devices' => $devices, 'count' => count($devices), 'scanned_at' => date('c')]); + + // Return current online devices from DB (populated by PVE1 netscan every 3 min) + $devices = JarvisDB::query( + "SELECT ip, mac, hostname, alias, device_type as type, status, last_seen + FROM network_devices WHERE status='online' AND last_seen > DATE_SUB(NOW(), INTERVAL 15 MINUTE) + ORDER BY COALESCE(alias,hostname,ip)" + ); + + echo json_encode([ + 'devices' => $devices, + 'count' => count($devices), + 'queued' => $queued, + 'scanned_at' => date('c'), + 'note' => $queued + ? 'Scan dispatched to PVE1 — results update in ~40 seconds.' + : 'Returning cached scan data. PVE1 auto-scans every 3 minutes.', + ]); } elseif ($action === 'add' && $method === 'POST') { $ip = filter_var($data['ip'] ?? '', FILTER_VALIDATE_IP); diff --git a/public_html/index.html b/public_html/index.html index 5e53939..18ef74e 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -1296,30 +1296,23 @@ function renderNetworkStatus(n) { // ── NETWORK SCAN ────────────────────────────────────────────────────── async function scanNetwork() { const btn = document.getElementById('scanBtn'); - btn.textContent = 'SCANNING...'; + btn.textContent = 'QUEUING...'; btn.disabled = true; - addMessage('jarvis', 'Initiating subnet scan on 10.48.200.0/24, Sir. This will take approximately 10 seconds.'); - speak('Initiating network scan.'); try { const data = await api('network/scan'); - if (data.devices) { - const el = document.getElementById('network-list'); - el.innerHTML = data.devices.map(d => - `