Port presets: name ports automatically, scan many hosts for a preset, apply names in bulk
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
93aaf5476d
commit
fca2dc0338
24 files changed
+9764
-40
No files matched your search
@@ -0,0 +1,231 @@
|
||||
import { Router } from "express";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { portPresets, servers, serverPorts } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { type AgentPort, groupAgentPorts, parseJson } from "../services/agentPorts.js";
|
||||
import { getPreset, listPresets, mapLimit, MAX_PRESET_PORTS, scannablePorts, type PresetPort } from "../services/portPresets.js";
|
||||
import { beginScan, endScan, resolveScanTarget, scanPortList } from "../services/portScan.js";
|
||||
import { recordPortScan, scanResponded } from "../services/portScanRecord.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const portPresetsRouter = Router();
|
||||
portPresetsRouter.use(requireAuth);
|
||||
|
||||
/** How many hosts a preset is scanned on at the same time: each scan holds hundreds of sockets open at most, but a whole network at once is a burst nobody asked for. */
|
||||
const HOSTS_AT_ONCE = 3;
|
||||
const MAX_TARGETS = 100;
|
||||
|
||||
const portSchema = z.object({
|
||||
port: z.number().int().min(1).max(65535),
|
||||
protocol: z.enum(["tcp", "udp"]).default("tcp"),
|
||||
label: z.string().trim().min(1).max(60),
|
||||
});
|
||||
|
||||
const presetSchema = z
|
||||
.object({
|
||||
name: z.string().trim().min(1).max(60),
|
||||
ports: z.array(portSchema).min(1).max(MAX_PRESET_PORTS),
|
||||
})
|
||||
.refine((d) => new Set(d.ports.map((p) => `${p.protocol}:${p.port}`)).size === d.ports.length, { message: "A port is in the list twice." });
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
|
||||
const invalid = (res: import("express").Response, parsed: { error: z.ZodError }) =>
|
||||
res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "That is not a preset.", details: parsed.error.flatten() });
|
||||
|
||||
// Everyone signed in sees the presets: they are what ports are called.
|
||||
portPresetsRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
res.json({ presets: await listPresets() });
|
||||
}));
|
||||
|
||||
portPresetsRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = presetSchema.safeParse(req.body);
|
||||
if (!parsed.success) return invalid(res, parsed);
|
||||
const taken = (await listPresets()).some((p) => p.name.toLowerCase() === parsed.data.name.toLowerCase());
|
||||
if (taken) return res.status(409).json({ error: "name_taken", message: "There is a preset with that name already." });
|
||||
const now = new Date().toISOString();
|
||||
const [row] = await db.insert(portPresets).values({ name: parsed.data.name, ports: JSON.stringify(parsed.data.ports), createdAt: now, updatedAt: now }).returning();
|
||||
await recordAudit({ actor: req.currentUser!, category: "server", action: "create_port_preset", targetType: "port_preset", targetId: row.id, detail: { name: row.name, ports: parsed.data.ports.length } });
|
||||
res.status(201).json({ preset: { id: row.id, name: row.name, ports: parsed.data.ports, updatedAt: row.updatedAt } });
|
||||
}));
|
||||
|
||||
portPresetsRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const parsed = presetSchema.safeParse(req.body);
|
||||
if (!parsed.success) return invalid(res, parsed);
|
||||
const before = await getPreset(id);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
const taken = (await listPresets()).some((p) => p.id !== id && p.name.toLowerCase() === parsed.data.name.toLowerCase());
|
||||
if (taken) return res.status(409).json({ error: "name_taken", message: "There is a preset with that name already." });
|
||||
const now = new Date().toISOString();
|
||||
await db.update(portPresets).set({ name: parsed.data.name, ports: JSON.stringify(parsed.data.ports), updatedAt: now }).where(eq(portPresets.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "server", action: "update_port_preset", targetType: "port_preset", targetId: id, detail: { name: parsed.data.name, ports: parsed.data.ports.length } });
|
||||
res.json({ preset: { id, name: parsed.data.name, ports: parsed.data.ports, updatedAt: now } });
|
||||
}));
|
||||
|
||||
portPresetsRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const before = await getPreset(id);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(portPresets).where(eq(portPresets.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "server", action: "delete_port_preset", targetType: "port_preset", targetId: id, detail: { name: before.name } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// ─── Scanning hosts for a preset ────────────────────────────────────────────
|
||||
|
||||
const scanSchema = z.object({
|
||||
targets: z
|
||||
.array(z.object({ serverId: z.number().int().positive(), address: z.string().trim().max(255).optional() }))
|
||||
.min(1)
|
||||
.max(MAX_TARGETS),
|
||||
});
|
||||
|
||||
export interface PresetHostResult {
|
||||
serverId: number;
|
||||
name: string;
|
||||
/** The address that was scanned; null when none could be found. */
|
||||
address: string | null;
|
||||
/** scanned: answered; no_answer: nothing answered (down, or dropping everything — says nothing of its ports); skipped: no address; error: could not be scanned; busy: a scan of it was running. */
|
||||
state: "scanned" | "no_answer" | "skipped" | "error" | "busy";
|
||||
message: string | null;
|
||||
open: number[];
|
||||
/** Refused: the host answered "nothing here". */
|
||||
closed: number[];
|
||||
/** Never answered (a firewall dropping packets). */
|
||||
filtered: number;
|
||||
}
|
||||
|
||||
/** The address to scan for a server that was not told one: the first address it is known by, else its host name. */
|
||||
export function addressOf(server: { ipAddresses: string | null; hostname: string | null }): string | null {
|
||||
const ips = parseJson<unknown>(server.ipAddresses, []);
|
||||
const first = Array.isArray(ips) ? ips.find((i): i is string => typeof i === "string" && i.trim() !== "") : undefined;
|
||||
return first?.trim() ?? (server.hostname?.trim() || null);
|
||||
}
|
||||
|
||||
portPresetsRouter.post("/:id/scan", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const parsed = scanSchema.safeParse(req.body);
|
||||
if (!parsed.success) return invalid(res, parsed);
|
||||
const preset = await getPreset(id);
|
||||
if (!preset) return res.status(404).json({ error: "not_found" });
|
||||
const ports = scannablePorts(preset);
|
||||
if (ports.length === 0) return res.status(400).json({ error: "nothing_to_scan", message: "This preset has no TCP ports — a scan only looks at TCP." });
|
||||
const wanted = new Set(ports);
|
||||
// A server named twice is scanned once, as the first request for it says.
|
||||
const targets = parsed.data.targets.filter((t, i, all) => all.findIndex((x) => x.serverId === t.serverId) === i);
|
||||
|
||||
const rows = await db.select({ id: servers.id, name: servers.name, ipAddresses: servers.ipAddresses, hostname: servers.hostname }).from(servers).where(inArray(servers.id, targets.map((t) => t.serverId)));
|
||||
const byId = new Map(rows.map((r) => [r.id, r]));
|
||||
|
||||
const results = await mapLimit(targets, HOSTS_AT_ONCE, async (t): Promise<PresetHostResult> => {
|
||||
const server = byId.get(t.serverId);
|
||||
const base = { serverId: t.serverId, name: server?.name ?? `#${t.serverId}`, open: [] as number[], closed: [] as number[], filtered: 0 };
|
||||
if (!server) return { ...base, address: null, state: "error", message: "That server does not exist." };
|
||||
const given = t.address && t.address !== "" ? t.address : addressOf(server);
|
||||
if (!given) return { ...base, address: null, state: "skipped", message: "No address is known for it — give one." };
|
||||
let target: string;
|
||||
try {
|
||||
target = await resolveScanTarget(given);
|
||||
} catch (err) {
|
||||
return { ...base, address: given, state: "error", message: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
if (!beginScan(server.id)) return { ...base, address: target, state: "busy", message: "A scan of this server is already running." };
|
||||
let scan;
|
||||
try {
|
||||
scan = await scanPortList(target, ports);
|
||||
} catch (err) {
|
||||
return { ...base, address: target, state: "error", message: err instanceof Error ? err.message : String(err) };
|
||||
} finally {
|
||||
endScan(server.id);
|
||||
}
|
||||
if (!scanResponded(scan)) return { ...base, address: target, state: "no_answer", message: "Nothing answered — it may be down, or dropping everything.", filtered: scan.filtered };
|
||||
await recordPortScan(server.id, (p) => wanted.has(p), scan, new Date().toISOString());
|
||||
return { ...base, address: target, state: "scanned", message: null, open: scan.open, closed: scan.refused, filtered: scan.filtered };
|
||||
});
|
||||
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "server",
|
||||
action: "scan_port_preset",
|
||||
targetType: "port_preset",
|
||||
targetId: id,
|
||||
detail: { name: preset.name, servers: results.length, scanned: results.filter((r) => r.state === "scanned").length, open: results.reduce((n, r) => n + r.open.length, 0) },
|
||||
});
|
||||
res.json({ preset: { id: preset.id, name: preset.name }, ports, labels: Object.fromEntries(preset.ports.filter((p) => p.protocol === "tcp").map((p) => [p.port, p.label])), results });
|
||||
}));
|
||||
|
||||
// ─── Writing a preset's names onto many hosts ───────────────────────────────
|
||||
|
||||
const applySchema = z.object({
|
||||
serverIds: z.array(z.number().int().positive()).min(1).max(500),
|
||||
/** Only the ports that are open on the host (a scan found them, or its agent says they are listening) — the default. False notes every port of the preset on every host. */
|
||||
onlyOpen: z.boolean().default(true),
|
||||
/** Replace a label somebody wrote with the preset's. Never by default. */
|
||||
overwrite: z.boolean().default(false),
|
||||
});
|
||||
|
||||
export interface PresetApplyResult {
|
||||
serverId: number;
|
||||
name: string;
|
||||
labelled: { port: number; protocol: "tcp" | "udp"; label: string }[];
|
||||
skipped: { port: number; protocol: "tcp" | "udp"; reason: "not_open" | "has_label" | "same" }[];
|
||||
}
|
||||
|
||||
portPresetsRouter.post("/:id/apply", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const parsed = applySchema.safeParse(req.body);
|
||||
if (!parsed.success) return invalid(res, parsed);
|
||||
const preset = await getPreset(id);
|
||||
if (!preset) return res.status(404).json({ error: "not_found" });
|
||||
const { onlyOpen, overwrite } = parsed.data;
|
||||
const ids = [...new Set(parsed.data.serverIds)];
|
||||
const rows = await db.select({ id: servers.id, name: servers.name, listeningPorts: servers.listeningPorts }).from(servers).where(inArray(servers.id, ids));
|
||||
const existing = await db.select().from(serverPorts).where(inArray(serverPorts.serverId, rows.map((r) => r.id)));
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const results: PresetApplyResult[] = [];
|
||||
for (const server of rows) {
|
||||
const mine = existing.filter((r) => r.serverId === server.id);
|
||||
const agent = groupAgentPorts(parseJson<AgentPort[] | null>(server.listeningPorts, null) ?? []);
|
||||
const labelled: PresetApplyResult["labelled"] = [];
|
||||
const skipped: PresetApplyResult["skipped"] = [];
|
||||
for (const p of preset.ports as PresetPort[]) {
|
||||
const row = mine.find((r) => r.port === p.port && r.protocol === p.protocol);
|
||||
const open = !!row?.open || agent.has(`${p.protocol}:${p.port}`);
|
||||
if (onlyOpen && !open) {
|
||||
skipped.push({ port: p.port, protocol: p.protocol, reason: "not_open" });
|
||||
continue;
|
||||
}
|
||||
if (row?.label === p.label) {
|
||||
skipped.push({ port: p.port, protocol: p.protocol, reason: "same" });
|
||||
continue;
|
||||
}
|
||||
if (row?.label && !overwrite) {
|
||||
skipped.push({ port: p.port, protocol: p.protocol, reason: "has_label" });
|
||||
continue;
|
||||
}
|
||||
if (row) await db.update(serverPorts).set({ label: p.label, updatedAt: now }).where(and(eq(serverPorts.id, row.id), eq(serverPorts.serverId, server.id)));
|
||||
else await db.insert(serverPorts).values({ serverId: server.id, port: p.port, protocol: p.protocol, label: p.label, updatedAt: now });
|
||||
labelled.push({ port: p.port, protocol: p.protocol, label: p.label });
|
||||
}
|
||||
results.push({ serverId: server.id, name: server.name, labelled, skipped });
|
||||
}
|
||||
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "server",
|
||||
action: "apply_port_preset",
|
||||
targetType: "port_preset",
|
||||
targetId: id,
|
||||
detail: { name: preset.name, servers: results.length, labelled: results.reduce((n, r) => n + r.labelled.length, 0), onlyOpen, overwrite },
|
||||
});
|
||||
res.json({ preset: { id: preset.id, name: preset.name }, results });
|
||||
}));
|
||||
@@ -6,6 +6,7 @@ import { servers, 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 { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const portsRouter = Router();
|
||||
@@ -23,6 +24,8 @@ export interface AgentPortRow {
|
||||
addresses: string[];
|
||||
process: string | null;
|
||||
localOnly: boolean;
|
||||
/** What a port preset calls it ("RDP"), when one does. */
|
||||
service: string | null;
|
||||
lastSeenAt: string | null;
|
||||
}
|
||||
|
||||
@@ -31,6 +34,7 @@ portsRouter.get("/agent", asyncHandler(async (_req, res) => {
|
||||
.select({ id: servers.id, name: servers.name, hostname: servers.hostname, listeningPorts: servers.listeningPorts, lastSeenAt: servers.lastSeenAt })
|
||||
.from(servers);
|
||||
|
||||
const names = serviceNames(await listPresets());
|
||||
const out: AgentPortRow[] = [];
|
||||
for (const s of rows) {
|
||||
const raw = parseJson<AgentPort[] | null>(s.listeningPorts, null);
|
||||
@@ -46,6 +50,7 @@ portsRouter.get("/agent", asyncHandler(async (_req, res) => {
|
||||
addresses: grouped.addresses,
|
||||
process: grouped.process,
|
||||
localOnly: grouped.localOnly,
|
||||
service: serviceName(names, protocol, port),
|
||||
lastSeenAt: s.lastSeenAt,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Router } from "express";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { servers, serverPorts } from "../db/schema.js";
|
||||
@@ -7,6 +7,8 @@ import { requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { beginScan, endScan, MAX_SCAN_SPAN, resolveScanTarget, scanPorts, toRanges } from "../services/portScan.js";
|
||||
import { type AgentPort, groupAgentPorts, parseJson } from "../services/agentPorts.js";
|
||||
import { listPresets, serviceName, serviceNames } from "../services/portPresets.js";
|
||||
import { recordPortScan } from "../services/portScanRecord.js";
|
||||
import { newListeningPorts, parseBaseline, portKey } from "../services/newPorts.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -32,6 +34,8 @@ export interface PortEntry {
|
||||
protocol: "tcp" | "udp";
|
||||
label: string | null;
|
||||
comment: string | null;
|
||||
/** What a port preset calls this port ("RDP"), when one does; shown where there is no label of its own. */
|
||||
service: string | null;
|
||||
/** The last scan from this app connected to it. */
|
||||
scanOpen: boolean;
|
||||
lastSeenOpenAt: string | null;
|
||||
@@ -55,6 +59,7 @@ async function buildPortList(serverId: number) {
|
||||
const alerting = (await getSettings()).healthChecks.newPortAlerts;
|
||||
const fresh = new Set(alerting ? newListeningPorts(agentRaw, parseBaseline(server.portBaseline), expected).map((p) => portKey(p.protocol, p.port)) : []);
|
||||
|
||||
const names = serviceNames(await listPresets());
|
||||
const entries = new Map<string, PortEntry>();
|
||||
for (const row of rows) {
|
||||
const key = `${row.protocol}:${row.port}`;
|
||||
@@ -64,6 +69,7 @@ async function buildPortList(serverId: number) {
|
||||
protocol: row.protocol,
|
||||
label: row.label,
|
||||
comment: row.comment,
|
||||
service: serviceName(names, row.protocol, row.port),
|
||||
scanOpen: row.open,
|
||||
lastSeenOpenAt: row.lastSeenOpenAt,
|
||||
agent: agent.get(key) ?? null,
|
||||
@@ -80,6 +86,7 @@ async function buildPortList(serverId: number) {
|
||||
protocol: protocol as "tcp" | "udp",
|
||||
label: null,
|
||||
comment: null,
|
||||
service: serviceName(names, protocol, Number(port)),
|
||||
scanOpen: false,
|
||||
lastSeenOpenAt: null,
|
||||
agent: info,
|
||||
@@ -160,32 +167,7 @@ serverPortsRouter.post("/scan", requireRole("operator"), asyncHandler(async (req
|
||||
// which ports are open, so leave what we knew before rather than marking it all closed.
|
||||
const responded = scan.open.length + scan.refused.length > 0;
|
||||
|
||||
if (responded) {
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(serverPorts)
|
||||
.where(and(eq(serverPorts.serverId, serverId), eq(serverPorts.protocol, "tcp")));
|
||||
const inRange = existing.filter((r) => r.port >= from && r.port <= to);
|
||||
const openSet = new Set(scan.open);
|
||||
const known = new Set(existing.map((r) => r.port));
|
||||
|
||||
const newlyFound = scan.open.filter((p) => !known.has(p));
|
||||
for (let i = 0; i < newlyFound.length; i += 50) {
|
||||
await db.insert(serverPorts).values(
|
||||
newlyFound.slice(i, i + 50).map((port) => ({ serverId, port, protocol: "tcp" as const, open: true, lastSeenOpenAt: now })),
|
||||
);
|
||||
}
|
||||
const stillOpen = inRange.filter((r) => openSet.has(r.port)).map((r) => r.id);
|
||||
if (stillOpen.length > 0) {
|
||||
await db.update(serverPorts).set({ open: true, lastSeenOpenAt: now }).where(inArray(serverPorts.id, stillOpen));
|
||||
}
|
||||
// No longer open: keep it if someone wrote a note about it (it's now "reserved"), otherwise it carries no information.
|
||||
const gone = inRange.filter((r) => r.open && !openSet.has(r.port));
|
||||
const keep = gone.filter((r) => r.label || r.comment).map((r) => r.id);
|
||||
const drop = gone.filter((r) => !(r.label || r.comment)).map((r) => r.id);
|
||||
if (keep.length > 0) await db.update(serverPorts).set({ open: false }).where(inArray(serverPorts.id, keep));
|
||||
if (drop.length > 0) await db.delete(serverPorts).where(inArray(serverPorts.id, drop));
|
||||
}
|
||||
if (responded) await recordPortScan(serverId, (port) => port >= from && port <= to, scan, now);
|
||||
|
||||
const summary: StoredScan = {
|
||||
at: now,
|
||||
|
||||
Reference in new issue
Block a user