From 77b7c96aeabb5a187b7a29cbd474f6f9b2260613 Mon Sep 17 00:00:00 2001 From: "Kam Y. Tse" Date: Sun, 7 Mar 2021 17:55:43 +0800 Subject: Make extension respect http proxy settings --- editors/code/package-lock.json | 17 +++++++---------- editors/code/package.json | 1 + editors/code/src/config.ts | 8 ++++++++ editors/code/src/main.ts | 6 ++++-- editors/code/src/net.ts | 25 ++++++++++++++++++++++--- 5 files changed, 42 insertions(+), 15 deletions(-) (limited to 'editors/code') diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 9d0d1d4ec..198c17556 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json @@ -9,6 +9,7 @@ "version": "0.4.0-dev", "license": "MIT OR Apache-2.0", "dependencies": { + "https-proxy-agent": "^5.0.0", "node-fetch": "^2.6.1", "vscode-languageclient": "^7.1.0-next.4" }, @@ -515,7 +516,6 @@ "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", - "dev": true, "dependencies": { "debug": "4" }, @@ -830,6 +830,7 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", + "fsevents": "~2.3.1", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -959,7 +960,6 @@ "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -1759,7 +1759,6 @@ "version": "5.0.0", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz", "integrity": "sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA==", - "dev": true, "dependencies": { "agent-base": "6", "debug": "4" @@ -2236,8 +2235,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/mute-stream": { "version": "0.0.8", @@ -2682,6 +2680,9 @@ "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.39.1.tgz", "integrity": "sha512-9rfr0Z6j+vE+eayfNVFr1KZ+k+jiUl2+0e4quZafy1x6SFCjzFspfRSO2ZZQeWeX9noeDTUDgg6eCENiEPFvQg==", "dev": true, + "dependencies": { + "fsevents": "~2.3.1" + }, "bin": { "rollup": "dist/bin/rollup" }, @@ -3843,7 +3844,6 @@ "version": "6.0.2", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", - "dev": true, "requires": { "debug": "4" } @@ -4190,7 +4190,6 @@ "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -4798,7 +4797,6 @@ "version": "5.0.0", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.0.tgz", "integrity": "sha512-EkYm5BcKUGiduxzSt3Eppko+PiNWNEpa4ySk9vTC6wDsQJW9rHSa+UhGNJoRYp7bz6Ht1eaRIa6QaJqO5rCFbA==", - "dev": true, "requires": { "agent-base": "6", "debug": "4" @@ -5175,8 +5173,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "mute-stream": { "version": "0.0.8", diff --git a/editors/code/package.json b/editors/code/package.json index 1987364bc..68f58d3ca 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -35,6 +35,7 @@ "test": "node ./out/tests/runTests.js" }, "dependencies": { + "https-proxy-agent": "^5.0.0", "node-fetch": "^2.6.1", "vscode-languageclient": "^7.1.0-next.4" }, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index ddb5cfbd3..82f0a0566 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -100,6 +100,14 @@ export class Config { get channel() { return this.get("updates.channel"); } get askBeforeDownload() { return this.get("updates.askBeforeDownload"); } get traceExtension() { return this.get("trace.extension"); } + get httpProxy() { + const httpProxy = vscode + .workspace + .getConfiguration('http') + .get("proxy")!; + + return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"]; + } get inlayHints() { return { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 00393d6e8..1be4f1758 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -183,7 +183,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi } const release = await downloadWithRetryDialog(state, async () => { - return await fetchRelease("nightly", state.githubToken); + return await fetchRelease("nightly", state.githubToken, config.httpProxy); }).catch(async (e) => { log.error(e); if (state.releaseId === undefined) { // Show error only for the initial download @@ -209,6 +209,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi url: artifact.browser_download_url, dest, progressTitle: "Downloading rust-analyzer extension", + httpProxy: config.httpProxy, }); }); @@ -331,7 +332,7 @@ async function getServer(config: Config, state: PersistentState): Promise { - return await fetchRelease(releaseTag, state.githubToken); + return await fetchRelease(releaseTag, state.githubToken, config.httpProxy); }); const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`); assert(!!artifact, `Bad release: ${JSON.stringify(release)}`); @@ -343,6 +344,7 @@ async function getServer(config: Config, state: PersistentState): Promise { const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`; @@ -30,7 +33,14 @@ export async function fetchRelease( headers.Authorization = "token " + githubToken; } - const response = await fetch(requestUrl, { headers: headers }); + const response = await (() => { + if (httpProxy) { + log.debug(`Fetching release metadata via proxy: ${httpProxy}`); + return fetch(requestUrl, { headers: headers, agent: new HttpsProxyAgent(httpProxy) }); + } + + return fetch(requestUrl, { headers: headers }); + })(); if (!response.ok) { log.error("Error fetching artifact release info", { @@ -73,6 +83,7 @@ interface DownloadOpts { dest: string; mode?: number; gunzip?: boolean; + httpProxy?: string; } export async function download(opts: DownloadOpts) { @@ -91,7 +102,7 @@ export async function download(opts: DownloadOpts) { }, async (progress, _cancellationToken) => { let lastPercentage = 0; - await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => { + await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => { const newPercentage = Math.round((readBytes / totalBytes) * 100); if (newPercentage !== lastPercentage) { progress.report({ @@ -113,9 +124,17 @@ async function downloadFile( destFilePath: fs.PathLike, mode: number | undefined, gunzip: boolean, + httpProxy: string | null | undefined, onProgress: (readBytes: number, totalBytes: number) => void ): Promise { - const res = await fetch(url); + const res = await (() => { + if (httpProxy) { + log.debug(`Downloading ${url} via proxy: ${httpProxy}`); + return fetch(url, { agent: new HttpsProxyAgent(httpProxy) }); + } + + return fetch(url); + })(); if (!res.ok) { log.error("Error", res.status, "while downloading file from", url); -- cgit v1.2.3