|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import { Url, parse as parseUrl } from 'url'; |
| 9 | +import HttpProxyAgent = require('http-proxy-agent'); |
| 10 | +import HttpsProxyAgent = require('https-proxy-agent'); |
| 11 | +import {workspace} from 'vscode'; |
| 12 | + |
| 13 | +function getSystemProxyURL(requestURL: Url): string { |
| 14 | + if (requestURL.protocol === 'http:') { |
| 15 | + return process.env.HTTP_PROXY || process.env.http_proxy || null; |
| 16 | + } else if (requestURL.protocol === 'https:') { |
| 17 | + return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null; |
| 18 | + } |
| 19 | + |
| 20 | + return null; |
| 21 | +} |
| 22 | + |
| 23 | +export function getProxyAgent(requestURL: Url): any { |
| 24 | + const vsConfigProxyUrl = workspace.getConfiguration().get<string>('http.proxy'); |
| 25 | + const proxyURL = vsConfigProxyUrl || getSystemProxyURL(requestURL); |
| 26 | + |
| 27 | + if (!proxyURL) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + const proxyEndpoint = parseUrl(proxyURL); |
| 32 | + |
| 33 | + if (!/^https?:$/.test(proxyEndpoint.protocol)) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const strictSSL = workspace.getConfiguration().get('http.proxyStrictSSL', true); |
| 38 | + const opts = { |
| 39 | + host: proxyEndpoint.hostname, |
| 40 | + port: Number(proxyEndpoint.port), |
| 41 | + auth: proxyEndpoint.auth, |
| 42 | + rejectUnauthorized: strictSSL |
| 43 | + }; |
| 44 | + |
| 45 | + return requestURL.protocol === 'http:' ? new HttpProxyAgent(opts) : new HttpsProxyAgent(opts); |
| 46 | +} |
0 commit comments