Skip to content

Commit cb44666

Browse files
authored
Merge pull request #2487 from cdr/proxy-docs-86d4
proxy-agent: Use proxy-from-env and add docs
2 parents ea1b387 + cb72128 commit cb44666

File tree

6 files changed

+99
-37
lines changed

6 files changed

+99
-37
lines changed

doc/FAQ.md

+25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Isn't an install script piped into sh insecure?](#isnt-an-install-script-piped-into-sh-insecure)
2828
- [How do I make my keyboard shortcuts work?](#how-do-i-make-my-keyboard-shortcuts-work)
2929
- [Differences compared to Theia?](#differences-compared-to-theia)
30+
- [`$HTTP_PROXY`, `$HTTPS_PROXY`, `$NO_PROXY`](#http_proxy-https_proxy-no_proxy)
3031
- [Enterprise](#enterprise)
3132

3233
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@@ -338,6 +339,30 @@ You can't just use your VS Code config in Theia like you can with code-server.
338339
To summarize, code-server is a patched fork of VS Code to run in the browser whereas
339340
Theia takes some parts of VS Code but is an entirely different editor.
340341

342+
## `$HTTP_PROXY`, `$HTTPS_PROXY`, `$NO_PROXY`
343+
344+
code-server supports the standard environment variables to allow directing
345+
server side requests through a proxy.
346+
347+
```sh
348+
export HTTP_PROXY=https://134.8.5.4
349+
export HTTPS_PROXY=https://134.8.5.4
350+
# Now all of code-server's server side requests will go through
351+
# https://134.8.5.4 first.
352+
code-server
353+
```
354+
355+
- See [proxy-from-env](https://www.npmjs.com/package/proxy-from-env#environment-variables)
356+
for a detailed reference on the various environment variables and their syntax.
357+
- code-server only uses the `http` and `https` protocols.
358+
- See [proxy-agent](https://www.npmjs.com/package/proxy-agent) for the various supported
359+
proxy protocols.
360+
361+
**note**: Only server side requests will be proxied! This includes fetching extensions,
362+
requests made from extensions etc. To proxy requests from your browser you need to
363+
configure your browser separately. Browser requests would cover exploring the extension
364+
marketplace.
365+
341366
## Enterprise
342367

343368
Visit [our enterprise page](https://coder.com) for more information about our

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
@@ -270,6 +270,13 @@
270270
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.28.tgz#b6d0628b0371d6c629d729c98322de314b640219"
271271
integrity sha512-EM/qFeRH8ZCD+TlsaIPULyyFm9vOhFIvgskY2JmHbEsWsOPgN+rtjSXrcHGgJpob4Nu17VfO95FKewr0XY7iOQ==
272272

273+
"@types/proxy-from-env@^1.0.1":
274+
version "1.0.1"
275+
resolved "https://registry.yarnpkg.com/@types/proxy-from-env/-/proxy-from-env-1.0.1.tgz#b5f3e99230ca4518af196c18267055fc51f892b7"
276+
integrity sha512-luG++TFHyS61eKcfkR1CVV6a1GMNXDjtqEQIIfaSHax75xp0HU3SlezjOi1yqubJwrG8e9DeW59n6wTblIDwFg==
277+
dependencies:
278+
"@types/node" "*"
279+
273280
"@types/semver@^5.4.0", "@types/semver@^5.5.0":
274281
version "5.5.0"
275282
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

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

56
/**
6-
* This file does not have anything to do with the code-server proxy.
7-
* It's for $HTTP_PROXY support!
7+
* This file has nothing to do with the code-server proxy.
8+
* It is to support $HTTP_PROXY, $HTTPS_PROXY and $NO_PROXY.
9+
*
810
* - https://github.com/cdr/code-server/issues/124
911
* - https://www.npmjs.com/package/proxy-agent
12+
* - https://www.npmjs.com/package/proxy-from-env
1013
*
1114
* This file exists in two locations:
1215
* - src/node/proxy_agent.ts
@@ -15,48 +18,64 @@ import * as proxyagent from "proxy-agent"
1518
*/
1619

1720
/**
18-
* monkeyPatch patches the node HTTP/HTTPS library to route all requests through our
19-
* custom agent from the proxyAgent package.
21+
* monkeyPatch patches the node http,https modules to route all requests through the
22+
* agent we get from the proxy-agent package.
23+
*
24+
* This approach only works if there is no code specifying an explicit agent when making
25+
* a request.
26+
*
27+
* None of our code ever passes in a explicit agent to the http,https modules.
28+
* VS Code's does sometimes but only when a user sets the http.proxy configuration.
29+
* See https://code.visualstudio.com/docs/setup/network#_legacy-proxy-server-support
30+
*
31+
* Even if they do, it's probably the same proxy so we should be fine! And those knobs
32+
* are deprecated anyway.
2033
*/
21-
export function monkeyPatch(vscode: boolean): void {
22-
// We do not support HTTPS_PROXY here to avoid confusion. proxy-agent will automatically
23-
// use the correct protocol to connect to the proxy depending on the requested URL.
24-
//
25-
// We could implement support ourselves to allow people to configure the proxy used for
26-
// HTTPS vs HTTP but there doesn't seem to be much value in that.
27-
//
28-
// At least of right now, it'd just be plain confusing to support HTTPS_PROXY when proxy-agent
29-
// will still use HTTP to hit it for HTTP requests.
30-
const proxyURL = process.env.HTTP_PROXY || process.env.http_proxy
31-
if (!proxyURL) {
32-
return
33-
}
34+
export function monkeyPatch(inVSCode: boolean): void {
35+
if (shouldEnableProxy()) {
36+
const http = require("http")
37+
const https = require("https")
3438

35-
logger.debug(`using $HTTP_PROXY ${process.env.HTTP_PROXY}`)
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
45+
}
46+
}
3647

37-
let pa: http.Agent
48+
function newProxyAgent(inVSCode: boolean): http.Agent {
3849
// The reasoning for this split is that VS Code's build process does not have
3950
// esModuleInterop enabled but the code-server one does. As a result depending on where
4051
// we execute, we either have a default attribute or we don't.
4152
//
4253
// I can't enable esModuleInterop in VS Code's build process as it breaks and spits out
43-
// a huge number of errors.
44-
if (vscode) {
45-
pa = new (proxyagent as any)(process.env.HTTP_PROXY)
54+
// a huge number of errors. And we can't use require as otherwise the modules won't be
55+
// included in the final product.
56+
if (inVSCode) {
57+
return new (proxyAgent as any)()
4658
} else {
47-
pa = new (proxyagent as any).default(process.env.HTTP_PROXY)
59+
return new (proxyAgent as any).default()
60+
}
61+
}
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}`)
4878
}
4979

50-
/**
51-
* None of our code ever passes in a explicit agent to the http modules but VS Code's
52-
* does sometimes but only when a user sets the http.proxy configuration.
53-
* See https://code.visualstudio.com/docs/setup/network#_legacy-proxy-server-support
54-
*
55-
* Even if they do, it's probably the same proxy so we should be fine! And those are
56-
* deprecated anyway.
57-
*/
58-
const http = require("http")
59-
const https = require("https")
60-
http.globalAgent = pa
61-
https.globalAgent = pa
80+
return shouldEnable
6281
}

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)