| 1 | import { RequestOptions } from "@continuedev/config-types"; |
| 2 | |
| 3 | /** |
| 4 | * Gets the proxy settings from environment variables |
| 5 | * @param protocol The URL protocol (http: or https:) |
| 6 | * @returns The proxy URL if available, otherwise undefined |
| 7 | */ |
| 8 | export function getProxyFromEnv(protocol: string): string | undefined { |
| 9 | if (protocol === "https:") { |
| 10 | return ( |
| 11 | process.env.HTTPS_PROXY || |
| 12 | process.env.https_proxy || |
| 13 | process.env.HTTP_PROXY || |
| 14 | process.env.http_proxy |
| 15 | ); |
| 16 | } else { |
| 17 | return process.env.HTTP_PROXY || process.env.http_proxy; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // Note that request options proxy (per model) takes precedence over environment variables |
| 22 | export function getProxy( |
| 23 | protocol: string, |
| 24 | requestOptions?: RequestOptions, |
| 25 | ): string | undefined { |
| 26 | if (requestOptions?.proxy) { |
| 27 | return requestOptions.proxy; |
| 28 | } |
| 29 | return getProxyFromEnv(protocol); |
| 30 | } |
| 31 | |
| 32 | export function getEnvNoProxyPatterns(): string[] { |
| 33 | const envValue = process.env.NO_PROXY || process.env.no_proxy; |
| 34 | if (envValue) { |
| 35 | return envValue |
| 36 | .split(",") |
| 37 | .map((item) => item.trim().toLowerCase()) |
| 38 | .filter((i) => !!i); |
| 39 | } else { |
| 40 | return []; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | export function getReqOptionsNoProxyPatterns( |
| 45 | options: RequestOptions | undefined, |
| 46 | ): string[] { |
| 47 | return ( |
| 48 | options?.noProxy?.map((i) => i.trim().toLowerCase()).filter((i) => !!i) ?? |
| 49 | [] |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | export function patternMatchesHostname(hostname: string, pattern: string) { |
| 54 | // Split hostname and pattern to separate hostname and port |
| 55 | const [hostnameWithoutPort, hostnamePort] = hostname.toLowerCase().split(":"); |
| 56 | const [patternWithoutPort, patternPort] = pattern.toLowerCase().split(":"); |
| 57 | |
| 58 | // If pattern specifies a port but hostname doesn't match it, no match |
| 59 | if (patternPort && (!hostnamePort || hostnamePort !== patternPort)) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Now compare just the hostname parts |
| 64 | |
| 65 | // exact match |
| 66 | if (patternWithoutPort === hostnameWithoutPort) { |
| 67 | return true; |
| 68 | } |
| 69 | // wildcard domain match (*.example.com) |
| 70 | if ( |
| 71 | patternWithoutPort.startsWith("*.") && |
| 72 | hostnameWithoutPort.endsWith(patternWithoutPort.substring(1)) |
| 73 | ) { |
| 74 | return true; |
| 75 | } |
| 76 | // Domain suffix match (.example.com) |
| 77 | if ( |
| 78 | patternWithoutPort.startsWith(".") && |
| 79 | hostnameWithoutPort.endsWith(patternWithoutPort.slice(1)) |
| 80 | ) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | // TODO IP address ranges |
| 85 | |
| 86 | // TODO CIDR notation |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if a hostname should bypass proxy based on NO_PROXY environment variable |
| 93 | * @param hostname The hostname to check |
| 94 | * @returns True if the hostname should bypass proxy |
| 95 | */ |
| 96 | export function shouldBypassProxy( |
| 97 | hostname: string, |
| 98 | requestOptions: RequestOptions | undefined, |
| 99 | ): boolean { |
| 100 | const ignores = [ |
| 101 | ...getEnvNoProxyPatterns(), |
| 102 | ...getReqOptionsNoProxyPatterns(requestOptions), |
| 103 | ]; |
| 104 | const hostLowerCase = hostname.toLowerCase(); |
| 105 | return ignores.some((ignore) => |
| 106 | patternMatchesHostname(hostLowerCase, ignore), |
| 107 | ); |
| 108 | } |