Skip to content

Commit 77b7c96

Browse files
committed
Make extension respect http proxy settings
1 parent f0b7c02 commit 77b7c96

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

editors/code/package-lock.json

Lines changed: 7 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editors/code/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"test": "node ./out/tests/runTests.js"
3636
},
3737
"dependencies": {
38+
"https-proxy-agent": "^5.0.0",
3839
"node-fetch": "^2.6.1",
3940
"vscode-languageclient": "^7.1.0-next.4"
4041
},

editors/code/src/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ export class Config {
100100
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
101101
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
102102
get traceExtension() { return this.get<boolean>("trace.extension"); }
103+
get httpProxy() {
104+
const httpProxy = vscode
105+
.workspace
106+
.getConfiguration('http')
107+
.get<null | string>("proxy")!;
108+
109+
return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
110+
}
103111

104112
get inlayHints() {
105113
return {

editors/code/src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
183183
}
184184

185185
const release = await downloadWithRetryDialog(state, async () => {
186-
return await fetchRelease("nightly", state.githubToken);
186+
return await fetchRelease("nightly", state.githubToken, config.httpProxy);
187187
}).catch(async (e) => {
188188
log.error(e);
189189
if (state.releaseId === undefined) { // Show error only for the initial download
@@ -209,6 +209,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
209209
url: artifact.browser_download_url,
210210
dest,
211211
progressTitle: "Downloading rust-analyzer extension",
212+
httpProxy: config.httpProxy,
212213
});
213214
});
214215

@@ -331,7 +332,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
331332

332333
const releaseTag = config.package.releaseTag;
333334
const release = await downloadWithRetryDialog(state, async () => {
334-
return await fetchRelease(releaseTag, state.githubToken);
335+
return await fetchRelease(releaseTag, state.githubToken, config.httpProxy);
335336
});
336337
const artifact = release.assets.find(artifact => artifact.name === `rust-analyzer-${platform}.gz`);
337338
assert(!!artifact, `Bad release: ${JSON.stringify(release)}`);
@@ -343,6 +344,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
343344
progressTitle: "Downloading rust-analyzer server",
344345
gunzip: true,
345346
mode: 0o755,
347+
httpProxy: config.httpProxy,
346348
});
347349
});
348350

editors/code/src/net.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import fetch from "node-fetch";
2+
var HttpsProxyAgent = require('https-proxy-agent');
3+
24
import * as vscode from "vscode";
35
import * as stream from "stream";
46
import * as crypto from "crypto";
@@ -17,6 +19,7 @@ const REPO = "rust-analyzer";
1719
export async function fetchRelease(
1820
releaseTag: string,
1921
githubToken: string | null | undefined,
22+
httpProxy: string | null | undefined,
2023
): Promise<GithubRelease> {
2124

2225
const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`;
@@ -30,7 +33,14 @@ export async function fetchRelease(
3033
headers.Authorization = "token " + githubToken;
3134
}
3235

33-
const response = await fetch(requestUrl, { headers: headers });
36+
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) });
40+
}
41+
42+
return fetch(requestUrl, { headers: headers });
43+
})();
3444

3545
if (!response.ok) {
3646
log.error("Error fetching artifact release info", {
@@ -73,6 +83,7 @@ interface DownloadOpts {
7383
dest: string;
7484
mode?: number;
7585
gunzip?: boolean;
86+
httpProxy?: string;
7687
}
7788

7889
export async function download(opts: DownloadOpts) {
@@ -91,7 +102,7 @@ export async function download(opts: DownloadOpts) {
91102
},
92103
async (progress, _cancellationToken) => {
93104
let lastPercentage = 0;
94-
await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => {
105+
await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => {
95106
const newPercentage = Math.round((readBytes / totalBytes) * 100);
96107
if (newPercentage !== lastPercentage) {
97108
progress.report({
@@ -113,9 +124,17 @@ async function downloadFile(
113124
destFilePath: fs.PathLike,
114125
mode: number | undefined,
115126
gunzip: boolean,
127+
httpProxy: string | null | undefined,
116128
onProgress: (readBytes: number, totalBytes: number) => void
117129
): Promise<void> {
118-
const res = await fetch(url);
130+
const res = await (() => {
131+
if (httpProxy) {
132+
log.debug(`Downloading ${url} via proxy: ${httpProxy}`);
133+
return fetch(url, { agent: new HttpsProxyAgent(httpProxy) });
134+
}
135+
136+
return fetch(url);
137+
})();
119138

120139
if (!res.ok) {
121140
log.error("Error", res.status, "while downloading file from", url);

0 commit comments

Comments
 (0)