Skip to content

Commit ae902b9

Browse files
committed
proxy_agent: Use proxy-from-env for robustness
Now we support pretty much every variable under the sun along with $NO_PROXY all correctly and with minimal code on our end.
1 parent 94b4ba1 commit ae902b9

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

lib/vscode/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"native-watchdog": "1.3.0",
6666
"node-pty": "0.10.0-beta17",
6767
"proxy-agent": "^4.0.0",
68+
"proxy-from-env": "^1.1.0",
6869
"rimraf": "^2.2.8",
6970
"spdlog": "^0.11.1",
7071
"sudo-prompt": "9.1.1",
@@ -95,6 +96,7 @@
9596
"@types/minimist": "^1.2.0",
9697
"@types/mocha": "2.2.39",
9798
"@types/node": "^12.11.7",
99+
"@types/proxy-from-env": "^1.0.1",
98100
"@types/sinon": "^1.16.36",
99101
"@types/trusted-types": "^1.0.6",
100102
"@types/vscode-windows-registry": "^1.0.0",

lib/vscode/yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@
302302
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.28.tgz#b6d0628b0371d6c629d729c98322de314b640219"
303303
integrity sha512-EM/qFeRH8ZCD+TlsaIPULyyFm9vOhFIvgskY2JmHbEsWsOPgN+rtjSXrcHGgJpob4Nu17VfO95FKewr0XY7iOQ==
304304

305+
"@types/proxy-from-env@^1.0.1":
306+
version "1.0.1"
307+
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.1.tgz#b5f3e99230ca4518af196c18267055fc51f892b7"
308+
integrity sha512-luG++TFHyS61eKcfkR1CVV6a1GMNXDjtqEQIIfaSHax75xp0HU3SlezjOi1yqubJwrG8e9DeW59n6wTblIDwFg==
309+
dependencies:
310+
"@types/node" "*"
311+
305312
"@types/semver@^5.4.0", "@types/semver@^5.5.0":
306313
version "5.5.0"
307314
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.5.0.tgz#146c2a29ee7d3bae4bf2fcb274636e264c813c45"

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@types/node": "^12.12.7",
4141
"@types/parcel-bundler": "^1.12.1",
4242
"@types/pem": "^1.9.5",
43+
"@types/proxy-from-env": "^1.0.1",
4344
"@types/safe-compare": "^1.1.0",
4445
"@types/semver": "^7.1.0",
4546
"@types/split2": "^2.1.6",
@@ -82,6 +83,7 @@
8283
"limiter": "^1.1.5",
8384
"pem": "^1.14.2",
8485
"proxy-agent": "^4.0.0",
86+
"proxy-from-env": "^1.1.0",
8587
"qs": "6.7.0",
8688
"rotating-file-stream": "^2.1.1",
8789
"safe-buffer": "^5.1.1",

src/node/proxy_agent.ts

+38-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { logger } from "@coder/logger"
22
import * as http from "http"
3-
import * as url from "url"
4-
import * as proxyagent from "proxy-agent"
3+
import * as proxyAgent from "proxy-agent"
4+
import * as proxyFromEnv from "proxy-from-env"
55

