mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
- Refines the overall structure of the CLI tools and MITM server functionalities.
- Add buildQwenBaseUrl function to construct URLs for Qwen resources. - Update buildProviderUrl to support Qwen model requests. - Enhance token refresh logic to include provider-specific data for Qwen. - Refactor CLI Tools page to exclude MITM tools and streamline model retrieval. - Introduce new components for MITM server management. - Update API routes to handle Qwen-specific resource URLs and improve error handling.
This commit is contained in:
@@ -1,21 +1,37 @@
|
||||
"use server";
|
||||
|
||||
import { NextResponse } from "next/server";
|
||||
import { getMitmStatus, startMitm, stopMitm, getCachedPassword, setCachedPassword, loadEncryptedPassword, initDbHooks } from "@/mitm/manager";
|
||||
import {
|
||||
getMitmStatus,
|
||||
startServer,
|
||||
stopServer,
|
||||
enableToolDNS,
|
||||
disableToolDNS,
|
||||
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
|
||||
const isWin = process.platform === "win32";
|
||||
|
||||
function getPassword(provided) {
|
||||
return provided || getCachedPassword() || null;
|
||||
}
|
||||
|
||||
// GET - Full MITM status (server + per-tool DNS)
|
||||
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,
|
||||
dnsStatus: status.dnsStatus || {},
|
||||
certCoversTools: status.certCoversTools || {},
|
||||
hasCachedPassword: !!getCachedPassword(),
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -24,13 +40,11 @@ export async function GET() {
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Start MITM proxy
|
||||
// POST - Start MITM server (cert + server, no DNS)
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const { apiKey, sudoPassword } = await request.json();
|
||||
const isWin = process.platform === "win32";
|
||||
// Priority: request password → in-memory cache → encrypted db
|
||||
const pwd = sudoPassword || getCachedPassword() || await loadEncryptedPassword() || "";
|
||||
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
|
||||
|
||||
if (!apiKey || (!isWin && !pwd)) {
|
||||
return NextResponse.json(
|
||||
@@ -39,38 +53,64 @@ export async function POST(request) {
|
||||
);
|
||||
}
|
||||
|
||||
const result = await startMitm(apiKey, pwd);
|
||||
const result = await startServer(apiKey, pwd);
|
||||
if (!isWin) setCachedPassword(pwd);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
running: result.running,
|
||||
pid: result.pid,
|
||||
steps: result.steps || { cert: true, server: true, dns: true },
|
||||
});
|
||||
return NextResponse.json({ success: true, running: result.running, pid: result.pid });
|
||||
} catch (error) {
|
||||
console.log("Error starting MITM:", error.message);
|
||||
return NextResponse.json({ error: error.message || "Failed to start MITM proxy" }, { status: 500 });
|
||||
console.log("Error starting MITM server:", error.message);
|
||||
return NextResponse.json({ error: error.message || "Failed to start MITM server" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Stop MITM proxy
|
||||
// DELETE - Stop MITM server (removes all DNS first, then kills server)
|
||||
export async function DELETE(request) {
|
||||
try {
|
||||
const { sudoPassword } = await request.json();
|
||||
const isWin = process.platform === "win32";
|
||||
const pwd = sudoPassword || getCachedPassword() || await loadEncryptedPassword() || "";
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const { sudoPassword } = body;
|
||||
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
|
||||
|
||||
if (!isWin && !pwd) {
|
||||
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
|
||||
}
|
||||
|
||||
await stopMitm(pwd);
|
||||
await stopServer(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 });
|
||||
console.log("Error stopping MITM server:", error.message);
|
||||
return NextResponse.json({ error: error.message || "Failed to stop MITM server" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PATCH - Toggle DNS for a specific tool (enable/disable)
|
||||
export async function PATCH(request) {
|
||||
try {
|
||||
const { tool, action, sudoPassword } = await request.json();
|
||||
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
|
||||
|
||||
if (!tool || !action) {
|
||||
return NextResponse.json({ error: "tool and action required" }, { status: 400 });
|
||||
}
|
||||
if (!isWin && !pwd) {
|
||||
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (action === "enable") {
|
||||
await enableToolDNS(tool, pwd);
|
||||
} else if (action === "disable") {
|
||||
await disableToolDNS(tool, pwd);
|
||||
} else {
|
||||
return NextResponse.json({ error: "action must be enable or disable" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!isWin && sudoPassword) setCachedPassword(sudoPassword);
|
||||
|
||||
const status = await getMitmStatus();
|
||||
return NextResponse.json({ success: true, dnsStatus: status.dnsStatus });
|
||||
} catch (error) {
|
||||
console.log("Error toggling DNS:", error.message);
|
||||
return NextResponse.json({ error: error.message || "Failed to toggle DNS" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user