Files

85 lines
3.1 KiB
PHP

<?php
// Network scan push endpoint — called by PVE1 cron with nmap results
// Authenticates via X-Registration-Key header (same key as agent installer)
define('NETSCAN_KEY', 'f846a9aaf7ce9a61742c63c87c4186052a71d2a580c65518');
if ($method !== 'POST') {
echo json_encode(['error' => 'POST only']); exit;
}
$reqKey = $_SERVER['HTTP_X_REGISTRATION_KEY'] ?? '';
if ($reqKey !== NETSCAN_KEY) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']); exit;
}
$body = file_get_contents('php://input');
$payload = json_decode($body, true);
$devices = $payload['devices'] ?? [];
if (empty($devices)) {
echo json_encode(['error' => 'No devices in payload']); exit;
}
$discoveredIPs = [];
$upserted = 0;
foreach ($devices as $d) {
$ip = trim($d['ip'] ?? '');
$mac = trim($d['mac'] ?? '');
$hostname = trim($d['hostname'] ?? '');
$vendor = trim($d['vendor'] ?? '');
// Respect explicit status from probe (e.g. phone probe knows if device is offline)
// Fall back to "online" for nmap results which only report reachable hosts
$status = in_array($d['status'] ?? '', ['online','offline']) ? $d['status'] : 'online';
if (!$ip) continue;
$discoveredIPs[] = $ip;
JarvisDB::execute(
'INSERT INTO network_devices (ip, mac, hostname, status, last_seen)
VALUES (?,?,?,?,NOW())
ON DUPLICATE KEY UPDATE
mac = COALESCE(NULLIF(VALUES(mac),""), mac),
hostname = COALESCE(NULLIF(VALUES(hostname),""), hostname),
status = VALUES(status),
last_seen = NOW()',
[$ip, $mac ?: null, $hostname ?: $vendor ?: null, $status]
);
if ($vendor) {
JarvisDB::execute(
'UPDATE network_devices SET device_type=? WHERE ip=? AND (device_type IS NULL OR device_type="")',
[$vendor, $ip]
);
}
// Store SIP registration status in kb_facts if provided (VoIP probe)
$sipStatus = trim($d['sip_status'] ?? '');
$extension = trim($d['extension'] ?? '');
if ($sipStatus && $extension && $extension !== 'none') {
JarvisDB::execute(
"INSERT INTO kb_facts (category, fact_key, fact_value, host)
VALUES ('voip', ?, ?, ?)
ON DUPLICATE KEY UPDATE fact_value=VALUES(fact_value), updated_at=NOW()",
["ext_{$extension}_sip", $sipStatus, $ip]
);
JarvisDB::execute(
"INSERT INTO kb_facts (category, fact_key, fact_value, host)
VALUES ('voip', ?, ?, ?)
ON DUPLICATE KEY UPDATE fact_value=VALUES(fact_value), updated_at=NOW()",
["ext_{$extension}_ip", $ip, $ip]
);
}
$upserted++;
}
// Mark anything NOT in this scan as offline if stale > 10 min
if (!empty($discoveredIPs)) {
$ph = implode(',', array_fill(0, count($discoveredIPs), '?'));
JarvisDB::execute(
"UPDATE network_devices SET status='offline'
WHERE ip NOT IN ($ph) AND last_seen < DATE_SUB(NOW(), INTERVAL 10 MINUTE)",
$discoveredIPs
);
}
echo json_encode(['ok' => true, 'upserted' => $upserted, 'total_discovered' => count($discoveredIPs)]);