Show new ports across every server on the Ports page, and mark them as expected there

A New badge, a notice with Show only new and Mark all as expected, and Mark as expected on each row. Uses the same rule as the alert and the server's Ports card.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-03 23:54:11 +01:00
1 parent ccb863e7c3
commit c79b2eda0d
7 files changed
+139 -13

No files matched your search

+20 -2
View File
@@ -2,11 +2,13 @@ import { Router } from "express";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { servers, portForwards } from "../db/schema.js";
import { servers, serverPorts, portForwards } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { type AgentPort, groupAgentPorts, parseJson, splitPortKey } from "../services/agentPorts.js";
import { listPresets, serviceName, serviceNames } from "../services/portPresets.js";
import { newListeningPorts, parseBaseline, portKey } from "../services/newPorts.js";
import { getSettings } from "../services/settingsStore.js";
import { containerPortName, containerPortNames, type ContainerPortName } from "../services/containerPorts.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -30,19 +32,34 @@ export interface AgentPortRow {
/** The container that publishes it, as Dockhand says, when one does. */
container: ContainerPortName | null;
lastSeenAt: string | null;
/** Began listening after the server's first report and nobody has noted it: a "new port" alert is on until it is noted or stops listening. */
isNew: boolean;
}
portsRouter.get("/agent", asyncHandler(async (_req, res) => {
const rows = await db
.select({ id: servers.id, name: servers.name, hostname: servers.hostname, listeningPorts: servers.listeningPorts, lastSeenAt: servers.lastSeenAt })
.select({ id: servers.id, name: servers.name, hostname: servers.hostname, listeningPorts: servers.listeningPorts, portBaseline: servers.portBaseline, lastSeenAt: servers.lastSeenAt })
.from(servers);
// Which are new: the rule the alert and the server's Ports card use — listening, reachable from outside, not in the baseline, and nobody has given the port a label or a comment.
const alerting = (await getSettings()).healthChecks.newPortAlerts;
const expectedByServer = new Map<number, Set<string>>();
if (alerting) {
for (const r of await db.select().from(serverPorts)) {
if (!r.label && !r.comment) continue;
const set = expectedByServer.get(r.serverId) ?? new Set<string>();
set.add(portKey(r.protocol, r.port));
expectedByServer.set(r.serverId, set);
}
}
const names = serviceNames(await listPresets());
const containers = await containerPortNames();
const out: AgentPortRow[] = [];
for (const s of rows) {
const raw = parseJson<AgentPort[] | null>(s.listeningPorts, null);
if (!raw) continue;
const fresh = new Set(alerting ? newListeningPorts(raw, parseBaseline(s.portBaseline), expectedByServer.get(s.id) ?? new Set()).map((p) => portKey(p.protocol, p.port)) : []);
for (const [key, grouped] of groupAgentPorts(raw)) {
const { protocol, port } = splitPortKey(key);
out.push({
@@ -57,6 +74,7 @@ portsRouter.get("/agent", asyncHandler(async (_req, res) => {
service: serviceName(names, protocol, port),
container: containerPortName(containers, s.id, protocol, port),
lastSeenAt: s.lastSeenAt,
isNew: fresh.has(key),
});
}
}