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,106 @@
|
||||
/**
|
||||
* Port presets: a named list of ports and what each is ("Remote access" = 22 SSH, 3389 RDP …), written once and used on every host — to put a name to a port wherever it shows (without a label
|
||||
* typed for each host), to scan several hosts for just those ports, and to write the names onto the ports that are open on many hosts in one go.
|
||||
*
|
||||
* A few ready-made presets are put there the first time they are asked for; they are ordinary presets (change them, delete them) and are not put back once the list has been touched — deleting them all
|
||||
* leaves none. Scanning is TCP only (as the Ports card's scan is), so a UDP entry in a preset is for naming only.
|
||||
*/
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { portPresets } from "../db/schema.js";
|
||||
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
|
||||
|
||||
export interface PresetPort {
|
||||
port: number;
|
||||
protocol: "tcp" | "udp";
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface PortPreset {
|
||||
id: number;
|
||||
name: string;
|
||||
ports: PresetPort[];
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export const MAX_PRESET_PORTS = 60;
|
||||
const SEEDED_FLAG = "portPresetsSeeded";
|
||||
|
||||
const tcp = (port: number, label: string): PresetPort => ({ port, protocol: "tcp", label });
|
||||
|
||||
/** What is there to begin with. */
|
||||
export const STARTER_PRESETS: { name: string; ports: PresetPort[] }[] = [
|
||||
{ name: "Remote access", ports: [tcp(22, "SSH"), tcp(3389, "RDP"), tcp(5900, "VNC"), tcp(5985, "WinRM (HTTP)"), tcp(5986, "WinRM (HTTPS)")] },
|
||||
{ name: "Databases", ports: [tcp(1433, "SQL Server"), tcp(1521, "Oracle"), tcp(3306, "MySQL / MariaDB"), tcp(5432, "PostgreSQL"), tcp(6379, "Redis"), tcp(11211, "Memcached"), tcp(27017, "MongoDB")] },
|
||||
{ name: "Web", ports: [tcp(80, "HTTP"), tcp(443, "HTTPS"), tcp(8080, "HTTP (alternate)"), tcp(8443, "HTTPS (alternate)")] },
|
||||
{ name: "File sharing", ports: [tcp(21, "FTP"), tcp(139, "NetBIOS"), tcp(445, "SMB"), tcp(873, "rsync"), tcp(2049, "NFS")] },
|
||||
{ name: "Homelab tools", ports: [tcp(3000, "Grafana"), tcp(5000, "Synology DSM"), tcp(5001, "Synology DSM (HTTPS)"), tcp(8006, "Proxmox VE"), tcp(8007, "Proxmox Backup Server"), tcp(9090, "Prometheus")] },
|
||||
];
|
||||
|
||||
function parsePorts(stored: string): PresetPort[] {
|
||||
try {
|
||||
const v = JSON.parse(stored);
|
||||
if (!Array.isArray(v)) return [];
|
||||
return v
|
||||
.filter((p): p is PresetPort => p !== null && typeof p === "object" && Number.isInteger(p.port) && (p.protocol === "tcp" || p.protocol === "udp") && typeof p.label === "string")
|
||||
.map((p) => ({ port: p.port, protocol: p.protocol, label: p.label }));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
const rowToPreset = (r: typeof portPresets.$inferSelect): PortPreset => ({ id: r.id, name: r.name, ports: parsePorts(r.ports), updatedAt: r.updatedAt });
|
||||
|
||||
/** Puts the ready-made presets there, once: the first time, and only into an empty list. */
|
||||
async function ensureStarters(): Promise<void> {
|
||||
if ((await getInternalFlag(SEEDED_FLAG)) !== null) return;
|
||||
const existing = await db.select({ id: portPresets.id }).from(portPresets).limit(1);
|
||||
if (existing.length === 0) {
|
||||
const now = new Date().toISOString();
|
||||
for (const p of STARTER_PRESETS) await db.insert(portPresets).values({ name: p.name, ports: JSON.stringify(p.ports), createdAt: now, updatedAt: now });
|
||||
}
|
||||
await setInternalFlag(SEEDED_FLAG, new Date().toISOString());
|
||||
}
|
||||
|
||||
/** Every preset, by name. */
|
||||
export async function listPresets(): Promise<PortPreset[]> {
|
||||
await ensureStarters();
|
||||
return (await db.select().from(portPresets).orderBy(asc(portPresets.name))).map(rowToPreset);
|
||||
}
|
||||
|
||||
export async function getPreset(id: number): Promise<PortPreset | null> {
|
||||
await ensureStarters();
|
||||
const [row] = await db.select().from(portPresets).where(eq(portPresets.id, id)).limit(1);
|
||||
return row ? rowToPreset(row) : null;
|
||||
}
|
||||
|
||||
const keyOf = (protocol: string, port: number) => `${protocol}:${port}`;
|
||||
|
||||
/**
|
||||
* Pure: what each port is called, from every preset: "tcp:3389" → "RDP". A port in more than one preset is called what the first of them (by name) calls it, so a port is never
|
||||
* called two things at once.
|
||||
*/
|
||||
export function serviceNames(presets: PortPreset[]): Map<string, string> {
|
||||
const names = new Map<string, string>();
|
||||
for (const p of presets) for (const e of p.ports) if (!names.has(keyOf(e.protocol, e.port))) names.set(keyOf(e.protocol, e.port), e.label);
|
||||
return names;
|
||||
}
|
||||
|
||||
export const serviceName = (names: Map<string, string>, protocol: string, port: number): string | null => names.get(keyOf(protocol, port)) ?? null;
|
||||
|
||||
/** Pure: the TCP ports of a preset, in order — what a scan of it looks at. */
|
||||
export const scannablePorts = (preset: Pick<PortPreset, "ports">): number[] => [...new Set(preset.ports.filter((p) => p.protocol === "tcp").map((p) => p.port))].sort((a, b) => a - b);
|
||||
|
||||
/** Runs `run` for each item, at most `limit` at a time, and returns the answers in the order of the items. */
|
||||
export async function mapLimit<T, R>(items: T[], limit: number, run: (item: T) => Promise<R>): Promise<R[]> {
|
||||
const out = new Array<R>(items.length);
|
||||
let next = 0;
|
||||
async function worker() {
|
||||
while (next < items.length) {
|
||||
const i = next++;
|
||||
out[i] = await run(items[i]);
|
||||
}
|
||||
}
|
||||
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user