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
This commit is contained in:
gen
2026-02-28 10:11:53 +07:00
parent 833069caac
commit 5a015e5b4d
14 changed files with 450 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { exportDb, importDb } from "@/lib/localDb";
import { exportDb, getSettings, importDb } from "@/lib/localDb";
import { applyOutboundProxyEnv } from "@/lib/network/outboundProxy";
export async function GET() {
try {
@@ -15,6 +16,15 @@ export async function POST(request) {
try {
const payload = await request.json();
await importDb(payload);
// Ensure proxy settings take effect immediately after a DB import.
try {
const settings = await getSettings();
applyOutboundProxyEnv(settings);
} catch (err) {
console.warn("[Settings][DatabaseImport] Failed to re-apply outbound proxy env:", err);
}
return NextResponse.json({ success: true });
} catch (error) {
console.log("Error importing database:", error);

View File

@@ -0,0 +1,23 @@
import { NextResponse } from "next/server";
import { testProxyUrl } from "@/lib/network/proxyTest";
export async function POST(request) {
try {
const body = await request.json();
const result = await testProxyUrl({
proxyUrl: body?.proxyUrl,
testUrl: body?.testUrl,
timeoutMs: body?.timeoutMs,
});
if (result?.ok) {
return NextResponse.json(result);
}
const status = typeof result?.status === "number" ? result.status : 500;
return NextResponse.json({ ok: false, error: result?.error || "Proxy test failed" }, { status });
} catch (err) {
const message = err?.name === "AbortError" ? "Proxy test timed out" : (err?.message || String(err));
return NextResponse.json({ ok: false, error: message }, { status: 500 });
}
}

View File

@@ -1,5 +1,6 @@
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() {
@@ -53,6 +54,15 @@ export async function PATCH(request) {
}
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) {