|
| 1 | +import { logger } from "@coder/logger" |
| 2 | +import * as http from "http" |
| 3 | +import * as proxyagent from "proxy-agent" |
| 4 | + |
| 5 | +/** |
| 6 | + * This file does not have anything to do with the code-server proxy. |
| 7 | + * It's for $HTTP_PROXY support! |
| 8 | + * - https://github.com/cdr/code-server/issues/124 |
| 9 | + * - https://www.npmjs.com/package/proxy-agent |
| 10 | + * |
| 11 | + * This file exists in two locations: |
| 12 | + * - src/node/proxy_agent.ts |
| 13 | + * - lib/vscode/src/vs/base/node/proxy_agent.ts |
| 14 | + * The second is a symlink to the first. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * monkeyPatch patches the node HTTP/HTTPS library to route all requests through our |
| 19 | + * custom agent from the proxyAgent package. |
| 20 | + */ |
| 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 | + |
| 35 | + logger.debug(`using $HTTP_PROXY ${process.env.HTTP_PROXY}`) |
| 36 | + |
| 37 | + let pa: http.Agent |
| 38 | + // The reasoning for this split is that VS Code's build process does not have |
| 39 | + // esModuleInterop enabled but the code-server one does. As a result depending on where |
| 40 | + // we execute, we either have a default attribute or we don't. |
| 41 | + // |
| 42 | + // 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) |
| 46 | + } else { |
| 47 | + pa = new (proxyagent as any).default(process.env.HTTP_PROXY) |
| 48 | + } |
| 49 | + |
| 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 |
| 62 | +} |
0 commit comments