forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_agent.ts
More file actions
62 lines (57 loc) · 2.31 KB
/
Copy pathproxy_agent.ts
File metadata and controls
62 lines (57 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { logger } from "@coder/logger"
import * as http from "http"
import * as proxyagent from "proxy-agent"
/**
* This file does not have anything to do with the code-server proxy.
* It's for $HTTP_PROXY support!
* - https://github.com/cdr/code-server/issues/124
* - https://www.npmjs.com/package/proxy-agent
*
* This file exists in two locations:
* - src/node/proxy_agent.ts
* - lib/vscode/src/vs/base/node/proxy_agent.ts
* The second is a symlink to the first.
*/
/**
* monkeyPatch patches the node HTTP/HTTPS library to route all requests through our
* custom agent from the proxyAgent package.
*/
export function monkeyPatch(vscode: boolean): void {
// We do not support HTTPS_PROXY here to avoid confusion. proxy-agent will automatically
// use the correct protocol to connect to the proxy depending on the requested URL.
//
// We could implement support ourselves to allow people to configure the proxy used for
// HTTPS vs HTTP but there doesn't seem to be much value in that.
//
// At least of right now, it'd just be plain confusing to support HTTPS_PROXY when proxy-agent
// will still use HTTP to hit it for HTTP requests.
const proxyURL = process.env.HTTP_PROXY || process.env.http_proxy
if (!proxyURL) {
return
}
logger.debug(`using $HTTP_PROXY ${process.env.HTTP_PROXY}`)
let pa: http.Agent
// The reasoning for this split is that VS Code's build process does not have
// esModuleInterop enabled but the code-server one does. As a result depending on where
// we execute, we either have a default attribute or we don't.
//
// I can't enable esModuleInterop in VS Code's build process as it breaks and spits out
// a huge number of errors.
if (vscode) {
pa = new (proxyagent as any)(process.env.HTTP_PROXY)
} else {
pa = new (proxyagent as any).default(process.env.HTTP_PROXY)
}
/**
* None of our code ever passes in a explicit agent to the http modules but VS Code's
* does sometimes but only when a user sets the http.proxy configuration.
* See https://code.visualstudio.com/docs/setup/network#_legacy-proxy-server-support
*
* Even if they do, it's probably the same proxy so we should be fine! And those are
* deprecated anyway.
*/
const http = require("http")
const https = require("https")
http.globalAgent = pa
https.globalAgent = pa
}