/** * POST /api/connect * 测试 SSH 连接是否可用 */ import { NextRequest, NextResponse } from "next/server"; import { testConnection } from "@/lib/ssh"; export async function POST(request: NextRequest) { try { const body = (await request.json()) as { host?: string; port?: number; username?: string; password?: string; privateKey?: string; }; const { host, port = 22, username, password, privateKey } = body; if (!host || !username) { return NextResponse.json( { error: "请填写主机地址和用户名" }, { status: 400 } ); } await testConnection({ host, port: Number(port) || 22, username, password, privateKey, }); return NextResponse.json({ success: true }); } catch (err) { const message = err instanceof Error ? err.message : "连接失败,请检查连接信息"; return NextResponse.json({ error: message }, { status: 400 }); } }