From 2f5149886d931c807f4b08ae02c10f1c036c03d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 27 Nov 2021 07:29:44 +0200 Subject: [PATCH 1/2] Respect http.proxyStrictSSL --- editors/code/src/config.ts | 3 +++ editors/code/src/main.ts | 6 ++++-- editors/code/src/net.ts | 33 ++++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index a3e29fc156f..28acfec057c 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -107,6 +107,9 @@ export class Config { return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"]; } + get proxyStrictSSL(): boolean { + return vscode.workspace.getConfiguration("http").get("proxyStrictSSL") || true; + } get inlayHints() { return { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 726620578c5..983aa728c64 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -198,7 +198,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi } const latestNightlyRelease = await downloadWithRetryDialog(state, async () => { - return await fetchRelease("nightly", state.githubToken, config.httpProxy); + return await fetchRelease("nightly", state.githubToken, config.httpProxy, config.proxyStrictSSL); }).catch(async (e) => { log.error(e); if (isInitialNightlyDownload) { @@ -231,6 +231,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi dest, progressTitle: "Downloading rust-analyzer extension", httpProxy: config.httpProxy, + proxyStrictSSL: config.proxyStrictSSL, }); }); @@ -361,7 +362,7 @@ async function getServer(config: Config, state: PersistentState): Promise { - return await fetchRelease(releaseTag, state.githubToken, config.httpProxy); + return await fetchRelease(releaseTag, state.githubToken, config.httpProxy, config.proxyStrictSSL); }); const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`); assert(!!artifact, `Bad release: ${JSON.stringify(release)}`); @@ -374,6 +375,7 @@ async function getServer(config: Config, state: PersistentState): Promise { const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`; @@ -36,10 +47,13 @@ export async function fetchRelease( 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 }); + let options: any = {}; + if (proxyStrictSSL) { + options["rejectUnauthorized"] = false; + } + const agent = makeHttpAgent(httpProxy, options); + return fetch(requestUrl, { headers: headers, agent: agent }); })(); if (!response.ok) { @@ -84,6 +98,7 @@ interface DownloadOpts { mode?: number; gunzip?: boolean; httpProxy?: string; + proxyStrictSSL: boolean; } export async function download(opts: DownloadOpts) { @@ -103,7 +118,7 @@ export async function download(opts: DownloadOpts) { }, async (progress, _cancellationToken) => { let lastPercentage = 0; - await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => { + await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, opts.proxyStrictSSL, (readBytes, totalBytes) => { const newPercentage = Math.round((readBytes / totalBytes) * 100); if (newPercentage !== lastPercentage) { progress.report({ @@ -168,6 +183,7 @@ async function downloadFile( mode: number | undefined, gunzip: boolean, httpProxy: string | null | undefined, + proxyStrictSSL: boolean, onProgress: (readBytes: number, totalBytes: number) => void ): Promise { const urlString = url.toString(); @@ -175,10 +191,13 @@ async function downloadFile( const res = await (() => { if (httpProxy) { log.debug(`Downloading ${urlString} via proxy: ${httpProxy}`); - return fetch(urlString, { agent: new HttpsProxyAgent(httpProxy) }); } - - return fetch(urlString); + let options: any = {}; + if (proxyStrictSSL) { + options["rejectUnauthorized"] = false; + } + const agent = makeHttpAgent(httpProxy, options); + return fetch(urlString, { agent: agent }); })(); if (!res.ok) { From 7d815b862f026d8223b4f40d6dde0bda95af40da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sun, 28 Nov 2021 10:54:35 +0200 Subject: [PATCH 2/2] Refactor proxy settings --- editors/code/src/config.ts | 20 +++++++++++++------- editors/code/src/main.ts | 10 ++++------ editors/code/src/net.ts | 32 +++++++++++++++----------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 28acfec057c..81cf5d28b7e 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -8,6 +8,11 @@ const NIGHTLY_TAG = "nightly"; export type RunnableEnvCfg = undefined | Record | { mask?: string; env: Record }[]; +export class ProxySettings { + proxy?: string = undefined; + strictSSL: boolean = true; +} + export class Config { readonly extensionId = "matklad.rust-analyzer"; @@ -99,16 +104,17 @@ 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 + get proxySettings(): ProxySettings { + const proxy = vscode .workspace .getConfiguration('http') - .get("proxy")!; + .get("proxy")! || process.env["https_proxy"] || process.env["HTTPS_PROXY"]; + const strictSSL = vscode.workspace.getConfiguration("http").get("proxyStrictSSL") || true; - return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"]; - } - get proxyStrictSSL(): boolean { - return vscode.workspace.getConfiguration("http").get("proxyStrictSSL") || true; + return { + proxy: proxy, + strictSSL: strictSSL, + }; } get inlayHints() { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 983aa728c64..734f2245036 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -198,7 +198,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi } const latestNightlyRelease = await downloadWithRetryDialog(state, async () => { - return await fetchRelease("nightly", state.githubToken, config.httpProxy, config.proxyStrictSSL); + return await fetchRelease("nightly", state.githubToken, config.proxySettings); }).catch(async (e) => { log.error(e); if (isInitialNightlyDownload) { @@ -230,8 +230,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi url: artifact.browser_download_url, dest, progressTitle: "Downloading rust-analyzer extension", - httpProxy: config.httpProxy, - proxyStrictSSL: config.proxyStrictSSL, + proxySettings: config.proxySettings, }); }); @@ -362,7 +361,7 @@ async function getServer(config: Config, state: PersistentState): Promise { - return await fetchRelease(releaseTag, state.githubToken, config.httpProxy, config.proxyStrictSSL); + return await fetchRelease(releaseTag, state.githubToken, config.proxySettings); }); const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`); assert(!!artifact, `Bad release: ${JSON.stringify(release)}`); @@ -374,8 +373,7 @@ async function getServer(config: Config, state: PersistentState): Promise { const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`; @@ -45,14 +45,14 @@ export async function fetchRelease( } const response = await (() => { - if (httpProxy) { - log.debug(`Fetching release metadata via proxy: ${httpProxy}`); + if (proxySettings.proxy) { + log.debug(`Fetching release metadata via proxy: ${proxySettings.proxy}`); } - let options: any = {}; - if (proxyStrictSSL) { + const options: any = {}; + if (proxySettings.strictSSL) { options["rejectUnauthorized"] = false; } - const agent = makeHttpAgent(httpProxy, options); + const agent = makeHttpAgent(proxySettings.proxy, options); return fetch(requestUrl, { headers: headers, agent: agent }); })(); @@ -97,8 +97,7 @@ interface DownloadOpts { dest: vscode.Uri; mode?: number; gunzip?: boolean; - httpProxy?: string; - proxyStrictSSL: boolean; + proxySettings: ProxySettings; } export async function download(opts: DownloadOpts) { @@ -118,7 +117,7 @@ export async function download(opts: DownloadOpts) { }, async (progress, _cancellationToken) => { let lastPercentage = 0; - await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, opts.proxyStrictSSL, (readBytes, totalBytes) => { + await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.proxySettings, (readBytes, totalBytes) => { const newPercentage = Math.round((readBytes / totalBytes) * 100); if (newPercentage !== lastPercentage) { progress.report({ @@ -182,21 +181,20 @@ async function downloadFile( destFilePath: vscode.Uri, mode: number | undefined, gunzip: boolean, - httpProxy: string | null | undefined, - proxyStrictSSL: boolean, + proxySettings: ProxySettings, onProgress: (readBytes: number, totalBytes: number) => void ): Promise { const urlString = url.toString(); const res = await (() => { - if (httpProxy) { - log.debug(`Downloading ${urlString} via proxy: ${httpProxy}`); + if (proxySettings.proxy) { + log.debug(`Downloading ${urlString} via proxy: ${proxySettings.proxy}`); } - let options: any = {}; - if (proxyStrictSSL) { + const options: any = {}; + if (proxySettings.strictSSL) { options["rejectUnauthorized"] = false; } - const agent = makeHttpAgent(httpProxy, options); + const agent = makeHttpAgent(proxySettings.proxy, options); return fetch(urlString, { agent: agent }); })();