Skip to content

Commit f5da494

Browse files
joyeecheungaduh95
authored andcommitted
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 <ilyasshabi94@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent f281272 commit f5da494

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
@@ -2959,6 +2959,21 @@ environment variables.
29592959

29602960
See `SSL_CERT_DIR` and `SSL_CERT_FILE`.
29612961

2962+
### `--use-env-proxy`
2963+
2964+
<!-- YAML
2965+
added: REPLACEME
2966+
-->
2967+
2968+
> Stability: 1.1 - Active Development
2969+
2970+
When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
2971+
environment variables during startup, and tunnels requests over the
2972+
specified proxy.
2973+
2974+
This is equivalent to setting the [`NODE_USE_ENV_PROXY=1`][] environment variable.
2975+
When both are set, `--use-env-proxy` takes precedence.
2976+
29622977
### `--use-largepages=mode`
29632978

29642979
<!-- YAML
@@ -3458,6 +3473,7 @@ one is included in the list below.
34583473
* `--track-heap-objects`
34593474
* `--unhandled-rejections`
34603475
* `--use-bundled-ca`
3476+
* `--use-env-proxy`
34613477
* `--use-largepages`
34623478
* `--use-openssl-ca`
34633479
* `--use-system-ca`
@@ -3613,8 +3629,8 @@ When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
36133629
environment variables during startup, and tunnels requests over the
36143630
specified proxy.
36153631

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

36193635
### `NODE_V8_COVERAGE=dir`
36203636

@@ -3943,12 +3959,14 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
39433959
[`--print`]: #-p---print-script
39443960
[`--redirect-warnings`]: #--redirect-warningsfile
39453961
[`--require`]: #-r---require-module
3962+
[`--use-env-proxy`]: #--use-env-proxy
39463963
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
39473964
[`Buffer`]: buffer.md#class-buffer
39483965
[`CRYPTO_secure_malloc_init`]: https://www.openssl.org/docs/man3.0/man3/CRYPTO_secure_malloc_init.html
39493966
[`ERR_INVALID_TYPESCRIPT_SYNTAX`]: errors.md#err_invalid_typescript_syntax
39503967
[`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax
39513968
[`NODE_OPTIONS`]: #node_optionsoptions
3969+
[`NODE_USE_ENV_PROXY=1`]: #node_use_env_proxy1
39523970
[`NO_COLOR`]: https://no-color.org
39533971
[`SlowBuffer`]: buffer.md#class-slowbuffer
39543972
[`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

doc/api/errors.md

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

25002500
### `ERR_PROXY_TUNNEL`
25012501

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

25042504
<a id="ERR_QUIC_APPLICATION_ERROR"></a>
25052505

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
@@ -538,6 +538,9 @@
538538
"use-bundled-ca": {
539539
"type": "boolean"
540540
},
541+
"use-env-proxy": {
542+
"type": "boolean"
543+
},
541544
"use-largepages": {
542545
"type": "string"
543546
},

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ See
600600
and
601601
.Ev SSL_CERT_FILE .
602602
.
603+
.It Fl -use-env-proxy
604+
Parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY environment variables and apply the setting in global HTTP/HTTPS clients.
605+
.
603606
.It Fl -use-largepages Ns = Ns Ar mode
604607
Re-map the Node.js static code to large memory pages at startup. If supported on
605608
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
@@ -660,6 +660,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
660660
"emit pending deprecation warnings",
661661
&EnvironmentOptions::pending_deprecation,
662662
kAllowedInEnvvar);
663+
AddOption("--use-env-proxy",
664+
"parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY"
665+
"environment variables and apply the setting in global HTTP/HTTPS "
666+
"clients",
667+
&EnvironmentOptions::use_env_proxy,
668+
kAllowedInEnvvar);
663669
AddOption("--preserve-symlinks",
664670
"preserve symbolic links when resolving",
665671
&EnvironmentOptions::preserve_symlinks,
@@ -1875,6 +1881,8 @@ void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
18751881
env_options->preserve_symlinks_main =
18761882
opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
18771883

1884+
env_options->use_env_proxy = opt_getter("NODE_USE_ENV_PROXY") == "1";
1885+
18781886
if (env_options->redirect_warnings.empty())
18791887
env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
18801888
}

src/node_options.h

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

243243
bool insecure_http_parser = false;
244+
bool use_env_proxy = false;
244245

245246
bool tls_min_v1_0 = false;
246247
bool tls_min_v1_1 = false;

0 commit comments

Comments
 (0)