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/src/config.ts | 8 ++++++++ editors/code/src/main.ts | 6 ++++-- editors/code/src/net.ts | 25 ++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) (limited to 'editors/code/src') 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