fix: infinite proxy loop (#4676)
I think the problem is that when a proxy is not in use proxy-agent returns the global agent...which is itself since we set it globally, causing the loop. VS Code already covers proxies meaning we only need to do it in our own requests so to fix this pass in the agent in the version fetch request instead of overidding globally. Also avoid proxy-from-env and pass in the proxy URI instead as both http_proxy and https_proxy can be used for either http or https requests but it does not allow that.
This commit is contained in:
@ -25,3 +25,5 @@ export const rootPath = path.resolve(__dirname, "../..")
|
||||
export const vsRootPath = path.join(rootPath, "vendor/modules/code-oss-dev")
|
||||
export const tmpdir = path.join(os.tmpdir(), "code-server")
|
||||
export const isDevMode = commit === "development"
|
||||
export const httpProxyUri =
|
||||
process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
|
||||
|
@ -2,12 +2,9 @@ import { logger } from "@coder/logger"
|
||||
import { optionDescriptions, parse, readConfigFile, setDefaults, shouldOpenInExistingInstance } from "./cli"
|
||||
import { commit, version } from "./constants"
|
||||
import { openInExistingInstance, runCodeServer, runVsCodeCli, shouldSpawnCliProcess } from "./main"
|
||||
import { monkeyPatchProxyProtocols } from "./proxy_agent"
|
||||
import { isChild, wrapper } from "./wrapper"
|
||||
|
||||
async function entry(): Promise<void> {
|
||||
monkeyPatchProxyProtocols()
|
||||
|
||||
// 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
|
||||
|
@ -1,71 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Coder Technologies. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import ProxyAgent from "proxy-agent"
|
||||
import { getProxyForUrl } from "proxy-from-env"
|
||||
|
||||
/**
|
||||
* This file has nothing to do with the code-server proxy.
|
||||
* It is to support $HTTP_PROXY, $HTTPS_PROXY and $NO_PROXY.
|
||||
*
|
||||
* - https://github.com/cdr/code-server/issues/124
|
||||
* - https://www.npmjs.com/package/proxy-agent
|
||||
* - https://www.npmjs.com/package/proxy-from-env
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* monkeyPatch patches the node http,https modules to route all requests through the
|
||||
* agent we get from the proxy-agent package.
|
||||
*
|
||||
* This approach only works if there is no code specifying an explicit agent when making
|
||||
* a request.
|
||||
*
|
||||
* None of our code ever passes in a explicit agent to the http,https modules.
|
||||
* 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 knobs
|
||||
* are deprecated anyway.
|
||||
*/
|
||||
export function monkeyPatchProxyProtocols(): void {
|
||||
if (!shouldEnableProxy()) {
|
||||
return
|
||||
}
|
||||
|
||||
const http = require("http")
|
||||
const https = require("https")
|
||||
|
||||
// If we do not pass in a proxy URL, proxy-agent will get the URL from the environment.
|
||||
// See https://www.npmjs.com/package/proxy-from-env.
|
||||
// Also see shouldEnableProxy.
|
||||
const pa = new ProxyAgent()
|
||||
http.globalAgent = pa
|
||||
https.globalAgent = pa
|
||||
}
|
||||
|
||||
const sampleUrls = [new URL("http://example.com"), new URL("https://example.com")]
|
||||
|
||||
// If they have $NO_PROXY set to example.com then this check won't work!
|
||||
// But that's drastically unlikely.
|
||||
export function shouldEnableProxy(): boolean {
|
||||
const testedProxyEndpoints = sampleUrls.map((url) => {
|
||||
return {
|
||||
url,
|
||||
proxyUrl: getProxyForUrl(url.toString()),
|
||||
}
|
||||
})
|
||||
|
||||
let shouldEnable = false
|
||||
|
||||
for (const { url, proxyUrl } of testedProxyEndpoints) {
|
||||
if (proxyUrl) {
|
||||
console.debug(`${url.protocol} -- Using "${proxyUrl}"`)
|
||||
shouldEnable = true
|
||||
}
|
||||
}
|
||||
|
||||
return shouldEnable
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
import { field, logger } from "@coder/logger"
|
||||
import * as http from "http"
|
||||
import * as https from "https"
|
||||
import ProxyAgent from "proxy-agent"
|
||||
import * as semver from "semver"
|
||||
import * as url from "url"
|
||||
import { version } from "./constants"
|
||||
import { httpProxyUri, version } from "./constants"
|
||||
import { SettingsProvider, UpdateSettings } from "./settings"
|
||||
|
||||
export interface Update {
|
||||
@ -102,8 +103,10 @@ export class UpdateProvider {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = (uri: string): void => {
|
||||
logger.debug("Making request", field("uri", uri))
|
||||
const httpx = uri.startsWith("https") ? https : http
|
||||
const client = httpx.get(uri, { headers: { "User-Agent": "code-server" } }, (response) => {
|
||||
const isHttps = uri.startsWith("https")
|
||||
const agent = httpProxyUri ? new ProxyAgent(httpProxyUri) : undefined
|
||||
const httpx = isHttps ? https : http
|
||||
const client = httpx.get(uri, { headers: { "User-Agent": "code-server" }, agent }, (response) => {
|
||||
if (!response.statusCode || response.statusCode < 200 || response.statusCode >= 400) {
|
||||
response.destroy()
|
||||
return reject(new Error(`${uri}: ${response.statusCode || "500"}`))
|
||||
|
Reference in New Issue
Block a user