Skip to content

Commit 1486fed

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 <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 4210259 commit 1486fed

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

29862986
See `SSL_CERT_DIR` and `SSL_CERT_FILE`.
29872987

2988+
### `--use-env-proxy`
2989+
2990+
<!-- YAML
2991+
added: REPLACEME
2992+
-->
2993+
2994+
> Stability: 1.1 - Active Development
2995+
2996+
When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
2997+
environment variables during startup, and tunnels requests over the
2998+
specified proxy.
2999+
3000+
This is equivalent to setting the [`NODE_USE_ENV_PROXY=1`][] environment variable.
3001+
When both are set, `--use-env-proxy` takes precedence.
3002+
29883003
### `--use-largepages=mode`
29893004

29903005
<!-- YAML
@@ -3486,6 +3501,7 @@ one is included in the list below.
34863501
* `--track-heap-objects`
34873502
* `--unhandled-rejections`
34883503
* `--use-bundled-ca`
3504+
* `--use-env-proxy`
34893505
* `--use-largepages`
34903506
* `--use-openssl-ca`
34913507
* `--use-system-ca`
@@ -3641,8 +3657,8 @@ When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`
36413657
environment variables during startup, and tunnels requests over the
36423658
specified proxy.
36433659

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

36473663
### `NODE_USE_SYSTEM_CA=1`
36483664

@@ -3989,6 +4005,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
39894005
[`--print`]: #-p---print-script
39904006
[`--redirect-warnings`]: #--redirect-warningsfile
39914007
[`--require`]: #-r---require-module
4008+
[`--use-env-proxy`]: #--use-env-proxy
39924009
[`--use-system-ca`]: #--use-system-ca
39934010
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
39944011
[`Atomics.wait()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait
@@ -3997,6 +4014,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
39974014
[`ERR_INVALID_TYPESCRIPT_SYNTAX`]: errors.md#err_invalid_typescript_syntax
39984015
[`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax
39994016
[`NODE_OPTIONS`]: #node_optionsoptions
4017+
[`NODE_USE_ENV_PROXY=1`]: #node_use_env_proxy1
40004018
[`NO_COLOR`]: https://no-color.org
40014019
[`SlowBuffer`]: buffer.md#class-slowbuffer
40024020
[`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
@@ -2537,7 +2537,7 @@ Failed to proxy a request because the proxy configuration is invalid.
25372537

25382538
### `ERR_PROXY_TUNNEL`
25392539

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

25422542
<a id="ERR_QUIC_APPLICATION_ERROR"></a>
25432543

doc/api/http.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,10 +4329,9 @@ added: REPLACEME
43294329
43304330
> Stability: 1.1 - Active development
43314331
4332-
When Node.js creates the global agent, it checks the `NODE_USE_ENV_PROXY`
4333-
environment variable. If it is set to `1`, the global agent will be constructed
4332+
When Node.js creates the global agent, if the `NODE_USE_ENV_PROXY` environment variable is
4333+
set to `1` or `--use-env-proxy` is enabled, the global agent will be constructed
43344334
with `proxyEnv: process.env`, enabling proxy support based on the environment variables.
4335-
43364335
Custom agents can also be created with proxy support by passing a
43374336
`proxyEnv` option when constructing the agent. The value can be `process.env`
43384337
if they just want to inherit the configuration from the environment variables,
@@ -4374,13 +4373,20 @@ Multiple entries should be separated by commas.
43744373
43754374
### Example
43764375
4377-
Starting a Node.js process with proxy support enabled for all requests sent
4378-
through the default global agent:
4376+
To start a Node.js process with proxy support enabled for all requests sent
4377+
through the default global agent, either use the `NODE_USE_ENV_PROXY` environment
4378+
variable:
43794379
43804380
```console
43814381
NODE_USE_ENV_PROXY=1 HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node client.js
43824382
```
43834383
4384+
Or the `--use-env-proxy` flag.
4385+
4386+
```console
4387+
HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node --use-env-proxy client.js
4388+
```
4389+
43844390
To create a custom agent with built-in proxy support:
43854391
43864392
```cjs

doc/node-config-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,9 @@
550550
"use-bundled-ca": {
551551
"type": "boolean"
552552
},
553+
"use-env-proxy": {
554+
"type": "boolean"
555+
},
553556
"use-largepages": {
554557
"type": "string"
555558
},

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ See
627627
and
628628
.Ev SSL_CERT_FILE .
629629
.
630+
.It Fl -use-env-proxy
631+
Parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY environment variables and apply the setting in global HTTP/HTTPS clients.
632+
.
630633
.It Fl -use-system-ca
631634
Use the trusted CA certificates present in the system store, in addition to the certificates in the
632635
bundled Mozilla CA store and certificates from `NODE_EXTRA_CA_CERTS`. On Windows and macOS, it uses system APIs to

lib/_http_agent.js

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

5960
const kOnKeylog = Symbol('onkeylog');
6061
const kRequestOptions = Symbol('requestOptions');
@@ -628,6 +629,7 @@ module.exports = {
628629
Agent,
629630
globalAgent: new Agent({
630631
keepAlive: true, scheduling: 'lifo', timeout: 5000,
631-
proxyEnv: process.env.NODE_USE_ENV_PROXY ? filterEnvForProxies(process.env) : undefined,
632+
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
633+
proxyEnv: getOptionValue('--use-env-proxy') ? filterEnvForProxies(process.env) : undefined,
632634
}),
633635
};

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
@@ -191,19 +191,24 @@ function setupSymbolDisposePolyfill() {
191191
}
192192

193193
function setupHttpProxy() {
194-
if (process.env.NODE_USE_ENV_PROXY &&
195-
(process.env.HTTP_PROXY || process.env.HTTPS_PROXY ||
196-
process.env.http_proxy || process.env.https_proxy)) {
197-
const { setGlobalDispatcher, EnvHttpProxyAgent } = require('internal/deps/undici/undici');
198-
const envHttpProxyAgent = new EnvHttpProxyAgent();
199-
setGlobalDispatcher(envHttpProxyAgent);
200-
// For fetch, we need to set the global dispatcher from here.
201-
// For http/https agents, we'll configure the global agent when they are
202-
// actually created, in lib/_http_agent.js and lib/https.js.
203-
// TODO(joyeecheung): This is currently guarded with NODE_USE_ENV_PROXY. Investigate whether
204-
// it's possible to enable it by default without stepping on other existing libraries that
205-
// sets the global dispatcher or monkey patches the global agent.
194+
// This normalized from both --use-env-proxy and NODE_USE_ENV_PROXY settings.
195+
if (!getOptionValue('--use-env-proxy')) {
196+
return;
197+
}
198+
if (!process.env.HTTP_PROXY && !process.env.HTTPS_PROXY &&
199+
!process.env.http_proxy && !process.env.https_proxy) {
200+
return;
206201
}
202+
203+
const { setGlobalDispatcher, EnvHttpProxyAgent } = require('internal/deps/undici/undici');
204+
const envHttpProxyAgent = new EnvHttpProxyAgent();
205+
setGlobalDispatcher(envHttpProxyAgent);
206+
// For fetch, we need to set the global dispatcher from here.
207+
// For http/https agents, we'll configure the global agent when they are
208+
// actually created, in lib/_http_agent.js and lib/https.js.
209+
// TODO(joyeecheung): This is currently guarded with NODE_USE_ENV_PROXY and --use-env-proxy.
210+
// Investigate whether it's possible to enable it by default without stepping on other
211+
// existing libraries that sets the global dispatcher or monkey patches the global agent.
207212
}
208213

209214
function setupUserModules(forceDefaultLoader = false) {

src/node_options.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
651651
"emit pending deprecation warnings",
652652
&EnvironmentOptions::pending_deprecation,
653653
kAllowedInEnvvar);
654+
AddOption("--use-env-proxy",
655+
"parse proxy settings from HTTP_PROXY/HTTPS_PROXY/NO_PROXY"
656+
"environment variables and apply the setting in global HTTP/HTTPS "
657+
"clients",
658+
&EnvironmentOptions::use_env_proxy,
659+
kAllowedInEnvvar);
654660
AddOption("--preserve-symlinks",
655661
"preserve symbolic links when resolving",
656662
&EnvironmentOptions::preserve_symlinks,
@@ -1773,6 +1779,8 @@ void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
17731779
env_options->preserve_symlinks_main =
17741780
opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
17751781

1782+
env_options->use_env_proxy = opt_getter("NODE_USE_ENV_PROXY") == "1";
1783+
17761784
if (env_options->redirect_warnings.empty())
17771785
env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
17781786
}

src/node_options.h

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

242242
bool insecure_http_parser = false;
243+
bool use_env_proxy = false;
243244

244245
bool tls_min_v1_0 = false;
245246
bool tls_min_v1_1 = false;

0 commit comments

Comments
 (0)