feat(proxy): add proxy pool and per-connection binding + strictProxy support
- Centralize proxy management with reusable proxy pools - Per-connection proxy binding with legacy fallback - Add strictProxy option: fail hard instead of silently falling back to direct - Resolve alicode-intl conflict: keep alicode-intl support + proxy support Made-with: Cursor
This commit is contained in:
1 parent
4c469291a1
commit
880f4eca91
22 files changed
+1811
-146
No files matched your search
@@ -1,5 +1,63 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getProviderConnectionById, updateProviderConnection, deleteProviderConnection } from "@/models";
|
||||
import {
|
||||
getProviderConnectionById,
|
||||
getProxyPoolById,
|
||||
updateProviderConnection,
|
||||
deleteProviderConnection,
|
||||
} from "@/models";
|
||||
|
||||
function normalizeProxyConfig(body = {}) {
|
||||
const hasAnyProxyField =
|
||||
Object.prototype.hasOwnProperty.call(body, "connectionProxyEnabled") ||
|
||||
Object.prototype.hasOwnProperty.call(body, "connectionProxyUrl") ||
|
||||
Object.prototype.hasOwnProperty.call(body, "connectionNoProxy");
|
||||
|
||||
if (!hasAnyProxyField) return { hasAnyProxyField: false };
|
||||
|
||||
const enabled = body?.connectionProxyEnabled === true;
|
||||
const url = typeof body?.connectionProxyUrl === "string" ? body.connectionProxyUrl.trim() : "";
|
||||
const noProxy = typeof body?.connectionNoProxy === "string" ? body.connectionNoProxy.trim() : "";
|
||||
|
||||
if (enabled && !url) {
|
||||
return {
|
||||
hasAnyProxyField: true,
|
||||
error: "Connection proxy URL is required when connection proxy is enabled",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
hasAnyProxyField: true,
|
||||
connectionProxyEnabled: enabled,
|
||||
connectionProxyUrl: url,
|
||||
connectionNoProxy: noProxy,
|
||||
};
|
||||
}
|
||||
|
||||
async function normalizeProxyPoolUpdate(proxyPoolIdInput) {
|
||||
if (proxyPoolIdInput === undefined) {
|
||||
return { hasProxyPoolField: false, proxyPoolId: null };
|
||||
}
|
||||
|
||||
if (proxyPoolIdInput === null || proxyPoolIdInput === "" || proxyPoolIdInput === "__none__") {
|
||||
return { hasProxyPoolField: true, proxyPoolId: null };
|
||||
}
|
||||
|
||||
const proxyPoolId = String(proxyPoolIdInput).trim();
|
||||
if (!proxyPoolId) {
|
||||
return { hasProxyPoolField: true, proxyPoolId: null };
|
||||
}
|
||||
|
||||
const proxyPool = await getProxyPoolById(proxyPoolId);
|
||||
if (!proxyPool) {
|
||||
return { hasProxyPoolField: true, error: "Proxy pool not found" };
|
||||
}
|
||||
|
||||
return { hasProxyPoolField: true, proxyPoolId };
|
||||
}
|
||||
|
||||
function shouldMergeProviderSpecificData(existing, incoming, hasLegacyProxy, hasProxyPoolField) {
|
||||
return existing !== undefined || incoming !== undefined || hasLegacyProxy || hasProxyPoolField;
|
||||
}
|
||||
|
||||
// GET /api/providers/[id] - Get single connection
|
||||
export async function GET(request, { params }) {
|
||||
@@ -48,6 +106,16 @@ export async function PUT(request, { params }) {
|
||||
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const proxyConfig = normalizeProxyConfig(body);
|
||||
if (proxyConfig.error) {
|
||||
return NextResponse.json({ error: proxyConfig.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const proxyPoolResult = await normalizeProxyPoolUpdate(body.proxyPoolId);
|
||||
if (proxyPoolResult.error) {
|
||||
return NextResponse.json({ error: proxyPoolResult.error }, { status: 400 });
|
||||
}
|
||||
|
||||
const updateData = {};
|
||||
if (name !== undefined) updateData.name = name;
|
||||
if (priority !== undefined) updateData.priority = priority;
|
||||
@@ -58,11 +126,33 @@ export async function PUT(request, { params }) {
|
||||
if (testStatus !== undefined) updateData.testStatus = testStatus;
|
||||
if (lastError !== undefined) updateData.lastError = lastError;
|
||||
if (lastErrorAt !== undefined) updateData.lastErrorAt = lastErrorAt;
|
||||
if (providerSpecificData !== undefined) {
|
||||
|
||||
if (
|
||||
shouldMergeProviderSpecificData(
|
||||
existing.providerSpecificData,
|
||||
providerSpecificData,
|
||||
proxyConfig.hasAnyProxyField,
|
||||
proxyPoolResult.hasProxyPoolField
|
||||
)
|
||||
) {
|
||||
updateData.providerSpecificData = {
|
||||
...(existing.providerSpecificData || {}),
|
||||
...providerSpecificData,
|
||||
...(providerSpecificData || {}),
|
||||
};
|
||||
|
||||
if (proxyConfig.hasAnyProxyField) {
|
||||
updateData.providerSpecificData.connectionProxyEnabled = proxyConfig.connectionProxyEnabled;
|
||||
updateData.providerSpecificData.connectionProxyUrl = proxyConfig.connectionProxyUrl;
|
||||
updateData.providerSpecificData.connectionNoProxy = proxyConfig.connectionNoProxy;
|
||||
}
|
||||
|
||||
if (proxyPoolResult.hasProxyPoolField) {
|
||||
if (proxyPoolResult.proxyPoolId === null) {
|
||||
delete updateData.providerSpecificData.proxyPoolId;
|
||||
} else {
|
||||
updateData.providerSpecificData.proxyPoolId = proxyPoolResult.proxyPoolId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await updateProviderConnection(id, updateData);
|
||||
|
||||
Reference in new issue
Block a user