Files
MUA-Mikes-Unraid-Agent/src/helpers.ts
T
2026-08-29 13:27:28 +02:00

1233 lines
46 KiB
TypeScript

/**
* MUA — Mikes Unraid Agent
* helpers.ts — Command execution, JSON-RPC, read operations
*
* Portiert von mcp/helpers.php. Alle Funktionen laufen LOKAL auf dem
* Unraid-Host. Read-Operationen via `docker` CLI. Write-Operationen
* werden an den PHP-Helper delegiert (Bun.spawn), da diese Unraids
* PHP-Klassen (DockerClient) benötigen.
*/
import { spawn } from "bun";
import { createConnection, type Socket } from "net";
import { createHash, randomUUID } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
// ── Konstanten ──────────────────────────────────────────────────────────
export const MUA_SERVER_NAME = "mua";
export const MUA_VERSION = "2026.08.29.r027";
export const MUA_PROTOCOL_VERSION = "2025-03-26";
export const PHP_HELPER = "/usr/local/bin/unraid-docker-mcp-helper.php";
export const STATUS_HELPER = "/usr/local/bin/unraid-mcp-status-helper.php";
// ── Command Execution ───────────────────────────────────────────────────
export interface CmdResult {
stdout: string;
stderr: string;
code: number;
}
/**
* Führe einen lokalen Shell-Befehl aus (via /bin/sh -c).
* Timeout in Sekunden.
*/
export async function runLocal(
label: string,
cmd: string,
timeoutSec = 60,
): Promise<string> {
try {
const proc = spawn(["/bin/sh", "-c", cmd], {
stdout: "pipe",
stderr: "pipe",
});
const timeout = setTimeout(() => proc.kill(), timeoutSec * 1000);
const [stdout, stderr, code] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
clearTimeout(timeout);
let result = stdout.trim();
if (result === "" && stderr.trim() !== "") result = stderr.trim();
return result;
} catch (e) {
throw new Error(`run_local failed for ${label}: ${String(e)}`);
}
}
/**
* Shell-Befehl ausführen und strukturiertes Ergebnis liefern
* (exit_code + stdout + stderr). Für das unraid_system_shell-Tool.
* Liefert JSON, damit der Agent Exit-Code und beides Streams sieht.
*/
export async function runShell(cmd: string, timeoutSec = 60): Promise<string> {
try {
const proc = spawn(["/bin/sh", "-c", cmd], {
stdout: "pipe",
stderr: "pipe",
});
const timeout = setTimeout(() => proc.kill(), timeoutSec * 1000);
const [stdout, stderr, code] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
clearTimeout(timeout);
return JSON.stringify({
exit_code: code,
stdout: sanitizeLogOutput(stdout.trim(), 100000),
stderr: sanitizeLogOutput(stderr.trim(), 20000),
});
} catch (e) {
throw new Error(`run_shell failed: ${String(e)}`);
}
}
const ASYNC_JOB_ROOT = "/tmp/mua-jobs";
const JOB_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function jobPath(jobId: string): string {
if (!JOB_ID_PATTERN.test(jobId)) throw new Error("invalid job_id");
return `${ASYNC_JOB_ROOT}/${jobId}`;
}
async function tailJobFile(path: string, maxBytes = 12_000): Promise<string> {
if (!existsSync(path)) return "";
const size = statSync(path).size;
const start = Math.max(0, size - maxBytes);
return sanitizeLogOutput(await Bun.file(path).slice(start, size).text(), maxBytes);
}
/** Start arbitrary, explicitly authorized host work without tying up MCP HTTP. */
export async function startShellJob(cmd: string): Promise<string> {
if (cmd.trim() === "") throw new Error("command is required");
mkdirSync(ASYNC_JOB_ROOT, { recursive: true, mode: 0o700 });
const jobId = randomUUID();
const dir = jobPath(jobId);
mkdirSync(dir, { mode: 0o700 });
const script = `${dir}/run.sh`;
// The unrestricted command is already a critical/admin-only capability.
// Keep it out of status output and audit responses because it may contain
// credentials supplied by the operator.
writeFileSync(
script,
`#!/bin/sh\n${cmd}\ncode=$?\nprintf '%s\\n' "$code" > '${dir}/exit_code'\nexit "$code"\n`,
{ mode: 0o700 },
);
const proc = spawn(["/bin/sh", script], {
cwd: "/",
stdin: "ignore",
stdout: Bun.file(`${dir}/stdout.log`),
stderr: Bun.file(`${dir}/stderr.log`),
});
proc.unref();
writeFileSync(
`${dir}/meta.json`,
JSON.stringify({ job_id: jobId, pid: proc.pid, started_at: new Date().toISOString() }),
{ mode: 0o600 },
);
return JSON.stringify({ job_id: jobId, state: "running", pid: proc.pid });
}
/** Poll a job with compact tail output; works across independent MCP calls. */
export async function shellJobStatus(jobId: string): Promise<string> {
const dir = jobPath(jobId);
if (!existsSync(`${dir}/meta.json`)) throw new Error("job not found");
const meta = JSON.parse(readFileSync(`${dir}/meta.json`, "utf-8"));
let running = false;
if (!existsSync(`${dir}/exit_code`)) {
try {
process.kill(Number(meta.pid), 0);
running = true;
} catch {
running = false;
}
}
const exitCode = existsSync(`${dir}/exit_code`)
? Number(readFileSync(`${dir}/exit_code`, "utf-8").trim())
: null;
return JSON.stringify({
job_id: jobId,
state: running ? "running" : exitCode === null ? "unknown" : "finished",
exit_code: exitCode,
started_at: meta.started_at,
stdout_tail: await tailJobFile(`${dir}/stdout.log`),
stderr_tail: await tailJobFile(`${dir}/stderr.log`),
});
}
/** Remove only one completed MUA job directory. */
export async function cleanupShellJob(jobId: string): Promise<string> {
const status = JSON.parse(await shellJobStatus(jobId));
if (status.state === "running") throw new Error("cannot clean up a running job");
rmSync(jobPath(jobId), { recursive: true, force: false });
return JSON.stringify({ job_id: jobId, cleaned: true, previous_state: status.state });
}
async function runArgv(
label: string,
argv: string[],
timeoutSec = 60,
maxStdout = 100_000,
maxStderr = 20_000,
): Promise<string> {
try {
const proc = spawn(argv, { stdout: "pipe", stderr: "pipe", cwd: "/" });
const timeout = setTimeout(() => proc.kill(), timeoutSec * 1000);
const [stdout, stderr, code] = await Promise.all([
readStreamLimited(proc.stdout, maxStdout),
readStreamLimited(proc.stderr, maxStderr),
proc.exited,
]);
clearTimeout(timeout);
if (code !== 0) {
throw new Error(`${label} failed (exit ${code}): ${sanitizeLogOutput(stderr.text || stdout.text, maxStderr)}`);
}
return sanitizeLogOutput(stdout.text.trim(), maxStdout);
} catch (error) {
throw new Error(`${label} failed: ${String(error)}`);
}
}
const READ_ONLY_PROGRAMS = new Set([
"cat", "date", "df", "dmesg", "du", "file", "find", "free", "grep",
"head", "hostname", "id", "ip", "lsof", "ls", "lsblk", "lspci", "mount",
"ps", "readlink", "realpath", "sha256sum", "ss", "stat", "tail", "uname",
"uptime", "wc", "whoami",
]);
async function readStreamLimited(
stream: ReadableStream<Uint8Array>,
maxBytes: number,
): Promise<{ text: string; truncated: boolean }> {
const reader = stream.getReader();
const chunks: Uint8Array[] = [];
let kept = 0;
let truncated = false;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const available = Math.max(0, maxBytes - kept);
if (kept < maxBytes) {
const slice = value.subarray(0, available);
if (slice.length > 0) chunks.push(slice);
kept += slice.length;
}
if (value.length > available) truncated = true;
}
const combined = new Uint8Array(chunks.reduce((n, c) => n + c.length, 0));
let offset = 0;
for (const chunk of chunks) {
combined.set(chunk, offset);
offset += chunk.length;
}
return { text: new TextDecoder().decode(combined), truncated };
}
/**
* Führt ausschließlich freigegebene Leseprogramme direkt als argv aus.
* Kein /bin/sh, keine Pipes, Umleitungen, Substitutionen oder Verkettungen.
*/
export async function runReadOnlyCommand(
program: string,
args: string[],
timeoutSec = 30,
maxOutputChars = 12_000,
): Promise<string> {
if (!READ_ONLY_PROGRAMS.has(program)) {
throw new Error(`Program is not allowed in read-only mode: ${program}`);
}
if (args.length > 64 || args.some((arg) => typeof arg !== "string" || arg.length > 4096)) {
throw new Error("Invalid or excessive arguments");
}
if (!Number.isInteger(maxOutputChars) || maxOutputChars < 1_000 || maxOutputChars > 30_000) {
throw new Error("maxOutputChars must be between 1000 and 30000");
}
const lowered = args.map((arg) => arg.toLowerCase());
const reject = (message: string) => { throw new Error(message); };
if (["hostname", "whoami", "uptime"].includes(program) && args.length > 0) {
reject(`${program} does not accept arguments in read-only mode`);
}
if (program === "date") {
const safeDateFlags = new Set(["-u", "--utc", "-r", "--reference", "--rfc-email", "-d", "--date"]);
for (let i = 0; i < args.length; i++) {
const arg = lowered[i];
if (i > 0 && ["-d", "--date", "-r", "--reference"].includes(lowered[i - 1])) continue;
if (arg.startsWith("+") || safeDateFlags.has(arg) || arg.startsWith("--date=") ||
arg.startsWith("--iso-8601") || arg.startsWith("--rfc-3339")) continue;
reject("Only date display and parsing options are allowed");
}
}
if (program === "dmesg" && args.some((a) =>
["-C", "-D", "-E", "-n"].includes(a) ||
["--clear", "--read-clear", "--console-off", "--console-on", "--console-level"].includes(a.toLowerCase())
)) {
reject("Changing or clearing the kernel log is not allowed");
}
if (program === "ss" && lowered.some((a) => a === "-k" || a === "--kill")) {
reject("Killing sockets is not allowed");
}
if (program === "mount" && args.length > 0) {
reject("mount is display-only and accepts no arguments in read-only mode");
}
if (program === "ip") {
const safeObjects = new Set(["address", "addr", "route", "link", "neigh", "neighbor"]);
const mutating = new Set(["add", "append", "change", "delete", "del", "flush", "replace", "set"]);
if (args.length === 0 || !safeObjects.has(lowered[0]) || lowered.some((a) => mutating.has(a))) {
reject("Only read-only ip objects and show/list operations are allowed");
}
}
if (program === "find") {
const mutatingFind = ["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fprint", "-fprintf", "-fls"];
if (lowered.some((a) => mutatingFind.some((blocked) => a === blocked || a.startsWith(blocked)))) {
reject("Mutating find actions are not allowed");
}
}
try {
const proc = spawn([program, ...args], { stdout: "pipe", stderr: "pipe", cwd: "/" });
const timeout = setTimeout(() => proc.kill(), timeoutSec * 1000);
const [stdout, stderr, code] = await Promise.all([
readStreamLimited(proc.stdout, maxOutputChars),
readStreamLimited(proc.stderr, Math.min(8_000, maxOutputChars)),
proc.exited,
]);
clearTimeout(timeout);
return JSON.stringify({
exit_code: code,
stdout: sanitizeLogOutput(stdout.text.trim(), maxOutputChars),
stderr: sanitizeLogOutput(stderr.text.trim(), Math.min(8_000, maxOutputChars)),
truncated: stdout.truncated || stderr.truncated,
mode: "read-only",
});
} catch (e) {
throw new Error(`read-only command failed: ${String(e)}`);
}
}
/**
* Docker-Befehl ausführen (kompakt).
*/
export async function dockerExec(cmd: string, timeoutSec = 60): Promise<string> {
return runLocal("docker", `/usr/bin/docker ${cmd} 2>&1`, timeoutSec);
}
export interface ToolboxContainer {
name: string;
image: string;
state: string;
status: string;
}
/** Find self-declared agent toolboxes without relying on a container name. */
export async function discoverToolboxContainers(): Promise<ToolboxContainer[]> {
const raw = await dockerExec(
"ps -a --filter 'label=mike.ai.role=toolbox' --format '{{json .}}'",
30,
);
if (!raw.trim()) return [];
const rows: ToolboxContainer[] = [];
for (const line of raw.split("\n")) {
if (!line.trim().startsWith("{")) continue;
try {
const item = JSON.parse(line) as Record<string, unknown>;
const name = String(item["Names"] ?? "").trim();
if (!name) continue;
rows.push({
name,
image: String(item["Image"] ?? ""),
state: String(item["State"] ?? "").toLowerCase(),
status: String(item["Status"] ?? ""),
});
} catch {
// Ignore unrelated Docker diagnostics; absence remains a valid result.
}
}
return rows;
}
function selectRunningToolbox(
containers: ToolboxContainer[],
requestedName?: string,
): ToolboxContainer {
const running = containers.filter((container) => container.state === "running");
if (requestedName) {
const selected = running.find((container) => container.name === requestedName);
if (!selected) throw new Error(`Running toolbox container not found: ${requestedName}`);
return selected;
}
if (running.length === 0) throw new Error("No labeled toolbox container is running");
if (running.length > 1) {
throw new Error(
`Multiple toolbox containers are running; specify container: ${running.map((c) => c.name).join(", ")}`,
);
}
return running[0];
}
/** Return labeled toolbox state and optionally probe specific command names. */
export async function toolboxStatus(programs: string[] = []): Promise<string> {
if (programs.length > 32) throw new Error("programs accepts at most 32 names");
for (const program of programs) {
if (!/^[A-Za-z0-9._+-]{1,128}$/.test(program)) {
throw new Error(`Invalid program name: ${program}`);
}
}
const containers = await discoverToolboxContainers();
const running = containers.filter((container) => container.state === "running");
const result: Record<string, unknown> = {
label: "mike.ai.role=toolbox",
available: containers.length > 0,
running: running.length > 0,
containers,
};
if (programs.length === 0 || running.length === 0) return JSON.stringify(result);
if (running.length > 1) {
result["note"] = "Multiple toolboxes are running; availability is reported for the first one.";
}
const selected = running[0];
const script =
'for p do path=$(command -v "$p" 2>/dev/null || true); ' +
'if [ -n "$path" ]; then printf "%s\\t%s\\n" "$p" "$path"; ' +
'else printf "%s\\t\\n" "$p"; fi; done';
const raw = await runArgv(
"toolbox_status",
["/usr/bin/docker", "exec", selected.name, "/bin/sh", "-c", script, "toolbox-status", ...programs],
30,
30_000,
8_000,
);
const paths = new Map(
raw.split("\n").map((line) => {
const tab = line.indexOf("\t");
return tab >= 0 ? [line.slice(0, tab), line.slice(tab + 1)] : [line, ""];
}),
);
result["selected_container"] = selected.name;
result["programs"] = programs.map((program) => ({
name: program,
available: Boolean(paths.get(program)),
path: paths.get(program) || null,
}));
return JSON.stringify(result);
}
/** Execute an explicitly requested shell command inside one labeled toolbox. */
export async function toolboxExec(
command: string,
timeoutSec = 300,
requestedName?: string,
): Promise<string> {
const selected = selectRunningToolbox(await discoverToolboxContainers(), requestedName);
try {
const proc = spawn(
["/usr/bin/docker", "exec", selected.name, "/bin/sh", "-lc", command],
{ stdout: "pipe", stderr: "pipe", cwd: "/" },
);
const timeout = setTimeout(() => proc.kill(), timeoutSec * 1000);
const [stdout, stderr, code] = await Promise.all([
readStreamLimited(proc.stdout, 100_000),
readStreamLimited(proc.stderr, 20_000),
proc.exited,
]);
clearTimeout(timeout);
return JSON.stringify({
container: selected.name,
exit_code: code,
stdout: sanitizeLogOutput(stdout.text.trim(), 100_000),
stderr: sanitizeLogOutput(stderr.text.trim(), 20_000),
truncated: stdout.truncated || stderr.truncated,
});
} catch (error) {
throw new Error(`toolbox execution failed: ${String(error)}`);
}
}
/**
* Delegiert eine Write-Operation an den PHP-Helper.
*/
export async function runPhpHelper(
action: string,
...args: string[]
): Promise<string> {
const helper = PHP_HELPER;
if (!(await Bun.file(helper).exists())) {
throw new Error(`PHP helper not found: ${helper}`);
}
return runArgv(`php_helper:${action}`, ["/usr/bin/php", helper, action, ...args], 300);
}
export async function runStatusHelper(action: string, ...args: string[]): Promise<string> {
if (!(await Bun.file(STATUS_HELPER).exists())) {
throw new Error(`Status helper not found: ${STATUS_HELPER}`);
}
return runArgv(`status_helper:${action}`, ["/usr/bin/php", STATUS_HELPER, action, ...args], 60);
}
// ── Validierung ─────────────────────────────────────────────────────────
export function validateName(value: unknown, field: string): string {
if (typeof value !== "string" || value === "") {
throw new Error(`${field} is required`);
}
if (!/^[a-zA-Z0-9._-]{1,128}$/.test(value)) {
throw new Error(`Invalid ${field}: ${value}`);
}
return value;
}
// ── JSON-Lines ──────────────────────────────────────────────────────────
export function jsonLines(text: string): Record<string, unknown>[] {
const result: Record<string, unknown>[] = [];
for (const line of text.split("\n")) {
const trimmed = line.trim();
if (trimmed === "") continue;
try {
result.push(JSON.parse(trimmed));
} catch {
// skip malformed lines
}
}
return result;
}
// ── Host-Adressen ───────────────────────────────────────────────────────
export interface HostAddresses {
ipv4: string;
ipv6: string;
public_ipv6: string;
}
export function hostAddresses(raw: string): HostAddresses {
const result: HostAddresses = { ipv4: "", ipv6: "", public_ipv6: "" };
let data: unknown;
try {
data = JSON.parse(raw);
} catch {
return result;
}
if (!Array.isArray(data)) return result;
for (const iface of data as Record<string, unknown>[]) {
const ifname = (iface["ifname"] as string) ?? "";
if (ifname === "lo") continue;
const addrInfo = (iface["addr_info"] as Record<string, unknown>[]) ?? [];
for (const addr of addrInfo) {
const local = (addr["local"] as string) ?? "";
const family = (addr["family"] as string) ?? "";
if (family === "inet") {
if (local !== "127.0.0.1" && local !== "0.0.0.0" && result.ipv4 === "") {
result.ipv4 = local;
}
} else if (family === "inet6") {
if (local.startsWith("fe80") && result.ipv6 === "") {
result.ipv6 = local;
}
if (
!local.startsWith("fe80") &&
!local.startsWith("fd") &&
!local.startsWith("fc") &&
local !== "::1" &&
result.public_ipv6 === ""
) {
result.public_ipv6 = local;
}
}
}
}
return result;
}
// ── TCP Probe ───────────────────────────────────────────────────────────
export interface ProbeResult {
reachable: boolean;
latency_ms?: number;
error?: string;
}
export function tcpProbe(
host: string,
port: number,
family: 4 | 6,
timeoutSec: number,
): Promise<ProbeResult> {
return new Promise((resolve) => {
if (host === "") {
resolve({ reachable: false, error: "no host address" });
return;
}
const start = Date.now();
const socket: Socket = createConnection({
host,
port,
family,
timeout: timeoutSec * 1000,
});
const done = (result: ProbeResult) => {
socket.destroy();
resolve(result);
};
socket.on("connect", () => {
const elapsed = Date.now() - start;
done({ reachable: true, latency_ms: Math.round(elapsed * 10) / 10 });
});
socket.on("timeout", () => {
done({ reachable: false, error: "timeout" });
});
socket.on("error", (err) => {
done({ reachable: false, error: err.message });
});
});
}
// ── Log Sanitize ────────────────────────────────────────────────────────
export function sanitizeLogOutput(text: string, maxChars = 50000): string {
// Entferne ANSI-Escape-Sequenzen
let result = text.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "");
// Entferne andere Control-Chars (außer \n, \r, \t)
result = result.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
// Häufige Secret-Formate redigieren. Das ist bewusst nur eine zusätzliche
// Schutzschicht; Container-Logs können weiterhin sensible Nutzdaten
// enthalten und sollten nur gezielt sowie mit kleinem `tail` gelesen werden.
result = result.replace(
/((?:api[_-]?key|token|secret|password|passwd|authorization|cookie)\s*[=:]\s*)([^\s,;]+)/gi,
"$1[REDACTED]",
);
result = result.replace(
/(\"(?:api[_-]?key|token|secret|password|passwd|authorization|cookie)\"\s*:\s*\")[^\"]*(\")/gi,
"$1[REDACTED]$2",
);
// Begrenze Länge
if (result.length > maxChars) {
result = "... [truncated] ..." + result.slice(-maxChars);
}
return result;
}
// ── Compact Container Inspect ───────────────────────────────────────────
export async function compactContainerInspect(
container: string,
detail: "summary" | "full" = "summary",
): Promise<string> {
const raw = await dockerExec(`inspect --type container -- ${container}`);
let data: unknown;
try {
data = JSON.parse(raw);
} catch {
return raw;
}
if (Array.isArray(data)) data = data[0];
const d = data as Record<string, any>;
const compact: Record<string, unknown> = {
Id: (d.Id ?? "").slice(0, 12),
Name: d.Name ?? "",
State: {
Status: d.State?.Status ?? "",
Running: d.State?.Running ?? false,
Pid: d.State?.Pid ?? 0,
ExitCode: d.State?.ExitCode ?? 0,
},
Image: d.Config?.Image ?? "",
NetworkMode: d.HostConfig?.NetworkMode ?? "",
Ports: d.NetworkSettings?.Ports ?? [],
RestartCount: d.RestartCount ?? 0,
Created: d.Created ?? "",
};
if (detail === "full") {
// Environment-Werte enthalten sehr häufig API-Keys, Passwörter und
// interne URLs. Für Diagnosezwecke reichen die Variablennamen.
compact.EnvNames = (d.Config?.Env ?? []).map((entry: unknown) =>
typeof entry === "string" ? entry.split("=", 1)[0] : "",
).filter((name: string) => name !== "");
compact.Mounts = (d.Mounts ?? []).map((m: any) => ({
Type: m.Type ?? "",
Source: m.Source ?? "",
Destination: m.Destination ?? "",
}));
}
return JSON.stringify(compact);
}
// ── Container Runtime Summary ───────────────────────────────────────────
export interface ContainerListOptions {
state?: "all" | "running" | "stopped" | "created" | "exited";
includeStats?: boolean;
detail?: "compact" | "full";
}
export async function containerRuntimeSummary(
options: ContainerListOptions = {},
): Promise<string> {
const stateFilter = options.state ?? "all";
const includeStats = options.includeStats ?? false;
const detail = options.detail ?? "compact";
const allowedStates = new Set(["all", "running", "stopped", "created", "exited"]);
if (!allowedStates.has(stateFilter)) {
throw new Error("state must be one of: all, running, stopped, created, exited");
}
if (detail !== "compact" && detail !== "full") {
throw new Error("detail must be compact or full");
}
const ps = await dockerExec(`ps -a --format '{{json .}}'`);
const containers = jsonLines(ps);
const runningIds = containers
.filter((c) => c["State"] === "running")
.map((c) => (c["ID"] as string) ?? "")
.filter((id) => id !== "");
const stats: Record<string, Record<string, unknown>> = {};
if (includeStats && runningIds.length > 0) {
const statsRaw = await dockerExec(`stats --no-stream --format '{{json .}}'`);
for (const s of jsonLines(statsRaw)) {
stats[(s["ID"] as string) ?? ""] = s;
}
}
const stateCounts: Record<string, number> = {};
for (const c of containers) {
const state = String(c["State"] ?? "unknown");
stateCounts[state] = (stateCounts[state] ?? 0) + 1;
}
const matchesState = (c: Record<string, unknown>) => {
const state = String(c["State"] ?? "");
if (stateFilter === "all") return true;
if (stateFilter === "stopped") return state !== "running";
return state === stateFilter;
};
const result = containers.filter(matchesState).map((c) => {
const id = (c["ID"] as string) ?? "";
const entry: Record<string, unknown> = {
name: c["Names"] ?? "",
status: c["Status"] ?? "",
state: c["State"] ?? "",
};
if (detail === "full") {
entry.id = id.slice(0, 12);
entry.image = c["Image"] ?? "";
entry.ports = c["Ports"] ?? "";
}
if (includeStats && stats[id]) {
entry.cpu_percent = stats[id]["CPUPerc"] ?? "";
entry.mem_usage = stats[id]["MemUsage"] ?? "";
entry.mem_percent = stats[id]["MemPerc"] ?? "";
entry.net_io = stats[id]["NetIO"] ?? "";
entry.block_io = stats[id]["BlockIO"] ?? "";
}
return entry;
});
return JSON.stringify({
schema_version: "1.1",
container_count: result.length,
returned_count: result.length,
total_count: containers.length,
running_count: stateCounts["running"] ?? 0,
stopped_count: containers.length - (stateCounts["running"] ?? 0),
state_counts: stateCounts,
filter: { state: stateFilter, include_stats: includeStats, detail },
containers: result,
});
}
// ── Compact Network Inventory ───────────────────────────────────────────
export async function compactNetworkInventory(): Promise<string> {
const networksRaw = await dockerExec(`network ls --no-trunc --format '{{json .}}'`);
const networks = jsonLines(networksRaw);
const result = [];
for (const n of networks) {
const entry: Record<string, unknown> = {
name: n["Name"] ?? "",
id: ((n["ID"] as string) ?? "").slice(0, 12),
driver: n["Driver"] ?? "",
scope: n["Scope"] ?? "",
};
const inspect = await dockerExec(
`network inspect --format '{{len .Containers}}' ${n["Name"]}`,
);
entry.container_count = parseInt(inspect.trim(), 10) || 0;
result.push(entry);
}
return JSON.stringify({
schema_version: "1.0",
network_count: result.length,
networks: result,
});
}
// ── Analyze Container Logs ──────────────────────────────────────────────
export async function analyzeContainerLogs(
severity: string,
container: string | null,
since: string,
scanTail: number,
maxResults: number,
focusTerms: string[] = [],
): Promise<string> {
const severityLevels: Record<string, string[]> = {
error: ["error", "fatal", "panic", "exception", "traceback", "critical"],
warn: ["warn", "warning", "deprecated"],
info: ["info", "started", "listening", "ready"],
};
const patterns = severityLevels[severity] ?? severityLevels["error"];
const regex = new RegExp(`(${patterns.join("|")})`, "i");
const normalisedFocusTerms = focusTerms
.map((term) => term.trim().toLowerCase())
.filter((term) => term.length > 0)
.slice(0, 12);
// Ziel-Container: einzelner Container oder alle laufenden
let targets: string[];
if (container) {
targets = [container];
} else {
const psRaw = await dockerExec("ps --format '{{.Names}}'", 30);
targets = psRaw
.split("\n")
.map((l) => l.trim())
.filter((l) => l.length > 0);
}
const matches: { pattern: string; line: string; container: string }[] = [];
const counts: Record<string, number> = {};
const containersWithMatches: Record<string, number> = {};
for (const name of targets) {
const logCmd = `logs --timestamps --since ${since} --tail ${scanTail} ${name}`;
const raw = await dockerExec(logCmd, 120);
let containerMatches = 0;
for (const line of raw.split("\n")) {
if (
normalisedFocusTerms.length > 0
&& !normalisedFocusTerms.some((term) => line.toLowerCase().includes(term))
) {
continue;
}
const m = line.match(regex);
if (m) {
const key = m[1].toLowerCase();
counts[key] = (counts[key] ?? 0) + 1;
containerMatches++;
if (matches.length < maxResults) {
matches.push({ pattern: m[1], line: line.trim().slice(0, 300), container: name });
}
}
}
if (containerMatches > 0) containersWithMatches[name] = containerMatches;
}
return JSON.stringify({
schema_version: "1.0",
severity,
container,
since,
scan_tail: scanTail,
focus_terms: normalisedFocusTerms,
total_matches: Object.values(counts).reduce((a, b) => a + b, 0),
pattern_counts: counts,
containers_with_matches: containersWithMatches,
sample_matches: matches,
});
}
// ── Dualstack LAN Probe ─────────────────────────────────────────────────
export async function probeDualstack(
host: string,
port: number,
timeoutSec: number,
): Promise<string> {
const [ipv4, ipv6] = await Promise.all([
tcpProbe(host, port, 4, timeoutSec),
tcpProbe(host, port, 6, timeoutSec),
]);
return JSON.stringify({ host, port, ipv4, ipv6 });
}
// ── Audit All TCP Endpoints ─────────────────────────────────────────────
export async function auditAllTcpEndpoints(
timeoutSec: number,
includeAllEndpoints = false,
): Promise<string> {
const inventoryCmd =
'ids=$(docker ps -aq); [ -z "$ids" ] || docker inspect --type container --format \'{"ID":{{json .Id}},"Name":{{json .Name}},"Image":{{json .Config.Image}},"Status":{{json .State.Status}},"Running":{{json .State.Running}},"Health":{{json (index .State "Health")}},"NetworkMode":{{json .HostConfig.NetworkMode}},"ExposedPorts":{{json (index .Config "ExposedPorts")}},"Ports":{{json .NetworkSettings.Ports}}}\' $ids';
const containersRaw = await runLocal("audit", inventoryCmd, 60);
const containers = jsonLines(containersRaw);
const hostCmd =
"printf '%s\\n' '--- IPv4/IPv6 addresses ---'; ip -j address show; printf '%s\\n' '--- Listening sockets ---'; ss -H -lntup";
const hostRaw = await runLocal("audit", hostCmd, 30);
const addrs = hostAddresses(hostRaw);
const inactive: { container: string; status: string }[] = [];
const noTcp: { container: string; mode: string }[] = [];
const udp: { container: string; container_port: string }[] = [];
const endpoints: {
container: string;
mode: string;
container_port: string;
host_port: number;
ipv4_reachable?: boolean;
ipv6_reachable?: boolean;
classification?: string;
}[] = [];
const endpointKeys = new Set<string>();
for (const item of containers) {
const name = ((item["Name"] as string) ?? "").replace(/^\//, "");
if (!(item["Running"] as boolean)) {
inactive.push({ container: name, status: (item["Status"] as string) ?? "" });
continue;
}
const mode = (item["NetworkMode"] as string) ?? "unknown";
let foundTcp = false;
const ports = (item["Ports"] as Record<string, { HostPort?: string }[]>) ?? {};
for (const [containerPort, bindings] of Object.entries(ports)) {
const protocol = containerPort.split("/").pop() ?? "";
if (protocol === "udp" && bindings.length > 0) {
udp.push({ container: name, container_port: containerPort });
continue;
}
if (protocol !== "tcp" || bindings.length === 0) continue;
for (const binding of bindings) {
if (binding.HostPort) {
const key = `${name}:${binding.HostPort}:${containerPort}`;
if (!endpointKeys.has(key)) {
endpointKeys.add(key);
endpoints.push({
container: name,
mode,
container_port: containerPort,
host_port: parseInt(binding.HostPort, 10),
});
}
foundTcp = true;
}
}
}
if (mode !== "host" && !foundTcp) {
noTcp.push({ container: name, mode });
}
}
const classifications = { dualstack: 0, "ipv4-only": 0, "ipv6-only": 0, unreachable: 0 };
for (const ep of endpoints) {
const [v4, v6] = await Promise.all([
tcpProbe(addrs.ipv4, ep.host_port, 4, timeoutSec),
tcpProbe(addrs.ipv6, ep.host_port, 6, timeoutSec),
]);
ep.ipv4_reachable = v4.reachable;
ep.ipv6_reachable = v6.reachable;
ep.classification =
v4.reachable && v6.reachable
? "dualstack"
: v4.reachable
? "ipv4-only"
: v6.reachable
? "ipv6-only"
: "unreachable";
classifications[ep.classification as keyof typeof classifications]++;
}
const issueEndpoints = endpoints.filter((e) => e.classification !== "dualstack");
const result: Record<string, unknown> = {
schema_version: "2.0",
targets: { ipv4: addrs.ipv4, lan_ipv6: addrs.ipv6 },
counts: {
containers_total: containers.length,
tcp_endpoints_total: endpoints.length,
tcp_dualstack: classifications.dualstack,
tcp_ipv4_only: classifications["ipv4-only"],
tcp_ipv6_only: classifications["ipv6-only"],
tcp_unreachable: classifications.unreachable,
tcp_problem_endpoints: issueEndpoints.length,
},
problem_endpoints_only: issueEndpoints,
inactive_containers: inactive,
running_without_published_tcp: noTcp,
task_complete: true,
};
if (includeAllEndpoints) {
result.all_tcp_endpoints = endpoints;
}
return JSON.stringify(result);
}
// ── Host State ──────────────────────────────────────────────────────────
export async function hostState(): Promise<string> {
return runLocal(
"host_state",
"printf '%s\\n' '--- IPv4/IPv6 addresses ---'; ip -j address show; printf '%s\\n' '--- IPv4 routes ---'; ip -j -4 route show; printf '%s\\n' '--- IPv6 routes ---'; ip -j -6 route show; printf '%s\\n' '--- Listening sockets ---'; ss -H -lntup",
30,
);
}
// ── Connection Test ─────────────────────────────────────────────────────
export async function connectionTest(): Promise<string> {
return runLocal(
"connection_test",
"id; printf 'hostname='; hostname; printf 'kernel='; uname -sr; printf 'unraid='; cat /etc/unraid-version",
15,
);
}
// ── Community Applications ─────────────────────────────────────────────
const CA_FEED_URL = "https://ca.unraid.net/assets/feed/applicationFeed.json";
const CA_CACHE_MS = 15 * 60 * 1000;
interface CaConfig {
"@attributes"?: Record<string, unknown>;
value?: unknown;
}
interface CaApp {
Name?: unknown;
Repository?: unknown;
Network?: unknown;
Privileged?: unknown;
Overview?: unknown;
CategoryList?: unknown;
TemplateURL?: unknown;
Repo?: unknown;
Project?: unknown;
Support?: unknown;
Config?: unknown;
downloads?: unknown;
stars?: unknown;
}
let caCache: { loadedAt: number; updated?: unknown; apps: CaApp[] } | null = null;
const caApprovalTickets = new Map<string, { fingerprint: string; expiresAt: number }>();
function caText(value: unknown, max = 500): string {
if (typeof value !== "string") return "";
return value
.replace(/\[br\]/gi, " ")
.replace(/\[(?:\/?(?:b|i|u|span|li|font|center|url)[^\]]*)\]/gi, " ")
.replace(/&#xD;|&amp;/gi, " ")
.replace(/\s+/g, " ")
.trim()
.slice(0, max);
}
function caAppId(app: CaApp): string {
return createHash("sha256").update(String(app.TemplateURL ?? "")).digest("hex").slice(0, 16);
}
async function caFeed(): Promise<{ updated?: unknown; apps: CaApp[] }> {
if (caCache && Date.now() - caCache.loadedAt < CA_CACHE_MS) return caCache;
const response = await fetch(CA_FEED_URL, { signal: AbortSignal.timeout(30_000) });
if (!response.ok) throw new Error(`Community Applications feed returned HTTP ${response.status}`);
const contentLength = Number(response.headers.get("content-length") ?? 0);
if (contentLength > 40_000_000) throw new Error("Community Applications feed is unexpectedly large");
const raw = await response.text();
if (raw.length > 40_000_000) throw new Error("Community Applications feed is unexpectedly large");
const parsed = JSON.parse(raw) as Record<string, unknown>;
if (!Array.isArray(parsed["applist"])) throw new Error("Community Applications feed has an invalid schema");
const apps = (parsed["applist"] as CaApp[]).filter((app) =>
typeof app?.Name === "string" &&
typeof app?.Repository === "string" &&
typeof app?.TemplateURL === "string" &&
app.Repository !== "" &&
app.TemplateURL !== ""
);
caCache = { loadedAt: Date.now(), updated: parsed["last_updated"], apps };
return caCache;
}
function caConfigSummary(app: CaApp): Record<string, unknown>[] {
if (!Array.isArray(app.Config)) return [];
return (app.Config as CaConfig[]).slice(0, 64).map((config) => {
const attrs = config?.["@attributes"] ?? {};
const target = caText(attrs["Target"], 200);
const masked = String(attrs["Mask"] ?? "false").toLowerCase() === "true" ||
/(?:api[_-]?key|token|secret|password|passwd)/i.test(target);
return {
type: caText(attrs["Type"], 40),
name: caText(attrs["Name"], 120),
target,
default: masked ? "[MASKED]" : caText(config.value ?? attrs["Default"], 500),
required: String(attrs["Required"] ?? "false").toLowerCase() === "true",
masked,
description: caText(attrs["Description"], 300),
};
});
}
export async function searchCommunityApps(query: string, limit = 10): Promise<string> {
// A pipe-separated query lets an agent check a small set of spelling or
// product-name variants in one bounded feed pass instead of burning one MCP
// round per synonym. Keep the schema itself a plain string; some local
// function-call parsers are unreliable with nested array/object schemas.
const needles = query.split("|").map((term) => term.trim().toLowerCase()).filter(Boolean);
if (needles.length < 1 || needles.length > 5) {
throw new Error("query must contain 1-5 terms separated by |");
}
if (query.length > 300 || needles.some((needle) => needle.length < 2 || needle.length > 100)) {
throw new Error("each query term must contain 2-100 characters (300 total)");
}
limit = Math.max(1, Math.min(25, Math.floor(limit)));
const feed = await caFeed();
const scored = feed.apps.map((app) => {
const name = caText(app.Name, 200);
const repo = caText(app.Repository, 300);
const extra = `${caText(app.Overview, 1000)} ${caText(app.Repo, 200)}`.toLowerCase();
const lower = name.toLowerCase();
const matches = needles.map((needle) => {
let score = 0;
if (lower === needle) score += 100;
else if (lower.startsWith(needle)) score += 60;
else if (lower.includes(needle)) score += 40;
if (repo.toLowerCase().includes(needle)) score += 20;
if (extra.includes(needle)) score += 5;
return { needle, score };
}).filter((match) => match.score > 0);
const score = Math.max(0, ...matches.map((match) => match.score));
return { app, score, name, repo, matches };
}).filter((entry) => entry.score > 0)
.sort((a, b) => b.score - a.score || a.name.localeCompare(b.name))
.slice(0, limit);
return JSON.stringify({
schema_version: "1.0",
source: "Unraid Community Applications official feed",
feed_updated: feed.updated ?? null,
query,
query_terms: needles,
result_count: scored.length,
results: scored.map(({ app, name, repo, matches }) => ({
app_id: caAppId(app),
name,
matched_terms: matches.map((match) => match.needle),
image: repo,
network: caText(app.Network, 80),
privileged: String(app.Privileged ?? "false").toLowerCase() === "true",
categories: Array.isArray(app.CategoryList) ? app.CategoryList.slice(0, 12) : [],
overview: caText(app.Overview, 500),
template_repository: caText(app.Repo, 160),
project: caText(app.Project, 500),
support: caText(app.Support, 500),
downloads: Number(app.downloads ?? 0),
stars: Number(app.stars ?? 0),
})),
});
}
async function caFindById(appId: string): Promise<CaApp> {
if (!/^[a-f0-9]{16}$/.test(appId)) throw new Error("Invalid app_id");
const feed = await caFeed();
const app = feed.apps.find((candidate) => caAppId(candidate) === appId);
if (!app) throw new Error("Community Applications entry no longer exists");
const url = new URL(String(app.TemplateURL));
if (url.protocol !== "https:") throw new Error("Only HTTPS Community Applications templates are accepted");
if (url.hostname === "localhost" || url.hostname.endsWith(".local")) throw new Error("Private template hosts are rejected");
return app;
}
function normalizeCaOverrides(value: unknown): Record<string, string> {
if (value === undefined || value === null) return {};
if (typeof value !== "object" || Array.isArray(value)) throw new Error("overrides must be an object");
const entries = Object.entries(value as Record<string, unknown>);
if (entries.length > 32) throw new Error("At most 32 overrides are allowed");
const normalized: Record<string, string> = {};
for (const [key, item] of entries.sort(([a], [b]) => a.localeCompare(b))) {
if (!/^[A-Za-z0-9_./:-]{1,200}$/.test(key)) throw new Error(`Invalid override target: ${key}`);
if (typeof item !== "string" || item.length > 4096 || /[\x00-\x08\x0b\x0c\x0e-\x1f]/.test(item)) {
throw new Error(`Invalid override value for ${key}`);
}
normalized[key] = item;
}
return normalized;
}
function caFingerprint(appId: string, containerName: string, overrides: Record<string, string>, start: boolean): string {
return createHash("sha256").update(JSON.stringify({ appId, containerName, overrides, start })).digest("hex");
}
export async function previewCommunityAppInstall(
appId: string,
containerName: string,
rawOverrides: unknown,
start: boolean,
): Promise<string> {
containerName = validateName(containerName, "container_name");
const overrides = normalizeCaOverrides(rawOverrides);
const app = await caFindById(appId);
const configuration = caConfigSummary(app);
const allowedTargets = new Set(configuration.map((item) => String(item["target"] ?? "")));
for (const target of Object.keys(overrides)) {
if (!allowedTargets.has(target)) throw new Error(`Override target is not present in the CA template: ${target}`);
}
const names = (await dockerExec("ps -a --format '{{.Names}}'", 30)).split("\n").map((x) => x.trim());
if (names.includes(containerName)) throw new Error(`Container already exists: ${containerName}`);
if (await Bun.file(`/boot/config/plugins/dockerMan/templates-user/my-${containerName}.xml`).exists()) {
throw new Error(`Unraid user template already exists: ${containerName}`);
}
const ticket = randomUUID();
const expiresAt = Date.now() + 10 * 60 * 1000;
caApprovalTickets.set(ticket, { fingerprint: caFingerprint(appId, containerName, overrides, start), expiresAt });
for (const [key, value] of caApprovalTickets) {
if (value.expiresAt < Date.now()) caApprovalTickets.delete(key);
}
return JSON.stringify({
schema_version: "1.0",
action: "preview-only",
app: { app_id: appId, name: caText(app.Name, 200), image: caText(app.Repository, 300) },
container_name: containerName,
network: caText(app.Network, 80),
privileged: String(app.Privileged ?? "false").toLowerCase() === "true",
configuration,
requested_overrides: Object.entries(overrides).map(([target, value]) => ({
target,
value: /(?:api[_-]?key|token|secret|password|passwd)/i.test(target) ? "[REDACTED]" : value,
})),
start_after_install: start,
writes_user_template: `/boot/config/plugins/dockerMan/templates-user/my-${containerName}.xml`,
approval_ticket: ticket,
approval_expires_in_seconds: 600,
next_step: "Review this preview. Only after explicit user approval call unraid_ca_install with exactly the same app_id, container_name, overrides and start_after_install plus confirm=true and this approval_ticket.",
});
}
export async function installCommunityApp(
appId: string,
containerName: string,
rawOverrides: unknown,
start: boolean,
confirm: boolean,
ticket: string,
): Promise<string> {
containerName = validateName(containerName, "container_name");
const overrides = normalizeCaOverrides(rawOverrides);
const approval = caApprovalTickets.get(ticket);
const fingerprint = caFingerprint(appId, containerName, overrides, start);
if (!confirm || !approval || approval.expiresAt < Date.now() || approval.fingerprint !== fingerprint) {
throw new Error("Approval ticket missing, expired, or does not match this exact change. Run unraid_ca_install_preview, show its preview to the user, then repeat unchanged with confirm=true and the returned approval_ticket.");
}
caApprovalTickets.delete(ticket);
const app = await caFindById(appId);
const result = await runPhpHelper(
"ca-install",
String(app.TemplateURL),
containerName,
JSON.stringify(overrides),
start ? "true" : "false",
);
return JSON.stringify({
schema_version: "1.0",
ok: true,
app: caText(app.Name, 200),
image: caText(app.Repository, 300),
container_name: containerName,
started: start,
gui_managed: true,
template: `/boot/config/plugins/dockerMan/templates-user/my-${containerName}.xml`,
helper_result: result.slice(0, 3000),
});
}