11import { logger } from "@coder/logger"
22import * as http from "http"
3+ import * as url from "url"
34import * as proxyagent from "proxy-agent"
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 for $HTTP_PROXY and $HTTPS_PROXY support.
89 * - https://github.com/cdr/code-server/issues/124
910 * - https://www.npmjs.com/package/proxy-agent
1011 *
@@ -15,57 +16,51 @@ import * as proxyagent from "proxy-agent"
1516 */
1617
1718/**
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.
2021 *
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.
2324 *
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
2728 *
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.
3131 *
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
3535 */
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 )
4044 }
4145
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+ }
4352
44- let pa : http . Agent
53+ function newProxyAgent ( inVSCode : boolean , for : " http" | "https" , proxyURL : string ) : http . Agent {
4554 // The reasoning for this split is that VS Code's build process does not have
4655 // esModuleInterop enabled but the code-server one does. As a result depending on where
4756 // we execute, we either have a default attribute or we don't.
4857 //
4958 // 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 )
5363 } else {
54- pa = new ( proxyagent as any ) . default ( proxyURL )
64+ return new ( proxyagent as any ) . default ( opts )
5565 }
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
7166}
0 commit comments