Spaces:
Runtime error
Runtime error
File size: 5,719 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | // Node.js-only route: uses child_process, fs, path via mitm/manager
// Dynamic imports prevent Turbopack from statically resolving native modules
export const runtime = "nodejs";
import { NextResponse } from "next/server";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
import { requireCliToolsAuth } from "@/lib/api/requireCliToolsAuth";
import { cliMitmStartSchema, cliMitmStopSchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { resolveApiKey } from "@/shared/services/apiKeyResolver";
import { isRoot } from "@/mitm/systemCommands";
import { isSudoPasswordRequired } from "@/mitm/dns/dnsConfig";
// GET - Check MITM status
export async function GET(request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
try {
const { getMitmStatus, getCachedPassword } = await import("@/mitm/manager.runtime");
const status = await getMitmStatus();
const isWin = process.platform === "win32";
const hasCachedPassword = !!getCachedPassword();
// Probe sudo availability so the UI can hide the password modal on hosts
// where it's unnecessary (Windows, root user, NOPASSWD sudoers, minimal
// containers without sudo). MITM elevation is decided by the server OS,
// not by the browser's user agent — see PR title.
const needsSudoPassword = !isWin && !hasCachedPassword && isSudoPasswordRequired();
return NextResponse.json({
running: status.running,
pid: status.pid || null,
dnsConfigured: status.dnsConfigured || false,
certExists: status.certExists || false,
hasCachedPassword,
isWin,
needsSudoPassword,
});
} catch (error) {
console.log("Error getting MITM status:", sanitizeErrorMessage(error));
return NextResponse.json({ error: "Failed to get MITM status" }, { status: 500 });
}
}
// POST - Start MITM proxy
export async function POST(request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = validateBody(cliMitmStartSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const { apiKey: rawApiKey, keyId: rawKeyId, sudoPassword } = validation.data;
const apiKeyId = rawKeyId ?? null;
const apiKey = await resolveApiKey(apiKeyId, rawApiKey);
if (!apiKey || apiKey === "sk_omniroute") {
return NextResponse.json(
{ error: "Missing apiKey: provide a valid apiKey or a resolvable keyId" },
{ status: 400 }
);
}
const { startMitm, getCachedPassword, setCachedPassword } =
await import("@/mitm/manager.runtime");
const isWin = process.platform === "win32";
const isRootUser = !isWin && isRoot();
const pwd = sudoPassword || getCachedPassword() || "";
// Require a sudo password only when the OS actually needs one. Skips the
// prompt on Windows (UAC), root, NOPASSWD sudoers, and minimal containers
// without sudo on PATH (#822).
if (!isWin && !pwd && !isRootUser && isSudoPasswordRequired()) {
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
}
const result = await startMitm(apiKey, pwd);
if (!isWin && pwd) setCachedPassword(pwd);
return NextResponse.json({
success: true,
running: result.running,
pid: result.pid,
});
} catch (error) {
console.log("Error starting MITM:", sanitizeErrorMessage(error));
return NextResponse.json(
{ error: sanitizeErrorMessage(error) || "Failed to start MITM proxy" },
{ status: 500 }
);
}
}
// DELETE - Stop MITM proxy
export async function DELETE(request) {
const authError = await requireCliToolsAuth(request);
if (authError) return authError;
let rawBody;
try {
rawBody = await request.json();
} catch {
return NextResponse.json(
{
error: {
message: "Invalid request",
details: [{ field: "body", message: "Invalid JSON body" }],
},
},
{ status: 400 }
);
}
try {
const validation = validateBody(cliMitmStopSchema, rawBody);
if (isValidationFailure(validation)) {
return NextResponse.json({ error: validation.error }, { status: 400 });
}
const { sudoPassword } = validation.data;
const { stopMitm, getCachedPassword, setCachedPassword } =
await import("@/mitm/manager.runtime");
const isWin = process.platform === "win32";
const isRootUser = !isWin && isRoot();
const pwd = sudoPassword || getCachedPassword() || "";
// Require a sudo password only when the OS actually needs one. Skips the
// prompt on Windows (UAC), root, NOPASSWD sudoers, and minimal containers
// without sudo on PATH (#822).
if (!isWin && !pwd && !isRootUser && isSudoPasswordRequired()) {
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:", sanitizeErrorMessage(error));
return NextResponse.json(
{ error: sanitizeErrorMessage(error) || "Failed to stop MITM proxy" },
{ status: 500 }
);
}
}
|