mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix(mitm): gate sudo prompts on server platform (#822)
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
getCachedPassword,
|
||||
setCachedPassword,
|
||||
loadEncryptedPassword,
|
||||
isSudoPasswordRequired,
|
||||
initDbHooks,
|
||||
} from "@/mitm/manager";
|
||||
import { getSettings, updateSettings } from "@/lib/localDb";
|
||||
@@ -40,6 +41,10 @@ function getPassword(provided) {
|
||||
return provided || getCachedPassword() || null;
|
||||
}
|
||||
|
||||
function requiresSudoPassword(pwd) {
|
||||
return !isWin && !pwd && isSudoPasswordRequired();
|
||||
}
|
||||
|
||||
function checkIsAdmin() {
|
||||
if (!isWin) return true;
|
||||
try {
|
||||
@@ -55,13 +60,16 @@ export async function GET() {
|
||||
try {
|
||||
const status = await getMitmStatus();
|
||||
const settings = await getSettings();
|
||||
const hasCachedPassword = !!getCachedPassword() || !!(await loadEncryptedPassword());
|
||||
return NextResponse.json({
|
||||
running: status.running,
|
||||
pid: status.pid || null,
|
||||
certExists: status.certExists || false,
|
||||
certTrusted: status.certTrusted || false,
|
||||
dnsStatus: status.dnsStatus || {},
|
||||
hasCachedPassword: !!getCachedPassword() || !!(await loadEncryptedPassword()),
|
||||
hasCachedPassword,
|
||||
isWin,
|
||||
needsSudoPassword: !isWin && !hasCachedPassword && isSudoPasswordRequired(),
|
||||
isAdmin: checkIsAdmin(),
|
||||
mitmRouterBaseUrl:
|
||||
(settings.mitmRouterBaseUrl && String(settings.mitmRouterBaseUrl).trim()) ||
|
||||
@@ -79,9 +87,9 @@ export async function POST(request) {
|
||||
const { apiKey, sudoPassword, mitmRouterBaseUrl } = await request.json();
|
||||
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
|
||||
|
||||
if (!apiKey || (!isWin && !pwd)) {
|
||||
if (!apiKey || requiresSudoPassword(pwd)) {
|
||||
return NextResponse.json(
|
||||
{ error: isWin ? "Missing apiKey" : "Missing apiKey or sudoPassword" },
|
||||
{ error: !apiKey ? "Missing apiKey" : "Missing sudoPassword" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
@@ -115,7 +123,7 @@ export async function DELETE(request) {
|
||||
const { sudoPassword } = body;
|
||||
const pwd = getPassword(sudoPassword) || await loadEncryptedPassword() || "";
|
||||
|
||||
if (!isWin && !pwd) {
|
||||
if (requiresSudoPassword(pwd)) {
|
||||
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -138,7 +146,7 @@ export async function PATCH(request) {
|
||||
if (!tool || !action) {
|
||||
return NextResponse.json({ error: "tool and action required" }, { status: 400 });
|
||||
}
|
||||
if (!isWin && !pwd) {
|
||||
if (requiresSudoPassword(pwd)) {
|
||||
return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user