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:
bobbanandClaude Sonnet 5.5 committed 2026-10-30 22:39:52 +01:00
1 parent 93aaf5476d
commit fca2dc0338
24 files changed
+9764 -40

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
/**
* A port preset written as text, one port to a line: the pure parser and formatter the preset editor on the Ports page uses, run here against the web's own source file.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { pathToFileURL } from "node:url";
import { join } from "node:path";
const web = (file: string) => pathToFileURL(join(import.meta.dirname, "../../web/src/utils", file)).href;
const { parsePortLines, formatPortLines, MAX_PRESET_PORTS } = (await import(web("portPresetText.ts"))) as typeof import("../../web/src/utils/portPresetText");
describe("parsePortLines", () => {
test("reads a port and its name, TCP unless /udp is written", () => {
const { ports, errors } = parsePortLines("22 SSH\n3389/tcp RDP\n53/udp DNS");
assert.deepEqual(errors, []);
assert.deepEqual(ports, [
{ port: 22, protocol: "tcp", label: "SSH" },
{ port: 3389, protocol: "tcp", label: "RDP" },
{ port: 53, protocol: "udp", label: "DNS" },
]);
});
test("accepts a separator after the port and keeps a name with spaces", () => {
const { ports, errors } = parsePortLines("5432: PostgreSQL main\r\n6379 - Redis\n8006=Proxmox VE");
assert.deepEqual(errors, []);
assert.deepEqual(
ports.map((p) => [p.port, p.label]),
[[5432, "PostgreSQL main"], [6379, "Redis"], [8006, "Proxmox VE"]],
);
});
test("skips blank lines and comments", () => {
const { ports, errors } = parsePortLines("# remote access\n\n 22 SSH \n \n");
assert.deepEqual(errors, []);
assert.equal(ports.length, 1);
assert.equal(ports[0].label, "SSH");
});
test("says what is wrong with a line, by number, and still returns the good ones", () => {
const { ports, errors } = parsePortLines("22 SSH\nSSH 22\n70000 Nope\n80\n22 Again\n443/udp QUIC\n443/udp Twice");
assert.deepEqual(ports.map((p) => `${p.port}/${p.protocol}`), ["22/tcp", "443/udp"]);
assert.equal(errors.length, 5);
assert.match(errors[0], /^Line 2:/);
assert.match(errors[1], /^Line 3: 70000 is not a port/);
assert.match(errors[2], /^Line 4: say what 80 is/);
assert.match(errors[3], /^Line 5: 22\/tcp is in the list twice/);
assert.match(errors[4], /^Line 7: 443\/udp is in the list twice/);
});
test("the same number over TCP and UDP is two ports", () => {
const { ports, errors } = parsePortLines("53 DNS\n53/udp DNS");
assert.deepEqual(errors, []);
assert.equal(ports.length, 2);
});
test("refuses a name longer than 60 characters and more ports than a preset holds", () => {
assert.match(parsePortLines(`22 ${"x".repeat(61)}`).errors[0], /longer than 60/);
const many = Array.from({ length: MAX_PRESET_PORTS + 1 }, (_, i) => `${i + 1} p${i + 1}`).join("\n");
assert.match(parsePortLines(many).errors.join(" "), /At most 60 ports/);
});
});
describe("formatPortLines", () => {
test("writes TCP without the protocol and UDP with it, and reads back the same", () => {
const ports = [
{ port: 22, protocol: "tcp" as const, label: "SSH" },
{ port: 53, protocol: "udp" as const, label: "DNS" },
];
const text = formatPortLines(ports);
assert.equal(text, "22 SSH\n53/udp DNS");
assert.deepEqual(parsePortLines(text).ports, ports);
});
});