Files
9router/src/app/api/settings/route.js
T
gen 5a015e5b4d feat(proxy): add outbound HTTP proxy support for OAuth + provider requests
- Patch Node fetch via undici ProxyAgent when HTTP_PROXY/HTTPS_PROXY/ALL_PROXY is set
- Ensure proxy patch is loaded for both chat pipeline and OAuth token exchange
- Add Dashboard Settings → Network to edit outbound proxy and apply immediately
- Persist outbound proxy settings in local db and initialize on server startup
- Move proxy helpers to src/lib/network/ for better structure
- Rename src/proxy.js → src/dashboardGuard.js to avoid naming confusion
- Re-apply proxy env after DB import
- Fix: close old dispatcher on proxy URL change to prevent connection pool leak
- Fix: idempotency guard to avoid patching globalThis.fetch multiple times

Made-with: Cursor
2026-02-28 10:11:53 +07:00

73 lines
2.4 KiB
JavaScript

import { NextResponse } from "next/server";
import { getSettings, updateSettings } from "@/lib/localDb";
import { applyOutboundProxyEnv } from "@/lib/network/outboundProxy";
import bcrypt from "bcryptjs";
export async function GET() {
try {
const settings = await getSettings();
const { password, ...safeSettings } = settings;
const enableRequestLogs = process.env.ENABLE_REQUEST_LOGS === "true";
return NextResponse.json({
...safeSettings,
enableRequestLogs,
hasPassword: !!password
});
} catch (error) {
console.log("Error getting settings:", error);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
export async function PATCH(request) {
try {
const body = await request.json();
// If updating password, hash it
if (body.newPassword) {
const settings = await getSettings();
const currentHash = settings.password;
// Verify current password if it exists
if (currentHash) {
if (!body.currentPassword) {
return NextResponse.json({ error: "Current password required" }, { status: 400 });
}
const isValid = await bcrypt.compare(body.currentPassword, currentHash);
if (!isValid) {
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
}
} else {
// First time setting password, no current password needed
// Allow empty currentPassword or default "123456"
if (body.currentPassword && body.currentPassword !== "123456") {
return NextResponse.json({ error: "Invalid current password" }, { status: 401 });
}
}
const salt = await bcrypt.genSalt(10);
body.password = await bcrypt.hash(body.newPassword, salt);
delete body.newPassword;
delete body.currentPassword;
}
const settings = await updateSettings(body);
// Apply outbound proxy settings immediately (no restart required)
if (
Object.prototype.hasOwnProperty.call(body, "outboundProxyEnabled") ||
Object.prototype.hasOwnProperty.call(body, "outboundProxyUrl") ||
Object.prototype.hasOwnProperty.call(body, "outboundNoProxy")
) {
applyOutboundProxyEnv(settings);
}
const { password, ...safeSettings } = settings;
return NextResponse.json(safeSettings);
} catch (error) {
console.log("Error updating settings:", error);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}