From c4a6086e9cb393024108c4466abf7afce4328311 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Mon, 2 Mar 2020 11:27:00 +0000 Subject: [PATCH 1/4] Don't try to setup a proxy agent when there's no proxy configured Change-type: patch --- lib/app-common.ts | 51 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/app-common.ts b/lib/app-common.ts index 36f1e5e5..4f8b2b4d 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -87,6 +87,8 @@ export interface GlobalTunnelNgConfig { sockets?: number; } +type ProxyConfig = string | GlobalTunnelNgConfig; + /** * Global proxy setup. Originally, `global-tunnel-ng` was used, but it only * supports Node.js versions older than 10.16.0. For v10.16.0 and later, @@ -110,12 +112,28 @@ export interface GlobalTunnelNgConfig { * default exclusion patterns are added for all private IPv4 address ranges. */ async function setupGlobalHttpProxy(settings: CliSettings) { - const semver = await import('semver'); - if (semver.lt(process.version, '10.16.0')) { - setupGlobalTunnelNgProxy(settings); - } else { - // use global-agent instead of global-tunnel-ng - await setupGlobalAgentProxy(settings); + // `global-tunnel-ng` accepts lowercase variables with higher precedence + // than uppercase variables, but `global-agent` does not accept lowercase. + // Set uppercase versions for backwards compatibility. + const { env } = process; + if (env.http_proxy) { + env.HTTP_PROXY = env.http_proxy; + } + if (env.https_proxy) { + env.HTTPS_PROXY = env.https_proxy; + } + delete env.http_proxy; + delete env.https_proxy; + + const proxy = settings.getCatch('proxy'); + if (proxy || env.HTTPS_PROXY || env.HTTP_PROXY) { + const semver = await import('semver'); + if (semver.lt(process.version, '10.16.0')) { + setupGlobalTunnelNgProxy(proxy); + } else { + // use global-agent instead of global-tunnel-ng + await setupGlobalAgentProxy(settings, proxy); + } } } @@ -123,8 +141,7 @@ async function setupGlobalHttpProxy(settings: CliSettings) { * `global-tunnel-ng` proxy setup. * See docs for setupGlobalHttpProxy() above. */ -function setupGlobalTunnelNgProxy(settings: CliSettings) { - const proxy = settings.getCatch('proxy'); +function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) { const globalTunnel = require('global-tunnel-ng'); // Init the tunnel even if BALENARC_PROXY is not defined, because // other env vars may be defined. If no proxy configuration exists, @@ -138,8 +155,10 @@ function setupGlobalTunnelNgProxy(settings: CliSettings) { * See docs for setupGlobalHttpProxy() above, and also the README file * (Proxy Support section). */ -async function setupGlobalAgentProxy(settings: CliSettings) { - const proxy = settings.getCatch('proxy'); +async function setupGlobalAgentProxy( + settings: CliSettings, + proxy?: ProxyConfig, +) { const noProxy = settings.getCatch('noProxy'); // Always exclude localhost, even if NO_PROXY is set const requiredNoProxy = ['localhost', '127.0.0.1']; @@ -158,23 +177,11 @@ async function setupGlobalAgentProxy(settings: CliSettings) { typeof proxy === 'string' ? proxy : makeUrlFromTunnelNgConfig(proxy); env.HTTPS_PROXY = env.HTTP_PROXY = proxyUrl; - delete env.http_proxy; - delete env.https_proxy; env.NO_PROXY = [ ...requiredNoProxy, ...(noProxy ? _.filter((noProxy || '').split(',')) : privateNoProxy), ].join(','); - } else { - // `global-tunnel-ng` accepts lowercase variables with higher precedence - // than uppercase variables, but `global-agent` does not accept lowercase. - // Set uppercase versions for backwards compatibility. - if (env.http_proxy) { - env.HTTP_PROXY = env.http_proxy; - } - if (env.https_proxy) { - env.HTTPS_PROXY = env.https_proxy; - } } const { bootstrap } = require('global-agent'); From 3f6d7702331d6e00615a676c22bf946298af60ce Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Mon, 2 Mar 2020 12:39:43 +0000 Subject: [PATCH 2/4] Remove lodash usage in proxy setup Change-type: patch --- lib/app-common.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/app-common.ts b/lib/app-common.ts index 4f8b2b4d..5002c38d 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -172,7 +172,6 @@ async function setupGlobalAgentProxy( env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE = ''; if (proxy) { - const _ = await import('lodash'); const proxyUrl: string = typeof proxy === 'string' ? proxy : makeUrlFromTunnelNgConfig(proxy); @@ -180,7 +179,7 @@ async function setupGlobalAgentProxy( env.NO_PROXY = [ ...requiredNoProxy, - ...(noProxy ? _.filter((noProxy || '').split(',')) : privateNoProxy), + ...(noProxy ? noProxy.split(',').filter(v => v) : privateNoProxy), ].join(','); } From 00943463a42d6b9d7103ce85aed4da14ed427717 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Mon, 2 Mar 2020 13:28:29 +0000 Subject: [PATCH 3/4] Use types for global-agent and global-tunnel-ng Change-type: patch --- lib/app-common.ts | 17 +++++------------ npm-shrinkwrap.json | 12 ++++++++++++ package.json | 2 ++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/app-common.ts b/lib/app-common.ts index 5002c38d..9aa51b83 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -78,14 +78,7 @@ function checkNodeVersion() { } } -export interface GlobalTunnelNgConfig { - host: string; - port: number; - protocol: string; - proxyAuth?: string; - connect?: string; - sockets?: number; -} +export type GlobalTunnelNgConfig = import('global-tunnel-ng').Options; type ProxyConfig = string | GlobalTunnelNgConfig; @@ -129,7 +122,7 @@ async function setupGlobalHttpProxy(settings: CliSettings) { if (proxy || env.HTTPS_PROXY || env.HTTP_PROXY) { const semver = await import('semver'); if (semver.lt(process.version, '10.16.0')) { - setupGlobalTunnelNgProxy(proxy); + await setupGlobalTunnelNgProxy(proxy); } else { // use global-agent instead of global-tunnel-ng await setupGlobalAgentProxy(settings, proxy); @@ -141,8 +134,8 @@ async function setupGlobalHttpProxy(settings: CliSettings) { * `global-tunnel-ng` proxy setup. * See docs for setupGlobalHttpProxy() above. */ -function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) { - const globalTunnel = require('global-tunnel-ng'); +async function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) { + const globalTunnel = await import('global-tunnel-ng'); // Init the tunnel even if BALENARC_PROXY is not defined, because // other env vars may be defined. If no proxy configuration exists, // initialize() does nothing. @@ -183,7 +176,7 @@ async function setupGlobalAgentProxy( ].join(','); } - const { bootstrap } = require('global-agent'); + const { bootstrap } = await import('global-agent'); bootstrap(); } diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 226ba9f9..af44595c 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -745,6 +745,18 @@ "@types/node": "*" } }, + "@types/global-agent": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/global-agent/-/global-agent-2.1.0.tgz", + "integrity": "sha512-xBOerse4Agekl7VZJclA9bfuA9aa3u9T24TDkBiMQrZgu4qe5HMBPzVGzAt2k4dx/v3uIFI6CzG0Z9X894LHrg==", + "dev": true + }, + "@types/global-tunnel-ng": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@types/global-tunnel-ng/-/global-tunnel-ng-2.1.0.tgz", + "integrity": "sha512-JPhCJHHu5/5HuQ78rmbrnCk+XlyxKuAopk+/8rP5MfAeL7KwiCH/gFFNtAqIr0/JFqWFk+jXWZjEWSP8dzGpMg==", + "dev": true + }, "@types/intercept-stdout": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/@types/intercept-stdout/-/intercept-stdout-0.1.0.tgz", diff --git a/package.json b/package.json index 0c0afdcc..27aab5b6 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,8 @@ "@types/ejs": "^3.0.1", "@types/express": "^4.17.2", "@types/fs-extra": "^8.1.0", + "@types/global-agent": "^2.1.0", + "@types/global-tunnel-ng": "^2.1.0", "@types/intercept-stdout": "^0.1.0", "@types/is-root": "^2.1.2", "@types/lodash": "^4.14.149", From 9470e804c0e7e2e53743fa7bdd039a21eebb3954 Mon Sep 17 00:00:00 2001 From: Paulo Castro Date: Mon, 2 Mar 2020 14:46:37 +0000 Subject: [PATCH 4/4] Don't ignore BALENARC_NO_PROXY env var if HTTP(S)_PROXY env vars are defined Change-type: patch --- lib/app-common.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/app-common.ts b/lib/app-common.ts index 9aa51b83..baaa5b78 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -147,6 +147,7 @@ async function setupGlobalTunnelNgProxy(proxy?: ProxyConfig) { * `global-agent` proxy setup. * See docs for setupGlobalHttpProxy() above, and also the README file * (Proxy Support section). + * If `proxy` is undefined, HTTP(S)_PROXY env vars are expected to be set. */ async function setupGlobalAgentProxy( settings: CliSettings, @@ -163,17 +164,16 @@ async function setupGlobalAgentProxy( const env = process.env; env.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE = ''; + env.NO_PROXY = [ + ...requiredNoProxy, + ...(noProxy ? noProxy.split(',').filter(v => v) : privateNoProxy), + ].join(','); if (proxy) { const proxyUrl: string = typeof proxy === 'string' ? proxy : makeUrlFromTunnelNgConfig(proxy); env.HTTPS_PROXY = env.HTTP_PROXY = proxyUrl; - - env.NO_PROXY = [ - ...requiredNoProxy, - ...(noProxy ? noProxy.split(',').filter(v => v) : privateNoProxy), - ].join(','); } const { bootstrap } = await import('global-agent');