File size: 8,422 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { request as undiciRequest } from "undici";
import {
  createProxyDispatcher,
  isRelayType,
  isSocks5ProxyEnabled,
  proxyConfigToUrl,
  proxyUrlForLogs,
} from "@omniroute/open-sse/utils/proxyDispatcher.ts";
import { testProxySchema } from "@/shared/validation/schemas";
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
import { createErrorResponse, createErrorResponseFromUnknown } from "@/lib/api/errorResponse";
import { getProxyById } from "@/lib/localDb";
import { extractRelayAuth } from "@/lib/db/proxies";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";

const BASE_SUPPORTED_PROXY_TYPES = new Set(["http", "https"]);

function getErrorMessage(error: unknown, fallbackMessage: string): string {
  return sanitizeErrorMessage(error) || fallbackMessage;
}

function getSupportedProxyTypes() {
  if (isSocks5ProxyEnabled()) {
    return new Set([...BASE_SUPPORTED_PROXY_TYPES, "socks5"]);
  }
  return BASE_SUPPORTED_PROXY_TYPES;
}

function supportedTypesMessage() {
  return isSocks5ProxyEnabled() ? "http, https, or socks5" : "http or https";
}

/**
 * POST /api/settings/proxy/test — test proxy connectivity
 * Body: { proxy: { type, host, port, username?, password? } }
 * Returns: { success, publicIp?, latencyMs?, error? }
 */
