init: webwatcher 服务器监控面板

This commit is contained in:
selfrelease
2026-07-27 09:32:21 +08:00
commit 7068191d3a
25 changed files with 5630 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import { NextRequest, NextResponse } from "next/server";
import { execRemote } from "@/lib/ssh";
export const dynamic = "force-dynamic";
/**
* POST /api/control
* 对服务器上的 PM2 进程或系统执行控制操作
*/
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const { host, port, username, password, privateKey, action, target } = body as {
host: string;
port?: number;
username: string;
password?: string;
privateKey?: string;
action: string;
target: string | number;
};
if (!host || !username) {
return NextResponse.json({ error: "缺少必要参数" }, { status: 400 });
}
const PM2_ALLOWED = ["restart", "stop", "start", "delete", "reload"];
const SVC_ALLOWED = ["svc-restart", "svc-stop", "svc-start", "svc-reload"];
let cmd: string;
if (PM2_ALLOWED.includes(action)) {
cmd = `pm2 ${action} ${target} --no-color 2>&1 && echo __OK__`;
} else if (SVC_ALLOWED.includes(action)) {
const svcAction = action.replace("svc-", "");
cmd = `systemctl ${svcAction} ${target}.service 2>&1 && echo __OK__`;
} else {
return NextResponse.json({ error: "不支持的操作" }, { status: 400 });
}
const output = await execRemote(
{ host, port: port ?? 22, username, password, privateKey },
cmd
);
if (!output.includes("__OK__")) {
return NextResponse.json(
{ error: output.trim() || "命令执行失败" },
{ status: 500 }
);
}
return NextResponse.json({ success: true, output: output.replace("__OK__", "").trim() });
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : "操作失败" },
{ status: 500 }
);
}
}