Skip to content

Commit 0259df9

Browse files
authored
cli: add --use-env-proxy
This does the same as NODE_USE_ENV_PROXY. When both are set, like other options that can be configured from both sides, the CLI flag takes precedence. PR-URL: #59151 Fixes: #59100 Reviewed-By: Ilyas Shabi <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 5e1a4fa commit 0259df9

16 files changed

+347
-30
lines changed

doc/api/cli.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,21 @@ environment variables.
29992999

30003000
See `SSL_CERT_DIR` and `SSL_CERT_FILE`.
30013001

3002+
### `--use-env-proxy`
3003+
3004+
<!-- YAML
3005+
added: REPLACEME
3006+
-->
3007+
3008+
> Stability: 1.1 - Active Development
3009+
3010+
When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
3011+
environment variables during startup, and tunnels requests over the
3012+
specified proxy.
3013+
3014+
This is equivalent to setting the [`NODE_USE_ENV_PROXY=1`][] environment variable.
3015+
When both are set, `--use-env-proxy` takes precedence.
3016+
30023017
### `--use-largepages=mode`
30033018

30043019
<!-- YAML
@@ -3498,6 +3513,7 @@ one is included in the list below.
34983513
* `--track-heap-objects`
34993514
* `--unhandled-rejections`
35003515
* `--use-bundled-ca`
3516+
* `--use-env-proxy`
35013517
* `--use-largepages`
35023518
* `--use-openssl-ca`
35033519
* `--use-system-ca`
@@ -3653,8 +3669,8 @@ When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
36533669
environment variables during startup, and tunnels requests over the
36543670
specified proxy.
36553671

3656-
This currently only affects requests sent over `fetch()`. Support for other
3657-
built-in `http` and `https` methods is under way.
3672+
This can also be enabled using the [`--use-env-proxy`][] command-line flag.
3673+
When both are set, `--use-env-proxy` takes precedence.
36583674

36593675
### `NODE_V8_COVERAGE=dir`
36603676

