]> git.lizzy.rs Git - rust.git/commitdiff
Refactor proxy settings
authorLaurențiu Nicola <lnicola@dend.ro>
Sun, 28 Nov 2021 08:54:35 +0000 (10:54 +0200)
committerLaurențiu Nicola <lnicola@dend.ro>
Sun, 28 Nov 2021 08:54:35 +0000 (10:54 +0200)
editors/code/src/config.ts
editors/code/src/main.ts
editors/code/src/net.ts

index 28acfec057ca9811c112c500a139535bedc897ae..81cf5d28b7e2a2ad5dd31a6a1d4eb8585c390d1f 100644 (file)
@@ -8,6 +8,11 @@ const NIGHTLY_TAG = "nightly";
 
 export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];
 
+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<UpdatesChannel>("updates.channel"); }
     get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
     get traceExtension() { return this.get<boolean>("trace.extension"); }
-    get httpProxy() {
-        const httpProxy = vscode
+    get proxySettings(): ProxySettings {
+        const proxy = vscode
             .workspace
             .getConfiguration('http')
-            .get<null | string>("proxy")!;
+            .get<null | string>("proxy")! || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
+        const strictSSL = vscode.workspace.getConfiguration("http").get<boolean>("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() {
index 983aa728c64fc894c1c98d425b384641e5198197..734f224503632a73db8ede9a49bca6b72f5134ac 100644 (file)
@@ -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<string
 
     const releaseTag = config.package.releaseTag;
     const release = await downloadWithRetryDialog(state, async () => {
-        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<string
             progressTitle: "Downloading rust-analyzer server",
             gunzip: true,
             mode: 0o755,
-            httpProxy: config.httpProxy,
-            proxyStrictSSL: config.proxyStrictSSL,
+            proxySettings: config.proxySettings,
         });
     });
 
index 96a7d61ff1fbf10fd884f3d9b5d56e90e97d4428..103aed360e79aefb408366eb0a9d6dd7585b1cea 100644 (file)
@@ -11,6 +11,7 @@ import * as path from "path";
 import { log, assert } from "./util";
 import * as url from "url";
 import * as https from "https";
+import { ProxySettings } from "./config";
 
 const pipeline = util.promisify(stream.pipeline);
 
@@ -29,8 +30,7 @@ function makeHttpAgent(proxy: string | null | undefined, options?: https.AgentOp
 export async function fetchRelease(
     releaseTag: string,
     githubToken: string | null | undefined,
-    httpProxy: string | null | undefined,
-    proxyStrictSSL: boolean,
+    proxySettings: ProxySettings,
 ): Promise<GithubRelease> {
 
     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<void> {
     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 });
     })();