Skip to content

Commit 7d815b8

Browse files
committed
Refactor proxy settings
1 parent 2f51498 commit 7d815b8

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

editors/code/src/config.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const NIGHTLY_TAG = "nightly";
88

99
export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];
1010

11+
export class ProxySettings {
12+
proxy?: string = undefined;
13+
strictSSL: boolean = true;
14+
}
15+
1116
export class Config {
1217
readonly extensionId = "matklad.rust-analyzer";
1318

@@ -99,16 +104,17 @@ export class Config {
99104
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
100105
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
101106
get traceExtension() { return this.get<boolean>("trace.extension"); }
102-
get httpProxy() {
103-
const httpProxy = vscode
107+
get proxySettings(): ProxySettings {
108+
const proxy = vscode
104109
.workspace
105110
.getConfiguration('http')
106-
.get<null | string>("proxy")!;
111+
.get<null | string>("proxy")! || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
112+
const strictSSL = vscode.workspace.getConfiguration("http").get<boolean>("proxyStrictSSL") || true;
107113

108-
return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
109-
}
110-
get proxyStrictSSL(): boolean {
111-
return vscode.workspace.getConfiguration("http").get("proxyStrictSSL") || true;
114+
return {
115+
proxy: proxy,
116+
strictSSL: strictSSL,
117+
};
112118
}
113119

114120
get inlayHints() {

editors/code/src/main.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
198198
}
199199

200200
const latestNightlyRelease = await downloadWithRetryDialog(state, async () => {
201-
return await fetchRelease("nightly", state.githubToken, config.httpProxy, config.proxyStrictSSL);
201+
return await fetchRelease("nightly", state.githubToken, config.proxySettings);
202202
}).catch(async (e) => {
203203
log.error(e);
204204
if (isInitialNightlyDownload) {
@@ -230,8 +230,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
230230
url: artifact.browser_download_url,
231231
dest,
232232
progressTitle: "Downloading rust-analyzer extension",
233-
httpProxy: config.httpProxy,
234-
proxyStrictSSL: config.proxyStrictSSL,
233+
proxySettings: config.proxySettings,
235234
});
236235
});
237236

@@ -362,7 +361,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
362361

363362
const releaseTag = config.package.releaseTag;
364363
const release = await downloadWithRetryDialog(state, async () => {
365-
return await fetchRelease(releaseTag, state.githubToken, config.httpProxy, config.proxyStrictSSL);
364+
return await fetchRelease(releaseTag, state.githubToken, config.proxySettings);
366365
});
367366
const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`);
368367
assert(!!artifact, `Bad release: ${JSON.stringify(release)}`);
@@ -374,8 +373,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
374373
progressTitle: "Downloading rust-analyzer server",
375374
gunzip: true,
376375
mode: 0o755,
377-
httpProxy: config.httpProxy,
378-
proxyStrictSSL: config.proxyStrictSSL,
376+
proxySettings: config.proxySettings,
379377
});
380378
});
381379

editors/code/src/net.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as path from "path";
1111
import { log, assert } from "./util";
1212
import * as url from "url";
1313
import * as https from "https";
14+
import { ProxySettings } from "./config";
1415

1516
const pipeline = util.promisify(stream.pipeline);
1617

@@ -29,8 +30,7 @@ function makeHttpAgent(proxy: string | null | undefined, options?: https.AgentOp
2930
export async function fetchRelease(
3031
releaseTag: string,
3132
githubToken: string | null | undefined,
32-
httpProxy: string | null | undefined,
33-
proxyStrictSSL: boolean,
33+
proxySettings: ProxySettings,
3434
): Promise<GithubRelease> {
3535

3636
const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`;
@@ -45,14 +45,14 @@ export async function fetchRelease(
4545
}
4646

4747
const response = await (() => {
48-
if (httpProxy) {
49-
log.debug(`Fetching release metadata via proxy: ${httpProxy}`);
48+
if (proxySettings.proxy) {
49+
log.debug(`Fetching release metadata via proxy: ${proxySettings.proxy}`);
5050
}
51-
let options: any = {};
52-
if (proxyStrictSSL) {
51+
const options: any = {};
52+
if (proxySettings.strictSSL) {
5353
options["rejectUnauthorized"] = false;
5454
}
55-
const agent = makeHttpAgent(httpProxy, options);
55+
const agent = makeHttpAgent(proxySettings.proxy, options);
5656
return fetch(requestUrl, { headers: headers, agent: agent });
5757
})();
5858

@@ -97,8 +97,7 @@ interface DownloadOpts {
9797
dest: vscode.Uri;
9898
mode?: number;
9999
gunzip?: boolean;
100-
httpProxy?: string;
101-
proxyStrictSSL: boolean;
100+
proxySettings: ProxySettings;
102101
}
103102

104103
export async function download(opts: DownloadOpts) {
@@ -118,7 +117,7 @@ export async function download(opts: DownloadOpts) {
118117
},
119118
async (progress, _cancellationToken) => {
120119
let lastPercentage = 0;
121-
await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, opts.proxyStrictSSL, (readBytes, totalBytes) => {
120+
await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.proxySettings, (readBytes, totalBytes) => {
122121
const newPercentage = Math.round((readBytes / totalBytes) * 100);
123122
if (newPercentage !== lastPercentage) {
124123
progress.report({
@@ -182,21 +181,20 @@ async function downloadFile(
182181
destFilePath: vscode.Uri,
183182
mode: number | undefined,
184183
gunzip: boolean,
185-
httpProxy: string | null | undefined,
186-
proxyStrictSSL: boolean,
184+
proxySettings: ProxySettings,
187185
onProgress: (readBytes: number, totalBytes: number) => void
188186
): Promise<void> {
189187
const urlString = url.toString();
190188

191189
const res = await (() => {
192-
if (httpProxy) {
193-
log.debug(`Downloading ${urlString} via proxy: ${httpProxy}`);
190+
if (proxySettings.proxy) {
191+
log.debug(`Downloading ${urlString} via proxy: ${proxySettings.proxy}`);
194192
}
195-
let options: any = {};
196-
if (proxyStrictSSL) {
193+
const options: any = {};
194+
if (proxySettings.strictSSL) {
197195
options["rejectUnauthorized"] = false;
198196
}
199-
const agent = makeHttpAgent(httpProxy, options);
197+
const agent = makeHttpAgent(proxySettings.proxy, options);
200198
return fetch(urlString, { agent: agent });
201199
})();
202200

0 commit comments

Comments
 (0)