1
1
import { logger } from "@coder/logger"
2
2
import * as http from "http"
3
+ import * as url from "url"
3
4
import * as proxyagent from "proxy-agent"
4
5
5
6
/**
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 for $HTTP_PROXY and $HTTPS_PROXY support.
8
9
* - https://github.com/cdr/code-server/issues/124
9
10
* - https://www.npmjs.com/package/proxy-agent
10
11
*
@@ -15,57 +16,51 @@ import * as proxyagent from "proxy-agent"
15
16
*/
16
17
17
18
/**
18
- * monkeyPatch patches the node http and https modules to route all requests through the
19
- * agent we get from the proxy-agent package.
19
+ * monkeyPatch patches the node http, https modules to route all requests through the
20
+ * agents we get from the proxy-agent package.
20
21
*
21
- * We do not support $HTTPS_PROXY here as it's equivalent in proxy- agent.
22
- * See the mapping at https://www.npmjs.com/package/proxy-agent
22
+ * This approach only works if there is no code specifying an explicit agent when making
23
+ * a request.
23
24
*
24
- * I guess with most proxies support both HTTP and HTTPS proxying on the same port and
25
- * so two variables aren't required anymore. And there's plenty of SOCKS proxies too where
26
- * it wouldn't make sense to have two variables.
25
+ * None of our code ever passes in a explicit agent to the http,https modules.
26
+ * VS Code's does sometimes but only when a user sets the http.proxy configuration.
27
+ * See https://code.visualstudio.com/docs/setup/network#_legacy-proxy-server-support
27
28
*
28
- * It's the most performant/secure setup as using a HTTP proxy for HTTP requests allows
29
- * for caching but then using a HTTPS proxy for HTTPS requests gives full end to end
30
- * security.
29
+ * Even if they do, it's probably the same proxy so we should be fine! And those knobs
30
+ * are deprecated anyway.
31
31
*
32
- * See https://stackoverflow.com/a/10442767/4283659 for HTTP vs HTTPS proxy.
33
- * To be clear, both support HTTP/ HTTPS resources, the difference is in how they fetch
34
- * them.
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
35
35
*/
36
- export function monkeyPatch ( vscode : boolean ) : void {
37
- const proxyURL = process . env . HTTP_PROXY || process . env . http_proxy
38
- if ( ! proxyURL ) {
39
- return
36
+ 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 )
40
44
}
41
45
42
- logger . debug ( `using $HTTP_PROXY ${ proxyURL } ` )
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 )
50
+ }
51
+ }
43
52
44
- let pa : http . Agent
53
+ function newProxyAgent ( inVSCode : boolean , for : " http" | "https" , proxyURL : string ) : http . Agent {
45
54
// The reasoning for this split is that VS Code's build process does not have
46
55
// esModuleInterop enabled but the code-server one does. As a result depending on where
47
56
// we execute, we either have a default attribute or we don't.
48
57
//
49
58
// I can't enable esModuleInterop in VS Code's build process as it breaks and spits out
50
- // a huge number of errors.
51
- if ( vscode ) {
52
- pa = new ( proxyagent as any ) ( proxyURL )
59
+ // a huge number of errors. And we can't use require as otherwise the modules won't be
60
+ // included in the final product.
61
+ if ( inVSCode ) {
62
+ return new ( proxyagent as any ) ( opts )
53
63
} else {
54
- pa = new ( proxyagent as any ) . default ( proxyURL )
64
+ return new ( proxyagent as any ) . default ( opts )
55
65
}
56
-
57
- // None of our code ever passes in a explicit agent to the http modules but VS Code's
58
- // does sometimes but only when a user sets the http.proxy configuration.
59
- // See https://code.visualstudio.com/docs/setup/network#_legacy-proxy-server-support
60
- //
61
- // Even if they do, it's probably the same proxy so we should be fine! And those are
62
- // deprecated anyway. In fact, they implemented it incorrectly as they won't retrieve
63
- // HTTPS resources over a HTTP proxy which is perfectly valid! Both HTTP and HTTPS proxies
64
- // support HTTP/HTTPS resources.
65
- //
66
- // See https://stackoverflow.com/a/10442767/4283659
67
- const http = require ( "http" )
68
- const https = require ( "https" )
69
- http . globalAgent = pa
70
- https . globalAgent = pa
71
66
}
0 commit comments