GeezerVison is showing github.com — cli: add --use-env-proxy · nodejs/node@1486fed
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.md +20-2Lines changed: 20 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2985,6 +2985,21 @@ environment variables. | 2985 | 2985 |
| 2986 | 2986 | See `SSL_CERT_DIR` and `SSL_CERT_FILE`. | 2987 | 2987 |
| | 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 | + | 2988 | 3003 | ### `--use-largepages=mode` | 2989 | 3004 |
| 2990 | 3005 | <!-- YAML | @@ -3486,6 +3501,7 @@ one is included in the list below. | 3486 | 3501 | * `--track-heap-objects` | 3487 | 3502 | * `--unhandled-rejections` | 3488 | 3503 | * `--use-bundled-ca` | | 3504 | +* `--use-env-proxy` | 3489 | 3505 | * `--use-largepages` | 3490 | 3506 | * `--use-openssl-ca` | 3491 | 3507 | * `--use-system-ca` | @@ -3641,8 +3657,8 @@ When enabled, Node.js parses the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` | 3641 | 3657 | environment variables during startup, and tunnels requests over the | 3642 | 3658 | specified proxy. | 3643 | 3659 |
| 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. | 3646 | 3662 |
| 3647 | 3663 | ### `NODE_USE_SYSTEM_CA=1` | 3648 | 3664 |
| @@ -3989,6 +4005,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 | 3989 | 4005 | [`--print`]: #-p---print-script | 3990 | 4006 | [`--redirect-warnings`]: #--redirect-warningsfile | 3991 | 4007 | [`--require`]: #-r---require-module | | 4008 | +[`--use-env-proxy`]: #--use-env-proxy | 3992 | 4009 | [`--use-system-ca`]: #--use-system-ca | 3993 | 4010 | [`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage | 3994 | 4011 | [`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 | 3997 | 4014 | [`ERR_INVALID_TYPESCRIPT_SYNTAX`]: errors.md#err_invalid_typescript_syntax | 3998 | 4015 | [`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`]: errors.md#err_unsupported_typescript_syntax | 3999 | 4016 | [`NODE_OPTIONS`]: #node_optionsoptions | | 4017 | +[`NODE_USE_ENV_PROXY=1`]: #node_use_env_proxy1 | 4000 | 4018 | [`NO_COLOR`]: https://no-color.org | 4001 | 4019 | [`SlowBuffer`]: buffer.md#class-slowbuffer | 4002 | 4020 | [`Web Storage`]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2537,7 +2537,7 @@ Failed to proxy a request because the proxy configuration is invalid. | 2537 | 2537 |
| 2538 | 2538 | ### `ERR_PROXY_TUNNEL` | 2539 | 2539 |
| 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. | 2541 | 2541 |
| 2542 | 2542 | <a id="ERR_QUIC_APPLICATION_ERROR"></a> | 2543 | 2543 |
| |
Collapse file Copy file name to clipboardExpand all lines: doc/api/http.md +11-5Lines changed: 11 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4329,10 +4329,9 @@ added: REPLACEME | 4329 | 4329 |
| 4330 | 4330 | > Stability: 1.1 - Active development | 4331 | 4331 |
| 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 | 4334 | 4334 | with `proxyEnv: process.env`, enabling proxy support based on the environment variables. | 4335 | | -
| 4336 | 4335 | Custom agents can also be created with proxy support by passing a | 4337 | 4336 | `proxyEnv` option when constructing the agent. The value can be `process.env` | 4338 | 4337 | if they just want to inherit the configuration from the environment variables, | @@ -4374,13 +4373,20 @@ Multiple entries should be separated by commas. | 4374 | 4373 |
| 4375 | 4374 | ### Example | 4376 | 4375 |
| 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: | 4379 | 4379 |
| 4380 | 4380 | ```console | 4381 | 4381 | NODE_USE_ENV_PROXY=1 HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node client.js | 4382 | 4382 | ``` | 4383 | 4383 |
| | 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 | +
| 4384 | 4390 | To create a custom agent with built-in proxy support: | 4385 | 4391 |
| 4386 | 4392 | ```cjs | |
Collapse file Copy file name to clipboardExpand all lines: doc/node-config-schema.json +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 550 | 550 | "use-bundled-ca": { | 551 | 551 | "type": "boolean" | 552 | 552 | }, | | 553 | + "use-env-proxy": { | | 554 | + "type": "boolean" | | 555 | + }, | 553 | 556 | "use-largepages": { | 554 | 557 | "type": "string" | 555 | 558 | }, | |
Collapse file Copy file name to clipboardExpand all lines: doc/node.1 +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 627 | 627 | and | 628 | 628 | .Ev SSL_CERT_FILE . | 629 | 629 | . | | 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 | +. | 630 | 633 | .It Fl -use-system-ca | 631 | 634 | Use the trusted CA certificates present in the system store, in addition to the certificates in the | 632 | 635 | bundled Mozilla CA store and certificates from `NODE_EXTRA_CA_CERTS`. On Windows and macOS, it uses system APIs to | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_agent.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,6 +55,7 @@ const { | 55 | 55 | validateString, | 56 | 56 | } = require('internal/validators'); | 57 | 57 | const assert = require('internal/assert'); | | 58 | +const { getOptionValue } = require('internal/options'); | 58 | 59 |
| 59 | 60 | const kOnKeylog = Symbol('onkeylog'); | 60 | 61 | const kRequestOptions = Symbol('requestOptions'); | @@ -628,6 +629,7 @@ module.exports = { | 628 | 629 | Agent, | 629 | 630 | globalAgent: new Agent({ | 630 | 631 | 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, | 632 | 634 | }), | 633 | 635 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/https.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,7 @@ const { URL, urlToHttpOptions, isURL } = require('internal/url'); | 70 | 70 | const { validateObject } = require('internal/validators'); | 71 | 71 | const { isIP, isIPv6 } = require('internal/net'); | 72 | 72 | const assert = require('internal/assert'); | | 73 | +const { getOptionValue } = require('internal/options'); | 73 | 74 |
| 74 | 75 | function Server(opts, requestListener) { | 75 | 76 | if (!(this instanceof Server)) return new Server(opts, requestListener); | @@ -599,7 +600,8 @@ Agent.prototype._evictSession = function _evictSession(key) { | 599 | 600 |
| 600 | 601 | const globalAgent = new Agent({ | 601 | 602 | 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, | 603 | 605 | }); | 604 | 606 |
| 605 | 607 | /** | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/pre_execution.js +17-12Lines changed: 17 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -191,19 +191,24 @@ function setupSymbolDisposePolyfill() { | 191 | 191 | } | 192 | 192 |
| 193 | 193 | 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; | 206 | 201 | } | | 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. | 207 | 212 | } | 208 | 213 |
| 209 | 214 | function setupUserModules(forceDefaultLoader = false) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -651,6 +651,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 651 | 651 | "emit pending deprecation warnings", | 652 | 652 | &EnvironmentOptions::pending_deprecation, | 653 | 653 | 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); | 654 | 660 | AddOption("--preserve-symlinks", | 655 | 661 | "preserve symbolic links when resolving", | 656 | 662 | &EnvironmentOptions::preserve_symlinks, | @@ -1773,6 +1779,8 @@ void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options, | 1773 | 1779 | env_options->preserve_symlinks_main = | 1774 | 1780 | opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1"; | 1775 | 1781 |
| | 1782 | + env_options->use_env_proxy = opt_getter("NODE_USE_ENV_PROXY") == "1"; | | 1783 | + | 1776 | 1784 | if (env_options->redirect_warnings.empty()) | 1777 | 1785 | env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS"); | 1778 | 1786 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -240,6 +240,7 @@ class EnvironmentOptions : public Options { | 240 | 240 | bool force_repl = false; | 241 | 241 |
| 242 | 242 | bool insecure_http_parser = false; | | 243 | + bool use_env_proxy = false; | 243 | 244 |
| 244 | 245 | bool tls_min_v1_0 = false; | 245 | 246 | bool tls_min_v1_1 = false; | |
|