-
Notifications
You must be signed in to change notification settings - Fork 157
feat: add upstream proxy configuration for outbound requests #1458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
be51974
b0a4b43
548e84c
9336511
45624bf
9af1d69
7534cf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ import { processUrlPath, validGitRequest } from './helper'; | |
| import { getAllProxiedHosts } from '../../db'; | ||
| import { ProxyOptions } from 'express-http-proxy'; | ||
| import { handleErrorAndLog } from '../../utils/errors'; | ||
| import { getUpstreamProxyConfig } from '../../config'; | ||
| import { HttpsProxyAgent } from 'https-proxy-agent'; | ||
| import { OutgoingHttpHeaders, RequestOptions } from 'http'; | ||
|
|
||
| enum ActionType { | ||
| ALLOWED = 'Allowed', | ||
|
|
@@ -144,7 +147,111 @@ const getRequestPathResolver: (prefix: string) => ProxyOptions['proxyReqPathReso | |
| }; | ||
| }; | ||
|
|
||
| const proxyReqOptDecorator: ProxyOptions['proxyReqOptDecorator'] = (proxyReqOpts) => proxyReqOpts; | ||
| const getEnvProxyUrl = () => | ||
| process.env.HTTPS_PROXY || | ||
| process.env.https_proxy || | ||
| process.env.HTTP_PROXY || | ||
| process.env.http_proxy; | ||
|
|
||
| const getEnvNoProxyList = (): string[] => { | ||
| const noProxy = process.env.NO_PROXY || process.env.no_proxy; | ||
| if (!noProxy) { | ||
| return []; | ||
| } | ||
| return noProxy | ||
| .split(',') | ||
| .map((entry) => entry.trim()) | ||
| .filter((entry) => entry.length > 0); | ||
| }; | ||
|
|
||
| const hostMatchesNoProxy = (host: string | null | undefined, noProxyList: string[]): boolean => { | ||
|
jescalada marked this conversation as resolved.
|
||
| if (!host) { | ||
| return false; | ||
| } | ||
|
|
||
| const hostname = host.split(':')[0]; | ||
|
|
||
| return noProxyList.some((pattern) => { | ||
| if (!pattern) { | ||
| return false; | ||
| } | ||
|
|
||
| const trimmed = pattern.trim().replace(/^\./, ''); // strip leading dot | ||
| if (trimmed === '*') return true; // wildcard - bypass all | ||
|
|
||
| if (trimmed === '') { | ||
| return false; | ||
| } | ||
|
|
||
| // Exact match | ||
| if (hostname === trimmed) { | ||
| return true; | ||
| } | ||
|
|
||
| // Domain suffix match, e.g. example.com matches foo.example.com | ||
| if (hostname.endsWith(`.${trimmed}`)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }); | ||
| }; | ||
|
|
||
| // WARNING: proxyUrl may contain plaintext credentials in the userinfo portion | ||
| // (e.g. http://user:pass@proxy.corp.local:8080). Never log it directly — use | ||
| // redactProxyUrl() from config for any log statements involving this value. | ||
| let _cachedProxyAgent: { proxyUrl: string; agent: HttpsProxyAgent<string> } | null = null; | ||
|
|
||
| const getOrCreateProxyAgent = (proxyUrl: string): HttpsProxyAgent<string> => { | ||
| if (!_cachedProxyAgent || _cachedProxyAgent.proxyUrl !== proxyUrl) { | ||
| try { | ||
| new URL(proxyUrl); | ||
| } catch { | ||
| throw new Error( | ||
| `Invalid upstream proxy URL: check your upstreamProxy.url config or HTTPS_PROXY env var`, | ||
| ); | ||
| } | ||
| _cachedProxyAgent = { proxyUrl, agent: new HttpsProxyAgent(proxyUrl) }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just repeating Denis earlier comment - this should be properly validated and/or throw a descriptive error
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, thanks for remind :) |
||
| } | ||
| return _cachedProxyAgent.agent; | ||
| }; | ||
|
|
||
| const buildUpstreamProxyAgent = ( | ||
| proxyReqOpts: Omit<RequestOptions, 'headers'> & { | ||
| headers: OutgoingHttpHeaders; | ||
| }, | ||
| ) => { | ||
| const { enabled, url, noProxy } = getUpstreamProxyConfig(); | ||
|
|
||
| const proxyUrl = url || getEnvProxyUrl(); | ||
|
|
||
| if (enabled === false || !proxyUrl) { | ||
| return undefined; | ||
|
jescalada marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const host: string | null | undefined = proxyReqOpts.host || proxyReqOpts.hostname; | ||
|
|
||
| const combinedNoProxy = [...(noProxy || []), ...getEnvNoProxyList()]; | ||
|
|
||
| if (hostMatchesNoProxy(host, combinedNoProxy)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return getOrCreateProxyAgent(proxyUrl); | ||
| }; | ||
|
|
||
| const proxyReqOptDecorator: ProxyOptions['proxyReqOptDecorator'] = (proxyReqOpts, _srcReq) => { | ||
| const agent = buildUpstreamProxyAgent(proxyReqOpts); | ||
|
jescalada marked this conversation as resolved.
|
||
|
|
||
| if (!agent) { | ||
| return proxyReqOpts; | ||
| } | ||
|
|
||
| return { | ||
| ...proxyReqOpts, | ||
| agent, | ||
| }; | ||
| }; | ||
|
|
||
| const proxyReqBodyDecorator: ProxyOptions['proxyReqBodyDecorator'] = (bodyContent, srcReq) => { | ||
| if (srcReq.method === 'GET') { | ||
|
|
@@ -273,4 +380,6 @@ export { | |
| isPackPost, | ||
| extractRawBody, | ||
| validGitRequest, | ||
| buildUpstreamProxyAgent, | ||
| hostMatchesNoProxy, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.