move proxy helper from workbench => base

This commit is contained in:
Benjamin Pasero
2016-01-11 09:59:29 +01:00
parent f1971c4dab
commit 0e7ed68303
4 changed files with 3 additions and 3 deletions

View File

@@ -1,63 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Url, parse as parseUrl } from 'url';
import HttpProxyAgent = require('http-proxy-agent');
import HttpsProxyAgent = require('https-proxy-agent');
function getAgent(rawRequestURL: string, proxyURL: string, strictSSL: boolean = true): any {
let requestURL = parseUrl(rawRequestURL);
let proxyEndpoint = parseUrl(proxyURL);
if (!/^https?:$/.test(proxyEndpoint.protocol)) {
return null;
}
if (requestURL.protocol === 'http:') {
return new HttpProxyAgent(proxyURL);
}
return new HttpsProxyAgent({
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
rejectUnauthorized: strictSSL
});
}
function getSystemProxyURI(requestURL: Url): string {
if (requestURL.protocol === 'http:') {
return process.env.HTTP_PROXY || process.env.http_proxy || null;
} else if (requestURL.protocol === 'https:') {
return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy || null;
}
return null;
}
function getSystemProxyAgent(rawRequestURL: string): any {
let requestURL = parseUrl(rawRequestURL);
let proxyURL = getSystemProxyURI(requestURL);
if (!proxyURL) {
return null;
}
return getAgent(rawRequestURL, proxyURL);
}
export interface IOptions {
proxyUrl?: string;
strictSSL?: boolean;
}
export function getProxyAgent(rawRequestURL: string, options: IOptions = {}): any {
if (!options.proxyUrl) {
return getSystemProxyAgent(rawRequestURL);
}
return getAgent(rawRequestURL, options.proxyUrl, options.strictSSL);
}