Files
azuredatastudio/src/vs/platform/request/node/proxy.ts
2023-03-13 11:05:59 -07:00

52 lines
1.7 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { parse as parseUrl, Url } from 'url';
import { isBoolean } from 'vs/base/common/types';
export type Agent = any;
function getSystemProxyURI(requestURL: Url, env: typeof process.env): string | null {
if (requestURL.protocol === 'http:') {
return env.HTTP_PROXY || env.http_proxy || null;
} else if (requestURL.protocol === 'https:') {
return env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy || null;
}
return null;
}
export interface IOptions {
proxyUrl?: string;
strictSSL?: boolean;
}
export async function getProxyAgent(rawRequestURL: string, env: typeof process.env, options: IOptions = {}): Promise<Agent> {
const requestURL = parseUrl(rawRequestURL);
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL, env);
if (!proxyURL) {
return null;
}
const proxyEndpoint = parseUrl(proxyURL);
if (!/^https?:$/.test(proxyEndpoint.protocol || '')) {
return null;
}
const opts = {
host: proxyEndpoint.hostname || '',
port: proxyEndpoint.port || (proxyEndpoint.protocol === 'https' ? '443' : '80'),
auth: proxyEndpoint.auth,
rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true,
};
// {{SQL CARBON EDIT}} Using updated 'http-proxy-agent' and 'https-proxy-agent' packages.
return requestURL.protocol === 'http:'
? (await import('http-proxy-agent'))(opts as any as Url)
: (await import('https-proxy-agent'))(opts);
}