66
/**
77
* This file has nothing to do with the code-server proxy.
8-
* It is for $HTTP_PROXY and $HTTPS_PROXY support.
8+
* It is to support $HTTP_PROXY, $HTTPS_PROXY and $NO_PROXY.
9+
*
910
* - https://github.com/cdr/code-server/issues/124
1011
* - https://www.npmjs.com/package/proxy-agent
12+
* - https://www.npmjs.com/package/proxy-from-env
1113
*
1214
* This file exists in two locations:
1315
* - src/node/proxy_agent.ts
@@ -17,7 +19,7 @@ import * as proxyagent from "proxy-agent"
1719

1820
/**
1921
* monkeyPatch patches the node http,https modules to route all requests through the
20-
* agents we get from the proxy-agent package.
22+
* agent we get from the proxy-agent package.
2123
*
2224
* This approach only works if there is no code specifying an explicit agent when making
2325
* a request.
@@ -28,29 +30,22 @@ import * as proxyagent from "proxy-agent"
2830
*
2931
* Even if they do, it's probably the same proxy so we should be fine! And those knobs
3032
* are deprecated anyway.
31-
*
32-
* We use $HTTP_PROXY for all HTTP resources via a normal HTTP proxy.
33-
* We use $HTTPS_PROXY for all HTTPS resources via HTTP connect.
34-
* See https://stackoverflow.com/a/10442767/4283659
3533
*/
3634
export function monkeyPatch(inVSCode: boolean): void {
37-
const http = require("http")
38-
const https = require("https")
39-
40-
const httpProxyURL = process.env.HTTP_PROXY || process.env.http_proxy
41-
if (httpProxyURL) {
42-
logger.debug(`using $HTTP_PROXY ${httpProxyURL}`)
43-
http.globalAgent = newProxyAgent(inVSCode, httpProxyURL)
44-
}
35+
if (shouldEnableProxy()) {
36+
const http = require("http")
37+
const https = require("https")
4538

46-
const httpsProxyURL = process.env.HTTPS_PROXY || process.env.https_proxy
47-
if (httpsProxyURL) {
48-
logger.debug(`using $HTTPS_PROXY ${httpsProxyURL}`)
49-
https.globalAgent = newProxyAgent(inVSCode, httpsProxyURL)
39+
// If we do not pass in a proxy URL, proxy-agent will get the URL from the environment.
40+
// See https://www.npmjs.com/package/proxy-from-env.
41+
// Also see shouldEnableProxy.
42+
const pa = newProxyAgent(inVSCode)
43+
http.globalAgent = pa
44+
https.globalAgent = pa
5045
}
5146
}
5247

53-
function newProxyAgent(inVSCode: boolean, for: "http" | "https", proxyURL: string): http.Agent {
48+
function newProxyAgent(inVSCode: boolean): http.Agent {
5449
// The reasoning for this split is that VS Code's build process does not have
5550
// esModuleInterop enabled but the code-server one does. As a result depending on where
5651
// we execute, we either have a default attribute or we don't.
@@ -59,8 +54,28 @@ function newProxyAgent(inVSCode: boolean, for: "http" | "https", proxyURL: strin
5954
// a huge number of errors. And we can't use require as otherwise the modules won't be
6055
// included in the final product.
6156
if (inVSCode) {
62-
return new (proxyagent as any)(opts)
57+
return new (proxyAgent as any)()
6358
} else {
64-
return new (proxyagent as any).default(opts)
59+
return new (proxyAgent as any).default()
6560
}
6661
}
62+
63+
// If they have $NO_PROXY set to example.com then this check won't work!
64+
// But that's drastically unlikely.
65+
function shouldEnableProxy(): boolean {
66+
let shouldEnable = false
67+
68+
const httpProxy = proxyFromEnv.getProxyForUrl(`http://example.com`)
69+
if (httpProxy) {
70+
shouldEnable = true
71+
logger.debug(`using $HTTP_PROXY ${httpProxy}`)
72+
}
73+
74+
const httpsProxy = proxyFromEnv.getProxyForUrl(`https://example.com`)
75+
if (httpsProxy) {
76+
shouldEnable = true
77+
logger.debug(`using $HTTPS_PROXY ${httpsProxy}`)
78+
}
79+
80+
return shouldEnable
81+
}

yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,13 @@
11371137
dependencies:
11381138
"@types/node" "*"
11391139

1140+
"@types/proxy-from-env@^1.0.1":
1141+
version "1.0.1"
1142+
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.1.tgz#b5f3e99230ca4518af196c18267055fc51f892b7"
1143+
integrity sha512-luG++TFHyS61eKcfkR1CVV6a1GMNXDjtqEQIIfaSHax75xp0HU3SlezjOi1yqubJwrG8e9DeW59n6wTblIDwFg==
1144+
dependencies:
1145+
"@types/node" "*"
1146+
11401147
"@types/q@^1.5.1":
11411148
version "1.5.4"
11421149
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
@@ -6333,7 +6340,7 @@ proxy-agent@^4.0.0:
63336340
proxy-from-env "^1.0.0"
63346341
socks-proxy-agent "^5.0.0"
63356342

6336-
proxy-from-env@^1.0.0:
6343+
proxy-from-env@^1.0.0, proxy-from-env@^1.1.0:
63376344
version "1.1.0"
63386345
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
63396346
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

0 commit comments

Comments
 (0)