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:
decolua
2026-03-09 15:46:06 +07:00
parent 4c469291a1
commit 880f4eca91
22 changed files with 1811 additions and 146 deletions
+90
View File
@@ -0,0 +1,90 @@
import { NextResponse } from "next/server";
import { createProxyPool, getProviderConnections, getProxyPools } from "@/models";
function toBoolean(value) {
if (value === "true") return true;
if (value === "false") return false;
return undefined;
}
function normalizeProxyPoolInput(body = {}) {
const name = typeof body?.name === "string" ? body.name.trim() : "";
const proxyUrl = typeof body?.proxyUrl === "string" ? body.proxyUrl.trim() : "";
const noProxy = typeof body?.noProxy === "string" ? body.noProxy.trim() : "";
const isActive = body?.isActive === undefined ? true : body.isActive === true;
const strictProxy = body?.strictProxy === true;
if (!name) {
return { error: "Name is required" };
}
if (!proxyUrl) {
return { error: "Proxy URL is required" };
}
return { name, proxyUrl, noProxy, isActive, strictProxy };
}
function buildUsageMap(connections = []) {
const usageMap = new Map();
for (const connection of connections) {
const proxyPoolId = connection?.providerSpecificData?.proxyPoolId;
if (!proxyPoolId) continue;
usageMap.set(proxyPoolId, (usageMap.get(proxyPoolId) || 0) + 1);
}
return usageMap;
}
// GET /api/proxy-pools - List proxy pools
export async function GET(request) {
try {
const { searchParams } = new URL(request.url);
const isActive = toBoolean(searchParams.get("isActive"));
const includeUsage = searchParams.get("includeUsage") === "true";
const filter = {};
if (isActive !== undefined) {
filter.isActive = isActive;
}
const proxyPools = await getProxyPools(filter);
if (!includeUsage) {
return NextResponse.json({ proxyPools });
}
const connections = await getProviderConnections();
const usageMap = buildUsageMap(connections);
const enrichedProxyPools = proxyPools.map((pool) => ({
...pool,
boundConnectionCount: usageMap.get(pool.id) || 0,
}));
return NextResponse.json({ proxyPools: enrichedProxyPools });
} catch (error) {
console.log("Error fetching proxy pools:", error);
return NextResponse.json({ error: "Failed to fetch proxy pools" }, { status: 500 });
}
}
// POST /api/proxy-pools - Create proxy pool
export async function POST(request) {
try {
const body = await request.json();
const normalized = normalizeProxyPoolInput(body);
if (normalized.error) {
return NextResponse.json({ error: normalized.error }, { status: 400 });
}
const proxyPool = await createProxyPool(normalized);
return NextResponse.json({ proxyPool }, { status: 201 });
} catch (error) {
console.log("Error creating proxy pool:", error);
return NextResponse.json({ error: "Failed to create proxy pool" }, { status: 500 });
}
}