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

+87 -5
View File
@@ -20,6 +20,8 @@ import { ErrorNotice } from "../components/Notice";
import PortPresets from "../components/PortPresets";
import ViewTabs from "../components/ViewTabs";
import { portName } from "../utils/portName";
import { confirmDialog } from "../utils/dialogs";
import { readableError } from "../utils/errors";
import { EmptyRow, LoadingRow } from "../components/EmptyState";
const emptyForm: PortForwardInput = {
@@ -42,6 +44,9 @@ export default function Ports({ user }: { user: CurrentUser }) {
const [reportingServers, setReportingServers] = useState(0);
const [totalServers, setTotalServers] = useState(0);
const [agentError, setAgentError] = useState<string | null>(null);
const [onlyNew, setOnlyNew] = useState(false);
/** Keys ("serverId:tcp:8080") of the ports being marked as expected right now. */
const [marking, setMarking] = useState<Set<string>>(new Set());
const [servers, setServers] = useState<ServerRecord[] | null>(null);
const [forwards, setForwards] = useState<PortForward[] | null>(null);
@@ -79,8 +84,12 @@ export default function Ports({ user }: { user: CurrentUser }) {
.catch(() => {});
}, []);
const newPorts = (agentPorts ?? []).filter((p) => p.isNew);
const newServers = new Set(newPorts.map((p) => p.serverId)).size;
// With none left, the filter has nothing to show: leave it rather than show an empty table that looks like a problem.
const showOnlyNew = onlyNew && newPorts.length > 0;
const { sorted: sortedAgentPorts, sortKey: agentSortKey, sortDir: agentSortDir, requestSort: requestAgentSort } = useSortable(
agentPorts,
agentPorts === null ? null : showOnlyNew ? newPorts : agentPorts,
"serverName",
);
const { pageItems: agentPageItems, page: agentPage, setPage: setAgentPage, pageCount: agentPageCount, totalCount: agentTotalCount } = usePagination(
@@ -92,10 +101,41 @@ export default function Ports({ user }: { user: CurrentUser }) {
"externalPort",
);
const portId = (p: AgentPortRow) => `${p.serverId}:${p.protocol}:${p.port}`;
/** "This is expected": the same note the server's own Ports card writes, so the new-port alert ends. One at a time, so a refusal names the port it stopped at. */
async function markExpected(list: AgentPortRow[]) {
setAgentError(null);
setMarking((prev) => new Set([...prev, ...list.map(portId)]));
try {
for (const p of list) {
try {
await api.servers.ports.save(p.serverId, { port: p.port, protocol: p.protocol, label: "Expected", comment: "" });
} catch (err) {
setAgentError(`${p.serverName} ${p.protocol}/${p.port}: ${readableError(err)}`);
break;
}
}
} finally {
setMarking(new Set());
loadAgentPorts();
}
}
async function markAllExpected() {
const names = [...new Set(newPorts.map((p) => p.serverName))];
const ok = await confirmDialog({
title: "Mark every new port as expected",
message: `${newPorts.length} port${newPorts.length !== 1 ? "s" : ""} on ${names.length} server${names.length !== 1 ? "s" : ""} (${names.join(", ")}) will be noted as expected, and their alerts end. Only do this if you have looked at what is listening on each.`,
confirmLabel: "Mark as expected",
});
if (ok) await markExpected(newPorts);
}
function exportAgentCsv() {
downloadCsv(
"agent-reported-ports.csv",
["Server", "Hostname", "Protocol", "Port", "Service", "Addresses", "Process", "Local only"],
["Server", "Hostname", "Protocol", "Port", "Service", "Addresses", "Process", "Local only", "New"],
(sortedAgentPorts ?? []).map((p) => [
p.serverName,
p.serverHostname ?? "",
@@ -105,6 +145,7 @@ export default function Ports({ user }: { user: CurrentUser }) {
p.addresses.join(", "),
p.process ?? "",
p.localOnly ? "yes" : "no",
p.isNew ? "yes" : "no",
]),
);
}
@@ -220,6 +261,27 @@ export default function Ports({ user }: { user: CurrentUser }) {
</div>
</div>
<ErrorNotice error={agentError} className="m-3" />
{newPorts.length > 0 && (
<div className="alert alert-warning mx-3 mt-3 mb-0 d-flex flex-wrap align-items-center gap-2" role="status">
<div className="me-auto">
<strong>
{newPorts.length} new port{newPorts.length !== 1 ? "s" : ""}
</strong>{" "}
on {newServers} server{newServers !== 1 ? "s" : ""} began listening after that server's first report, and nobody has said {newPorts.length !== 1 ? "they are" : "it is"} expected. Each is a warning on the{" "}
<Link to="/alerts">Alerts page</Link> until it is marked as expected or stops listening.
</div>
<div className="btn-list flex-nowrap">
<button className="btn btn-sm btn-outline-secondary" onClick={() => setOnlyNew(!showOnlyNew)} aria-pressed={showOnlyNew}>
{showOnlyNew ? "Show all ports" : "Show only new"}
</button>
{canEdit && (
<button className="btn btn-sm btn-primary" onClick={() => void markAllExpected()} disabled={marking.size > 0}>
Mark all as expected
</button>
)}
</div>
</div>
)}
<div className="table-responsive">
<table className="table table-vcenter card-table">
<thead>
@@ -231,6 +293,7 @@ export default function Ports({ user }: { user: CurrentUser }) {
<th>Address</th>
<th>Process</th>
<SortableTh<AgentPortRow> label="Last report" sortKeyName="lastSeenAt" activeKey={agentSortKey} direction={agentSortDir} onSort={requestAgentSort} />
{canEdit && newPorts.length > 0 && <th />}
</tr>
</thead>
<tbody>
@@ -241,7 +304,17 @@ export default function Ports({ user }: { user: CurrentUser }) {
{p.serverHostname && <div className="text-secondary small">{p.serverHostname}</div>}
</td>
<td className="text-uppercase text-secondary">{p.protocol}</td>
<td>{p.port}</td>
<td>
{p.port}
{p.isNew && (
<span
className="badge bg-yellow-lt text-yellow ms-2"
title="It began listening after this server's first report and nobody has said it is expected. It is on the Alerts page until you mark it as expected or it stops listening."
>
New
</span>
)}
</td>
<td className="text-secondary" title={portName(p)?.title}>{portName(p)?.text ?? "—"}</td>
<td className="text-secondary">
{p.addresses.join(", ")}
@@ -249,11 +322,20 @@ export default function Ports({ user }: { user: CurrentUser }) {
</td>
<td className="text-secondary">{p.process ?? "—"}</td>
<td className="text-secondary"><TimeAgo value={p.lastSeenAt} /></td>
{canEdit && newPorts.length > 0 && (
<td className="text-end">
{p.isNew && (
<button className="btn btn-sm btn-outline-primary" onClick={() => void markExpected([p])} disabled={marking.has(portId(p))} title="Notes it as expected on the server's Ports card, so there is no alert for it">
Mark as expected
</button>
)}
</td>
)}
</tr>
))}
{agentPorts === null && <LoadingRow colSpan={7} />}
{agentPorts === null && <LoadingRow colSpan={8} />}
{agentPorts !== null && agentPorts.length === 0 && (
<EmptyRow colSpan={7} title="No listening ports reported yet" hint="Each server's agent lists what is listening with its next report. Install or update the agent to see ports here." action={{ label: "Go to Servers", to: "/servers" }} />
<EmptyRow colSpan={8} title="No listening ports reported yet" hint="Each server's agent lists what is listening with its next report. Install or update the agent to see ports here." action={{ label: "Go to Servers", to: "/servers" }} />
)}
</tbody>
</table>