export async function POST(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  let rawBody: unknown;
  try {
    rawBody = await request.json();
  } catch {
    return createErrorResponse({
      status: 400,
      message: "Invalid JSON body",
      type: "invalid_request",
    });
  }

  try {
    const validation = validateBody(testProxySchema, rawBody);
    if (isValidationFailure(validation)) {
      return createErrorResponse({
        status: 400,
        message: validation.error.message,
        details: validation.error.details,
        type: "invalid_request",
      });
    }
    let { proxy } = validation.data;

    // If a proxyId is provided, look up the real (non-redacted) credentials from DB.
    // The frontend sends redacted credentials (***) from listProxies(), so we need
    // the actual secrets for testing.
    const body = rawBody as Record<string, unknown>;
    const proxyId = typeof body.proxyId === "string" ? body.proxyId.trim() : null;
    let dbProxyNotes: string | null = null;
    if (proxyId) {
      const dbProxy = await getProxyById(proxyId, { includeSecrets: true });
      if (dbProxy) {
        proxy = {
          ...proxy,
          host: proxy.host || dbProxy.host,
          port: proxy.port || String(dbProxy.port),
          type: proxy.type || dbProxy.type,
          username: dbProxy.username,
          password: dbProxy.password,
        };
        dbProxyNotes = dbProxy.notes ?? null;
      }
    }

    const proxyType = String(proxy.type || "http").toLowerCase();

    // Relay proxies (Vercel / Deno / Cloudflare): test by hitting ipify via the
    // relay headers. All three share the same x-relay-* header contract; the
    // only difference is the deployed edge target (#5128 — Deno/Cloudflare were
    // previously rejected here as unsupported proxy types).
    if (isRelayType(proxyType)) {
      const relayHost = proxy.host;
      // relayAuth lives in notes JSON, written by the deploy routes as either a
      // plaintext { relayAuth } or, on installs with STORAGE_ENCRYPTION_KEY, an
      // encrypted { relayAuthEnc }. extractRelayAuth handles both (#5128 — the
      // encrypted form was previously ignored, leaving relayAuth empty → 401).
      let relayAuth = extractRelayAuth(dbProxyNotes) ?? "";
      // Fallback: ad-hoc callers may pass relayAuth in the password field
      if (!relayAuth) relayAuth = proxy.password ?? "";
      const relayUrl = `https://${relayHost}`;
      const start = Date.now();
      const controller2 = new AbortController();
      const timeout2 = setTimeout(() => controller2.abort(), 10000);
      try {
        // Send request to the relay URL with relay headers; relay forwards to ipify
        const res = await undiciRequest(`${relayUrl}/`, {
          method: "GET",
          signal: controller2.signal,
          headersTimeout: 10000,
          bodyTimeout: 10000,
          headers: {
            "x-relay-target": "https://api64.ipify.org",
            "x-relay-path": "/?format=json",
            "x-relay-auth": relayAuth,
          },
        });
        const text = await res.body.text();
        let parsedIp: { ip?: string } = {};
        try {
          parsedIp = JSON.parse(text) as { ip?: string };
        } catch {}
        return Response.json({
          success: res.statusCode === 200,
          publicIp: parsedIp.ip || null,
          latencyMs: Date.now() - start,
          proxyUrl: relayUrl,
        });
      } catch (relayErr) {
        return Response.json({
          success: false,
          error:
            relayErr instanceof Error && relayErr.name === "AbortError"
              ? "Connection timeout (10s)"
              : getErrorMessage(relayErr, "Relay test failed"),
          latencyMs: Date.now() - start,
          proxyUrl: relayUrl,
        });
      } finally {
        clearTimeout(timeout2);
      }
    }

    if (proxyType === "socks5" && !isSocks5ProxyEnabled()) {
      return createErrorResponse({
        status: 400,
        message: "SOCKS5 proxy is disabled (set ENABLE_SOCKS5_PROXY=true to enable)",
        type: "invalid_request",
      });
    }
    if (proxyType.startsWith("socks") && proxyType !== "socks5") {
      return createErrorResponse({
        status: 400,
        message: `proxy.type must be ${supportedTypesMessage()}`,
        type: "invalid_request",
      });
    }
    if (!getSupportedProxyTypes().has(proxyType)) {
      return createErrorResponse({
        status: 400,
        message: `proxy.type must be ${supportedTypesMessage()}`,
        type: "invalid_request",
      });
    }

    let proxyUrl: string;
    try {
      const normalizedProxyUrl = proxyConfigToUrl(
        {
          type: proxyType,
          host: proxy.host,
          port: proxy.port,
          username: proxy.username || "",
          password: proxy.password || "",
        },
        { allowSocks5: isSocks5ProxyEnabled() }
      );
      if (!normalizedProxyUrl) {
        return createErrorResponse({
          status: 400,
          message: "Invalid proxy configuration",
          type: "invalid_request",
        });
      }
      proxyUrl = normalizedProxyUrl;
    } catch (proxyError) {
      return createErrorResponse({
        status: 400,
        message: getErrorMessage(proxyError, "Invalid proxy configuration"),
        type: "invalid_request",
      });
    }

    const publicProxyUrl = proxyUrlForLogs(proxyUrl);

    const startTime = Date.now();
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), 10000);
    const dispatcher = createProxyDispatcher(proxyUrl);

    try {
      const result = await undiciRequest("https://api64.ipify.org?format=json", {
        method: "GET",
        dispatcher,
        signal: controller.signal,
        headersTimeout: 10000,
        bodyTimeout: 10000,
      });

      const responseText = await result.body.text();
      let parsed: { ip?: string };
      try {
        const parsedJson = JSON.parse(responseText);
        if (parsedJson && typeof parsedJson === "object") {
          parsed = parsedJson as { ip?: string };
        } else {
          parsed = { ip: String(parsedJson) };
        }
      } catch {
        parsed = { ip: responseText.trim() };
      }

      return Response.json({
        success: true,
        publicIp: parsed.ip || null,
        latencyMs: Date.now() - startTime,
        proxyUrl: publicProxyUrl,
      });
    } catch (fetchError) {
      return Response.json({
        success: false,
        error:
          fetchError instanceof Error && fetchError.name === "AbortError"
            ? "Connection timeout (10s)"
            : getErrorMessage(fetchError, "Connection failed"),
        latencyMs: Date.now() - startTime,
        proxyUrl: publicProxyUrl,
      });
    } finally {
      clearTimeout(timeout);
    }
  } catch (error) {
    return createErrorResponseFromUnknown(error, "Unexpected server error");
  }
}