- Introduce default MITM router base URL and update related components to handle it.

- Add input for MITM router base URL in MitmServerCard component.
This commit is contained in:
decolua 2026-04-04 11:25:58 +07:00
parent 2e740ad7e4
commit d84489dba4
11 changed files with 559 additions and 266 deletions

View file

@ -17,6 +17,25 @@ import { getSettings, updateSettings } from "@/lib/localDb";
initDbHooks(getSettings, updateSettings);
const DEFAULT_MITM_ROUTER_BASE = "http://localhost:20128";
function normalizeMitmRouterBaseUrlInput(input) {
if (input == null || String(input).trim() === "") {
return DEFAULT_MITM_ROUTER_BASE;
}
const t = String(input).trim().replace(/\/+$/, "");
let u;
try {
u = new URL(t);
} catch {
throw new Error("Invalid MITM router URL");
}
if (u.protocol !== "http:" && u.protocol !== "https:") {
throw new Error("MITM router URL must use http or https");
}
return t;
}
const isWin = process.platform === "win32";
function getPassword(provided) {
@ -37,6 +56,7 @@ function checkIsAdmin() {
export async function GET() {
try {
const status = await getMitmStatus();
const settings = await getSettings();
return NextResponse.json({
running: status.running,
pid: status.pid || null,
@ -45,6 +65,9 @@ export async function GET() {
dnsStatus: status.dnsStatus || {},
hasCachedPassword: !!getCachedPassword() || !!(await loadEncryptedPassword()),
isAdmin: checkIsAdmin(),
mitmRouterBaseUrl:
(settings.mitmRouterBaseUrl && String(settings.mitmRouterBaseUrl).trim()) ||
DEFAULT_MITM_ROUTER_BASE,
});
} catch (error) {
console.log("Error getting MITM status:", error.message);
@ -55,7 +78,7 @@ export async function GET() {
// POST - Start MITM server (cert + server, no DNS)
export async function POST(request) {
try {
const { apiKey, sudoPassword } = await request.json();
const { apiKey, sudoPassword, mitmRouterBaseUrl } = await request.json();
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
if (!apiKey || (!isWin && !pwd)) {
@ -65,6 +88,18 @@ export async function POST(request) {
);
}
if (mitmRouterBaseUrl !== undefined && mitmRouterBaseUrl !== null) {
try {
const normalized = normalizeMitmRouterBaseUrlInput(mitmRouterBaseUrl);
await updateSettings({ mitmRouterBaseUrl: normalized });
} catch (e) {
return NextResponse.json(
{ error: e.message || "Invalid MITM router URL" },
{ status: 400 },
);
}
}
const result = await startServer(apiKey, pwd);
if (!isWin) setCachedPassword(pwd);