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/net.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'editors/code/src/net.ts') diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts index d39dc1baf..07ebc615c 100644 --- a/editors/code/src/net.ts +++ b/editors/code/src/net.ts @@ -1,4 +1,6 @@ import fetch from "node-fetch"; +var HttpsProxyAgent = require('https-proxy-agent'); + import * as vscode from "vscode"; import * as stream from "stream"; import * as crypto from "crypto"; @@ -17,6 +19,7 @@ const REPO = "rust-analyzer"; export async function fetchRelease( releaseTag: string, githubToken: string | null | undefined, + httpProxy: string | null | undefined, ): 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