Skip to content

Commit a164820

Browse files
bors[bot]lnicola
andauthored
Merge #10871
10871: Respect `http.proxyStrictSSL` r=lnicola a=lnicola Closes #10866 Currently untested. Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 393cbd0 + 7d815b8 commit a164820

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

editors/code/src/config.ts

Lines changed: 13 additions & 4 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,13 +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"];
114+
return {
115+
proxy: proxy,
116+
strictSSL: strictSSL,
117+
};
109118
}
110119

111120
get inlayHints() {

editors/code/src/main.ts

Lines changed: 4 additions & 4 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);
201+
return await fetchRelease("nightly", state.githubToken, config.proxySettings);
202202
}).catch(async (e) => {
203203
log.error(e);
204204
if (isInitialNightlyDownload) {
@@ -230,7 +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,
233+
proxySettings: config.proxySettings,
234234
});
235235
});
236236

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

362362
const releaseTag = config.package.releaseTag;
363363
const release = await downloadWithRetryDialog(state, async () => {
364-
return await fetchRelease(releaseTag, state.githubToken, config.httpProxy);
364+
return await fetchRelease(releaseTag, state.githubToken, config.proxySettings);
365365
});
366366
const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`);
367367
assert(!!artifact, `Bad release: ${JSON.stringify(release)}`);
@@ -373,7 +373,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
373373
progressTitle: "Downloading rust-analyzer server",
374374
gunzip: true,
375375
mode: 0o755,
376-
httpProxy: config.httpProxy,
376+
proxySettings: config.proxySettings,
377377
});
378378
});
379379

editors/code/src/net.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@ import * as zlib from "zlib";
99
import * as util from "util";
1010
import * as path from "path";
1111
import { log, assert } from "./util";
12+
import * as url from "url";
13+
import * as https from "https";
14+
import { ProxySettings } from "./config";
1215

1316
const pipeline = util.promisify(stream.pipeline);
1417

1518
const GITHUB_API_ENDPOINT_URL = "https://api.github.com";
1619
const OWNER = "rust-analyzer";
1720
const REPO = "rust-analyzer";
1821

22+
function makeHttpAgent(proxy: string | null | undefined, options?: https.AgentOptions) {
23+
if (proxy) {
24+
return new HttpsProxyAgent(proxy, { ...options, ...url.parse(proxy) });
25+
} else {
26+
return new https.Agent(options);
27+
}
28+
}
29+
1930
export async function fetchRelease(
2031
releaseTag: string,
2132
githubToken: string | null | undefined,
22-
httpProxy: string | null | undefined,
33+
proxySettings: ProxySettings,
2334
): Promise<GithubRelease> {
2435

2536
const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`;
@@ -34,12 +45,15 @@ export async function fetchRelease(
3445
}
3546

3647
const response = await (() => {
37-
if (httpProxy) {
38-
log.debug(`Fetching release metadata via proxy: ${httpProxy}`);
39-
return fetch(requestUrl, { headers: headers, agent: new HttpsProxyAgent(httpProxy) });
48+
if (proxySettings.proxy) {
49+
log.debug(`Fetching release metadata via proxy: ${proxySettings.proxy}`);
4050
}
41-
42-
return fetch(requestUrl, { headers: headers });
51+
const options: any = {};
52+
if (proxySettings.strictSSL) {
53+
options["rejectUnauthorized"] = false;
54+
}
55+
const agent = makeHttpAgent(proxySettings.proxy, options);
56+
return fetch(requestUrl, { headers: headers, agent: agent });
4357
})();
4458

4559
if (!response.ok) {
@@ -83,7 +97,7 @@ interface DownloadOpts {
8397
dest: vscode.Uri;
8498
mode?: number;
8599
gunzip?: boolean;
86-
httpProxy?: string;
100+
proxySettings: ProxySettings;
87101
}
88102

89103
export async function download(opts: DownloadOpts) {
@@ -103,7 +117,7 @@ export async function download(opts: DownloadOpts) {
103117
},
104118
async (progress, _cancellationToken) => {
105119
let lastPercentage = 0;
106-
await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => {
120+
await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.proxySettings, (readBytes, totalBytes) => {
107121
const newPercentage = Math.round((readBytes / totalBytes) * 100);
108122
if (newPercentage !== lastPercentage) {
109123
progress.report({
@@ -167,18 +181,21 @@ async function downloadFile(
167181
destFilePath: vscode.Uri,
168182
mode: number | undefined,
169183
gunzip: boolean,
170-
httpProxy: string | null | undefined,
184+
proxySettings: ProxySettings,
171185
onProgress: (readBytes: number, totalBytes: number) => void
172186
): Promise<void> {
173187
const urlString = url.toString();
174188

175189
const res = await (() => {
176-
if (httpProxy) {
177-
log.debug(`Downloading ${urlString} via proxy: ${httpProxy}`);
178-
return fetch(urlString, { agent: new HttpsProxyAgent(httpProxy) });
190+
if (proxySettings.proxy) {
191+
log.debug(`Downloading ${urlString} via proxy: ${proxySettings.proxy}`);
179192
}
180-
181-
return fetch(urlString);
193+
const options: any = {};
194+
if (proxySettings.strictSSL) {
195+
options["rejectUnauthorized"] = false;
196+
}
197+
const agent = makeHttpAgent(proxySettings.proxy, options);
198+
return fetch(urlString, { agent: agent });
182199
})();
183200

184201
if (!res.ok) {

0 commit comments

Comments
 (0)