Adding connections api

This commit is contained in:
2026-03-11 10:57:40 -04:00
parent e0fe0c4d34
commit 3634f502a1
4 changed files with 167 additions and 2 deletions

View File

@@ -180,6 +180,37 @@ function run_fetch(): array {
return $results;
}
// ── API queries ───────────────────────────────────────────────────────────────
/**
* Return connections from the last $minutes minutes, newest first.
* Optionally filtered to a single node.
*/
function connections_since(int $minutes = 10, ?int $node_id = null): array {
$since = date('Y-m-d H:i:s', time() - $minutes * 60);
if ($node_id !== null) {
$s = db()->prepare(
'SELECT c.id, c.occurred_at, c.src_ip, c.src_port, c.dst_port,
c.banner_hex, c.banner_len, n.name AS node_name, n.id AS node_id
FROM connections c JOIN nodes n ON n.id = c.node_id
WHERE c.node_id = ? AND c.occurred_at >= ?
ORDER BY c.occurred_at DESC'
);
$s->execute([$node_id, $since]);
} else {
$s = db()->prepare(
'SELECT c.id, c.occurred_at, c.src_ip, c.src_port, c.dst_port,
c.banner_hex, c.banner_len, n.name AS node_name, n.id AS node_id
FROM connections c JOIN nodes n ON n.id = c.node_id
WHERE c.occurred_at >= ?
ORDER BY c.occurred_at DESC'
);
$s->execute([$since]);
}
return $s->fetchAll();
}
// ── Dashboard stats ───────────────────────────────────────────────────────────
function global_stats(): array {