Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

4.0 KB
Raw Blame
import { request as undiciRequest } from "undici";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { createErrorResponse, createErrorResponseFromUnknown } from "@/lib/api/errorResponse";
import { getFreeProxyById, promoteFreeProxyToPool } from "@/lib/db/freeProxies";
import {
  createProxyDispatcher,
  proxyConfigToUrl,
} from "@omniroute/open-sse/utils/proxyDispatcher.ts";
import { probeEchoTargets } from "@/lib/proxyEchoTarget";

type ConnectivityTester = (
  host: string,
  port: number,
  type: string
) => Promise<{ success: boolean; latencyMs: number; publicIp?: string }>;

async function testProxyConnectivity(
  host: string,
  port: number,
  type: string
): Promise<{ success: boolean; latencyMs: number; publicIp?: string }> {
  const proxyUrl = proxyConfigToUrl({ type, host, port });
  if (!proxyUrl) return { success: false, latencyMs: 0 };

  const dispatcher = createProxyDispatcher(proxyUrl);
  const start = Date.now();

  try {
    // #9694: try the IPv6-first echo target, then the IPv4-only one, so a proxy
    // with no IPv6 route is not reported dead.
    const { result } = await probeEchoTargets(async (url, timeoutMs) => {
      const controller = new AbortController();
      const timeout = setTimeout(() => controller.abort(), timeoutMs);
      try {
        const res = await undiciRequest(url, {
          method: "GET",
          dispatcher,
          signal: controller.signal,
          headersTimeout: timeoutMs,
          bodyTimeout: timeoutMs,
        });
        return { statusCode: res.statusCode, text: await res.body.text() };
      } finally {
        clearTimeout(timeout);
      }
    }, 5000);
    let parsed: { ip?: string } = {};
    try {
      parsed = JSON.parse(result.text) as { ip?: string };
    } catch {}
    return {
      success: result.statusCode === 200,
      latencyMs: Date.now() - start,
      publicIp: parsed.ip,
    };
  } catch {
    return { success: false, latencyMs: Date.now() - start };
  }
}

let _connectivityTester: ConnectivityTester = testProxyConnectivity;
export function _setConnectivityTesterForTests(fn: ConnectivityTester): void {
  _connectivityTester = fn;
}
export function _resetConnectivityTesterForTests(): void {
  _connectivityTester = testProxyConnectivity;
}

export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  const { id } = await params;
  const freeProxy = await getFreeProxyById(id);
  if (!freeProxy) {
    return createErrorResponse({ status: 404, message: "Free proxy not found", type: "not_found" });
  }
  if (freeProxy.inPool) {
    return Response.json({
      success: true,
      alreadyInPool: true,
      poolProxyId: freeProxy.poolProxyId,
    });
  }

  try {
    const testResult = await _connectivityTester(freeProxy.host, freeProxy.port, freeProxy.type);
    if (!testResult.success) {
      // #4878: a failed connectivity probe must surface a non-2xx status so the
      // frontend (which gates on res.ok) does NOT optimistically mark the proxy
      // as "In Pool". 422 = the request was well-formed but the proxy is unusable.
      return Response.json(
        {
          success: false,
          error: "Proxy test failed",
          latencyMs: testResult.latencyMs,
        },
        { status: 422 }
      );
    }

    const newPoolProxyId = await promoteFreeProxyToPool(id, {
      name: `[${freeProxy.source}] ${freeProxy.host}:${freeProxy.port}`,
      type: freeProxy.type,
      host: freeProxy.host,
      port: freeProxy.port,
      source: freeProxy.source,
    });

    if (!newPoolProxyId) {
      return createErrorResponse({
        status: 500,
        message: "Failed to create proxy in registry",
        type: "server_error",
      });
    }

    return Response.json({
      success: true,
      poolProxyId: newPoolProxyId,
      latencyMs: testResult.latencyMs,
    });
  } catch (error) {
    return createErrorResponseFromUnknown(error, "Failed to add proxy to pool");
  }
}