@@ -3984,12 +4000,14 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
39844000
[`--print`]: #-p---print-script
39854001
[`--redirect-warnings`]: #--redirect-warningsfile
39864002
[`--require`]: #-r---require-module
4003+
[`--use-env-proxy`]: #--use-env-proxy
39874004
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
39884005
[`Buffer`]: buffer.md#class-buffer
39894006
[`CRYPTO_secure_malloc_init`]: https://www.openssl.org/docs/man3.0/man3/CRYPTO_secure_malloc_init.html
39904007
[`ERR_INVALID_TYPESCRIPT_SYNTAX`]: errors.md#err_invalid_typescript_syntax
39914008
[`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax
39924009
[`NODE_OPTIONS`]: #node_optionsoptions
4010+
[`NODE_USE_ENV_PROXY=1`]: #node_use_env_proxy1
39934011
[`NO_COLOR`]: https://no-color.org
39944012
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
39954013
[`YoungGenerationSizeFromSemiSpaceSize`]: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/10.3.129/src/heap/heap.cc#328

doc/api/errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,7 @@ Failed to proxy a request because the proxy configuration is invalid.
25012501

25022502
### `ERR_PROXY_TUNNEL`
25032503

2504-
Failed to establish proxy tunnel when `NODE_USE_ENV_PROXY` is enabled.
2504+
Failed to establish proxy tunnel when `NODE_USE_ENV_PROXY` or `--use-env-proxy` is enabled.
25052505

25062506
<a id="ERR_QUIC_APPLICATION_ERROR"></a>
25072507

doc/api/http.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,10 +4273,9 @@ added: REPLACEME
42734273
42744274
> Stability: 1.1 - Active development
42754275
4276-
When Node.js creates the global agent, it checks the `NODE_USE_ENV_PROXY`
4277-
environment variable. If it is set to `1`, the global agent will be constructed
4276+
When Node.js creates the global agent, if the `NODE_USE_ENV_PROXY` environment variable is
4277+
set to `1` or `--use-env-proxy` is enabled, the global agent will be constructed
42784278
with `proxyEnv: process.env`, enabling proxy support based on the environment variables.
4279-
42804279
Custom agents can also be created with proxy support by passing a
42814280
`proxyEnv` option when constructing the agent. The value can be `process.env`
42824281
if they just want to inherit the configuration from the environment variables,
@@ -4318,13 +4317,20 @@ Multiple entries should be separated by commas.
43184317
43194318
### Example
43204319
4321-
Starting a Node.js process with proxy support enabled for all requests sent
4322-
through the default global agent:
4320+
To start a Node.js process with proxy support enabled for all requests sent
4321+
through the default global agent, either use the `NODE_USE_ENV_PROXY` environment
4322+
variable:
43234323
43244324
```console
43254325
NODE_USE_ENV_PROXY=1 HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node client.js
43264326
```
43274327
4328+
Or the `--use-env-proxy` flag.
4329+
4330+
```console
4331+
HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node --use-env-proxy client.js
4332+
```
4333+
43284334
To create a custom agent with built-in proxy support:
43294335
43304336
```cjs

doc/node-config-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,9 @@
541541
"use-bundled-ca": {
542542
"type": "boolean"
543543
},
544+
"use-env-proxy": {
545+
"type": "boolean"
546+
},
544547
"use-largepages": {
545548
"type": "string"
546549
},

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,9 @@ See
603603
and
604604
.Ev SSL_CERT_FILE .
605605
.
606+
.It Fl -use-env-proxy
607+
Parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY environment variables and apply the setting in global HTTP/HTTPS clients.
608+
.
606609
.It Fl -use-largepages Ns = Ns Ar mode
607610
Re-map the Node.js static code to large memory pages at startup. If supported on
608611
the target system, this will cause the Node.js static code to be moved onto 2

lib/_http_agent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const {
5454
validateString,
5555
} = require('internal/validators');
5656
const assert = require('internal/assert');
57+
const { getOptionValue } = require('internal/options');
5758

5859
const kOnKeylog = Symbol('onkeylog');
5960
const kRequestOptions = Symbol('requestOptions');
@@ -622,6 +623,7 @@ module.exports = {
622623
Agent,
623624
globalAgent: new Agent({
624625
keepAlive: true, scheduling: 'lifo', timeout: 5000,
625-
proxyEnv: process.env.NODE_USE_ENV_PROXY ? filterEnvForProxies(process.env) : undefined,
626+
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
627+
proxyEnv: getOptionValue('--use-env-proxy') ? filterEnvForProxies(process.env) : undefined,
626628
}),
627629
};

lib/https.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const { URL, urlToHttpOptions, isURL } = require('internal/url');
7070
const { validateObject } = require('internal/validators');
7171
const { isIP, isIPv6 } = require('internal/net');
7272
const assert = require('internal/assert');
73+
const { getOptionValue } = require('internal/options');
7374

7475
function Server(opts, requestListener) {
7576
if (!(this instanceof Server)) return new Server(opts, requestListener);
@@ -599,7 +600,8 @@ Agent.prototype._evictSession = function _evictSession(key) {
599600

600601
const globalAgent = new Agent({
601602
keepAlive: true, scheduling: 'lifo', timeout: 5000,
602-
proxyEnv: process.env.NODE_USE_ENV_PROXY ? filterEnvForProxies(process.env) : undefined,
603+
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
604+
proxyEnv: getOptionValue('--use-env-proxy') ? filterEnvForProxies(process.env) : undefined,
603605
});
604606

605607
/**

lib/internal/process/pre_execution.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,24 @@ function prepareExecution(options) {
166166
}
167167

168168
function setupHttpProxy() {
169-
if (process.env.NODE_USE_ENV_PROXY &&
170-
(process.env.HTTP_PROXY || process.env.HTTPS_PROXY ||
171-
process.env.http_proxy || process.env.https_proxy)) {
172-
const { setGlobalDispatcher, EnvHttpProxyAgent } = require('internal/deps/undici/undici');
173-
const envHttpProxyAgent = new EnvHttpProxyAgent();
174-
setGlobalDispatcher(envHttpProxyAgent);
175-
// For fetch, we need to set the global dispatcher from here.
176-
// For http/https agents, we'll configure the global agent when they are
177-
// actually created, in lib/_http_agent.js and lib/https.js.
178-
// TODO(joyeecheung): This is currently guarded with NODE_USE_ENV_PROXY. Investigate whether
179-
// it's possible to enable it by default without stepping on other existing libraries that
180-
// sets the global dispatcher or monkey patches the global agent.
169+
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
170+
if (!getOptionValue('--use-env-proxy')) {
171+
return;
172+
}
173+
if (!process.env.HTTP_PROXY && !process.env.HTTPS_PROXY &&
174+
!process.env.http_proxy && !process.env.https_proxy) {
175+
return;
181176
}
177+
178+
const { setGlobalDispatcher, EnvHttpProxyAgent } = require('internal/deps/undici/undici');
179+
const envHttpProxyAgent = new EnvHttpProxyAgent();
180+
setGlobalDispatcher(envHttpProxyAgent);
181+
// For fetch, we need to set the global dispatcher from here.
182+
// For http/https agents, we'll configure the global agent when they are
183+
// actually created, in lib/_http_agent.js and lib/https.js.
184+
// TODO(joyeecheung): This is currently guarded with NODE_USE_ENV_PROXY and --use-env-proxy.
185+
// Investigate whether it's possible to enable it by default without stepping on other
186+
// existing libraries that sets the global dispatcher or monkey patches the global agent.
182187
}
183188

184189
function setupUserModules(forceDefaultLoader = false) {

src/node_options.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
664664
"emit pending deprecation warnings",
665665
&EnvironmentOptions::pending_deprecation,
666666
kAllowedInEnvvar);
667+
AddOption("--use-env-proxy",
668+
"parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY"
669+
"environment variables and apply the setting in global HTTP/HTTPS "
670+
"clients",
671+
&EnvironmentOptions::use_env_proxy,
672+
kAllowedInEnvvar);
667673
AddOption("--preserve-symlinks",
668674
"preserve symbolic links when resolving",
669675
&EnvironmentOptions::preserve_symlinks,
@@ -1878,6 +1884,8 @@ void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
18781884
env_options->preserve_symlinks_main =
18791885
opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
18801886

1887+
env_options->use_env_proxy = opt_getter("NODE_USE_ENV_PROXY") == "1";
1888+
18811889
if (env_options->redirect_warnings.empty())
18821890
env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
18831891
}

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class EnvironmentOptions : public Options {
242242
bool force_repl = false;
243243

244244
bool insecure_http_parser = false;
245+
bool use_env_proxy = false;
245246

246247
bool tls_min_v1_0 = false;
247248
bool tls_min_v1_1 = false;

0 commit comments

Comments
 (0)