Archived
1
0

Use proxy-agent to support $HTTP_PROXY

Closes #124

This works by monkey patching the http and https modules's default agent
at runtime to the one given by the proxy-agent package.
This commit is contained in:
Anmol Sethi
2020-12-04 00:53:27 -05:00
parent cee88ad637
commit 691d44d4a8
5 changed files with 845 additions and 17 deletions

View File

@ -20,6 +20,7 @@ import { commit, version } from "./constants"
import { register } from "./routes"
import { humanPath, isFile, open } from "./util"
import { isChild, wrapper } from "./wrapper"
import * as proxyAgent from "./proxy_agent"
export const runVsCodeCli = (args: DefaultedArgs): void => {
logger.debug("forking vs code cli...")
@ -154,6 +155,8 @@ const main = async (args: DefaultedArgs): Promise<void> => {
}
async function entry(): Promise<void> {
proxyAgent.monkeyPatch(false)
// There's no need to check flags like --help or to spawn in an existing
// instance for the child process because these would have already happened in
// the parent and the child wouldn't have been spawned. We also get the

45
src/node/proxy_agent.ts Normal file
View File

@ -0,0 +1,45 @@
import { logger } from "@coder/logger"
import * as proxyagent from "proxy-agent"
import * as http from "http"
/**
* 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 {
if (!process.env.HTTP_PROXY) {
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)
}
const http = require("http")
const https = require("https")
http.globalAgent = pa
https.globalAgent = pa
}