9router/src/app/api/cli-tools/antigravity-mitm/route.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

"use server";
import { NextResponse } from "next/server";
2026-02-22 09:44:11 -05:00
import { getMitmStatus, startMitm, stopMitm, getCachedPassword, setCachedPassword, loadEncryptedPassword, initDbHooks } from "@/mitm/manager";
import { getSettings, updateSettings } from "@/lib/localDb";
// Inject DB hooks so manager.js (CJS) can persist settings without dynamic import issues
initDbHooks(getSettings, updateSettings);
// GET - Check MITM status
export async function GET() {
try {
const status = await getMitmStatus();
return NextResponse.json({
running: status.running,
pid: status.pid || null,
dnsConfigured: status.dnsConfigured || false,
certExists: status.certExists || false,
hasCachedPassword: !!getCachedPassword(),
});
} catch (error) {
console.log("Error getting MITM status:", error.message);
return NextResponse.json({ error: "Failed to get MITM status" }, { status: 500 });
}
}
// POST - Start MITM proxy
export async function POST(request) {
try {
const { apiKey, sudoPassword } = await request.json();
const isWin = process.platform === "win32";
2026-02-22 09:44:11 -05:00
// Priority: request password → in-memory cache → encrypted db
const pwd = sudoPassword || getCachedPassword() || await loadEncryptedPassword() || "";
if (!apiKey || (!isWin && !pwd)) {
return NextResponse.json(
{ error: isWin ? "Missing apiKey" : "Missing apiKey or sudoPassword" },
{ status: 400 }
);
}
const result = await startMitm(apiKey, pwd);
if (!isWin) setCachedPassword(pwd);
return NextResponse.json({
success: true,
running: result.running,
pid: result.pid,
2026-03-03 03:52:20 -05:00
steps: result.steps || { cert: true, server: true, dns: true },
});
} catch (error) {
console.log("Error starting MITM:", error.message);
return NextResponse.json({ error: error.message || "Failed to start MITM proxy" }, { status: 500 });
}
}
// DELETE - Stop MITM proxy
export async function DELETE(request) {
try {
const { sudoPassword } = await request.json();
const isWin = process.platform === "win32";
2026-02-22 09:44:11 -05:00
const pwd = sudoPassword || getCachedPassword() || await loadEncryptedPassword() || "";
if (!isWin && !pwd) {
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
}
await stopMitm(pwd);
if (!isWin && sudoPassword) setCachedPassword(sudoPassword);
return NextResponse.json({ success: true, running: false });
} catch (error) {
console.log("Error stopping MITM:", error.message);
return NextResponse.json({ error: error.message || "Failed to stop MITM proxy" }, { status: 500 });
}
}