|
| 1 | +import { ProxyAgent } from "undici"; |
| 2 | +import { readSettings, readProjectSettings } from "../settings"; |
| 3 | +import type { SettingsProcessEnv } from "../settings"; |
| 4 | + |
| 5 | +export type ProxyType = "http" | "https" | "socks5"; |
| 6 | + |
| 7 | +export type ResolvedProxy = { |
| 8 | + url: string; |
| 9 | + type: ProxyType; |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * Determine whether a target URL should bypass the proxy based on NO_PROXY. |
| 14 | + * |
| 15 | + * Supports: |
| 16 | + * - `*` wildcard (bypass everything) |
| 17 | + * - Exact hostname match (e.g. `localhost`, `example.com`) |
| 18 | + * - Sub-domain wildcard (e.g. `.example.com` matches `api.example.com`) |
| 19 | + */ |
| 20 | +function shouldBypassProxy(targetUrl: string, noProxy: string): boolean { |
| 21 | + if (!noProxy.trim()) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + const entries = noProxy |
| 26 | + .split(",") |
| 27 | + .map((entry) => entry.trim().toLowerCase()) |
| 28 | + .filter(Boolean); |
| 29 | + |
| 30 | + if (entries.includes("*")) { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + let hostname: string; |
| 35 | + try { |
| 36 | + hostname = new URL(targetUrl).hostname.toLowerCase(); |
| 37 | + } catch { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + for (const entry of entries) { |
| 42 | + if (entry.startsWith(".")) { |
| 43 | + // ".example.com" matches "api.example.com" and "example.com" |
| 44 | + if (hostname.endsWith(entry) || hostname === entry.slice(1)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } else if (hostname === entry || hostname.endsWith(`.${entry}`)) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Pick the first non-empty proxy variable from `source`, preferring |
| 57 | + * HTTPS_PROXY → HTTP_PROXY → SOCKS_PROXY → SOCKS5_PROXY (case-insensitive |
| 58 | + * lowercase fallback for each). |
| 59 | + */ |
| 60 | +function pickProxyVar(source: Record<string, string | undefined>): { url: string; type: ProxyType } | undefined { |
| 61 | + const candidates: Array<{ keys: string[]; type: ProxyType }> = [ |
| 62 | + { keys: ["HTTPS_PROXY", "https_proxy"], type: "https" }, |
| 63 | + { keys: ["HTTP_PROXY", "http_proxy"], type: "http" }, |
| 64 | + { keys: ["SOCKS_PROXY", "socks_proxy", "SOCKS5_PROXY", "socks5_proxy"], type: "socks5" }, |
| 65 | + ]; |
| 66 | + |
| 67 | + for (const { keys, type } of candidates) { |
| 68 | + for (const key of keys) { |
| 69 | + const value = source[key]; |
| 70 | + if (typeof value === "string" && value.trim()) { |
| 71 | + return { url: value.trim(), type }; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return undefined; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Pick NO_PROXY from a source (case-insensitive fallback). |
| 81 | + */ |
| 82 | +function pickNoProxy(source: Record<string, string | undefined>): string { |
| 83 | + return ( |
| 84 | + (typeof source.NO_PROXY === "string" && source.NO_PROXY) || |
| 85 | + (typeof source.no_proxy === "string" && source.no_proxy) || |
| 86 | + "" |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Resolve the effective proxy URL by consulting (in ascending priority): |
| 92 | + * 1. User-level `settings.json` → `env` |
| 93 | + * 2. Project-level `settings.json` → `env` |
| 94 | + * 3. Process environment variables (both standard `HTTP_PROXY` / `HTTPS_PROXY` |
| 95 | + * and `DEEPCODE_`-prefixed variants) |
| 96 | + * |
| 97 | + * Returns `undefined` when no proxy is configured or when NO_PROXY matches. |
| 98 | + */ |
| 99 | +export function resolveProxyUrl( |
| 100 | + targetUrl: string, |
| 101 | + projectRoot: string = process.cwd(), |
| 102 | + processEnv: SettingsProcessEnv = process.env |
| 103 | +): ResolvedProxy | undefined { |
| 104 | + // --- Collect proxy vars from each layer --- |
| 105 | + const userEnv = readSettings()?.env ?? {}; |
| 106 | + const projectEnv = readProjectSettings(projectRoot)?.env ?? {}; |
| 107 | + |
| 108 | + // System env includes both standard proxy vars and DEEPCODE_-prefixed ones |
| 109 | + // (collectDeepcodeEnv strips the prefix, so DEEPCODE_HTTPS_PROXY → HTTPS_PROXY). |
| 110 | + const systemProxySource: Record<string, string | undefined> = { ...processEnv }; |
| 111 | + for (const [key, value] of Object.entries(processEnv)) { |
| 112 | + if (key.startsWith("DEEPCODE_") && typeof value === "string") { |
| 113 | + const stripped = key.slice("DEEPCODE_".length); |
| 114 | + if (stripped) { |
| 115 | + systemProxySource[stripped] = value; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // --- NO_PROXY check (system level takes absolute precedence) --- |
| 121 | + const systemNoProxy = pickNoProxy(systemProxySource); |
| 122 | + if (shouldBypassProxy(targetUrl, systemNoProxy)) { |
| 123 | + return undefined; |
| 124 | + } |
| 125 | + |
| 126 | + // --- Merge: user < project < system --- |
| 127 | + const merged: Record<string, string | undefined> = { |
| 128 | + ...userEnv, |
| 129 | + ...projectEnv, |
| 130 | + ...systemProxySource, |
| 131 | + }; |
| 132 | + |
| 133 | + // NO_PROXY from merged (user/project may also define it, but system wins) |
| 134 | + const mergedNoProxy = pickNoProxy(merged); |
| 135 | + if (shouldBypassProxy(targetUrl, mergedNoProxy)) { |
| 136 | + return undefined; |
| 137 | + } |
| 138 | + |
| 139 | + return pickProxyVar(merged); |
| 140 | +} |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// Dispatcher cache – avoids re-creating ProxyAgent on every request. |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | +let cachedProxyUrl = ""; |
| 146 | +let cachedDispatcher: ProxyAgent | null = null; |
| 147 | + |
| 148 | +/** |
| 149 | + * Return a `ProxyAgent` dispatcher when a proxy is configured for the given |
| 150 | + * target URL, or `null` when requests should go direct. |
| 151 | + */ |
| 152 | +export function getProxyDispatcher(targetUrl?: string): ProxyAgent | null { |
| 153 | + const resolved = resolveProxyUrl(targetUrl ?? "https://api.deepseek.com"); |
| 154 | + const proxyUrl = resolved?.url ?? ""; |
| 155 | + |
| 156 | + if (!proxyUrl) { |
| 157 | + cachedProxyUrl = ""; |
| 158 | + cachedDispatcher = null; |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + if (cachedDispatcher && cachedProxyUrl === proxyUrl) { |
| 163 | + return cachedDispatcher; |
| 164 | + } |
| 165 | + |
| 166 | + cachedProxyUrl = proxyUrl; |
| 167 | + cachedDispatcher = new ProxyAgent({ |
| 168 | + uri: proxyUrl, |
| 169 | + keepAliveTimeout: 180_000, |
| 170 | + }); |
| 171 | + return cachedDispatcher; |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * Fetch wrapper that automatically routes through the configured proxy. |
| 176 | + * Use this in place of the global `fetch` for any HTTP request that should |
| 177 | + * respect the proxy configuration. |
| 178 | + */ |
| 179 | +export async function proxyFetch(url: string | URL, init?: RequestInit): Promise<Response> { |
| 180 | + const dispatcher = getProxyDispatcher(String(url)); |
| 181 | + if (!dispatcher) { |
| 182 | + return fetch(url, init); |
| 183 | + } |
| 184 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 185 | + return (fetch as any)(url, { ...init, dispatcher }); |
| 186 | +} |
0 commit comments