File size: 3,729 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import { NextResponse } from "next/server";
import {
deleteProxyPool,
getProviderConnections,
getProxyPoolById,
updateProxyPool,
} from "@/models";
function normalizeProxyPoolUpdate(body = {}) {
const updates = {};
if (Object.prototype.hasOwnProperty.call(body, "name")) {
const name = typeof body?.name === "string" ? body.name.trim() : "";
if (!name) {
return { error: "Name is required" };
}
updates.name = name;
}
if (Object.prototype.hasOwnProperty.call(body, "proxyUrl")) {
const proxyUrl = typeof body?.proxyUrl === "string" ? body.proxyUrl.trim() : "";
if (!proxyUrl) {
return { error: "Proxy URL is required" };
}
updates.proxyUrl = proxyUrl;
}
if (Object.prototype.hasOwnProperty.call(body, "noProxy")) {
updates.noProxy = typeof body?.noProxy === "string" ? body.noProxy.trim() : "";
}
if (Object.prototype.hasOwnProperty.call(body, "isActive")) {
updates.isActive = body?.isActive === true;
}
if (Object.prototype.hasOwnProperty.call(body, "strictProxy")) {
updates.strictProxy = body?.strictProxy === true;
}
if (Object.prototype.hasOwnProperty.call(body, "type")) {
const validTypes = ["http", "vercel", "cloudflare"];
updates.type = validTypes.includes(body?.type) ? body.type : "http";
}
return { updates };
}
function countBoundConnections(connections = [], proxyPoolId) {
return connections.filter((connection) => connection?.providerSpecificData?.proxyPoolId === proxyPoolId).length;
}
// GET /api/proxy-pools/[id] - Get proxy pool
export async function GET(request, { params }) {
try {
const { id } = await params;
const proxyPool = await getProxyPoolById(id);
if (!proxyPool) {
return NextResponse.json({ error: "Proxy pool not found" }, { status: 404 });
}
return NextResponse.json({ proxyPool });
} catch (error) {
console.log("Error fetching proxy pool:", error);
return NextResponse.json({ error: "Failed to fetch proxy pool" }, { status: 500 });
}
}
// PUT /api/proxy-pools/[id] - Update proxy pool
export async function PUT(request, { params }) {
try {
const { id } = await params;
const existing = await getProxyPoolById(id);
if (!existing) {
return NextResponse.json({ error: "Proxy pool not found" }, { status: 404 });
}
const body = await request.json();
const normalized = normalizeProxyPoolUpdate(body);
if (normalized.error) {
return NextResponse.json({ error: normalized.error }, { status: 400 });
}
const updated = await updateProxyPool(id, normalized.updates);
return NextResponse.json({ proxyPool: updated });
} catch (error) {
console.log("Error updating proxy pool:", error);
return NextResponse.json({ error: "Failed to update proxy pool" }, { status: 500 });
}
}
// DELETE /api/proxy-pools/[id] - Delete proxy pool
export async function DELETE(request, { params }) {
try {
const { id } = await params;
const existing = await getProxyPoolById(id);
if (!existing) {
return NextResponse.json({ error: "Proxy pool not found" }, { status: 404 });
}
const connections = await getProviderConnections();
const boundConnectionCount = countBoundConnections(connections, id);
if (boundConnectionCount > 0) {
return NextResponse.json(
{
error: "Proxy pool is currently in use",
boundConnectionCount,
},
{ status: 409 }
);
}
await deleteProxyPool(id);
return NextResponse.json({ success: true });
} catch (error) {
console.log("Error deleting proxy pool:", error);
return NextResponse.json({ error: "Failed to delete proxy pool" }, { status: 500 });
}
}
|