2019-06-24 19:01:20 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
|
|
|
|
import * as http from "http";
|
|
|
|
import * as https from "https";
|
|
|
|
import * as tunnel from "tunnel";
|
|
|
|
|
|
|
|
import { ProxySettings } from "./serviceClient";
|
|
|
|
import { URLBuilder } from "./url";
|
2020-04-15 01:35:46 +03:00
|
|
|
import { HttpHeadersLike } from "./httpHeaders";
|
2019-06-24 19:01:20 +03:00
|
|
|
|
|
|
|
export type ProxyAgent = { isHttps: boolean; agent: http.Agent | https.Agent };
|
2021-02-02 04:54:34 +03:00
|
|
|
export function createProxyAgent(
|
|
|
|
requestUrl: string,
|
|
|
|
proxySettings: ProxySettings,
|
|
|
|
headers?: HttpHeadersLike
|
|
|
|
): ProxyAgent {
|
2019-06-24 19:01:20 +03:00
|
|
|
const tunnelOptions: tunnel.HttpsOverHttpsOptions = {
|
|
|
|
proxy: {
|
2019-07-06 04:07:51 +03:00
|
|
|
host: URLBuilder.parse(proxySettings.host).getHost() as string,
|
2019-06-24 19:01:20 +03:00
|
|
|
port: proxySettings.port,
|
2021-02-02 04:54:34 +03:00
|
|
|
headers: (headers && headers.rawHeaders()) || {},
|
|
|
|
},
|
2019-06-24 19:01:20 +03:00
|
|
|
};
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
if (proxySettings.username && proxySettings.password) {
|
2019-06-24 19:01:20 +03:00
|
|
|
tunnelOptions.proxy!.proxyAuth = `${proxySettings.username}:${proxySettings.password}`;
|
2021-06-15 03:30:17 +03:00
|
|
|
} else if (proxySettings.username) {
|
|
|
|
tunnelOptions.proxy!.proxyAuth = `${proxySettings.username}`;
|
2019-06-24 19:01:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const requestScheme = URLBuilder.parse(requestUrl).getScheme() || "";
|
|
|
|
const isRequestHttps = requestScheme.toLowerCase() === "https";
|
|
|
|
const proxyScheme = URLBuilder.parse(proxySettings.host).getScheme() || "";
|
|
|
|
const isProxyHttps = proxyScheme.toLowerCase() === "https";
|
|
|
|
|
|
|
|
const proxyAgent = {
|
|
|
|
isHttps: isRequestHttps,
|
2021-02-02 04:54:34 +03:00
|
|
|
agent: createTunnel(isRequestHttps, isProxyHttps, tunnelOptions),
|
2019-06-24 19:01:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return proxyAgent;
|
|
|
|
}
|
|
|
|
|
2021-02-11 22:12:16 +03:00
|
|
|
// Duplicate tunnel.HttpsOverHttpsOptions to avoid exporting createTunnel() with dependency on @types/tunnel
|
|
|
|
// createIunnel() is only imported by tests.
|
|
|
|
export interface HttpsProxyOptions {
|
|
|
|
host: string;
|
|
|
|
port: number;
|
|
|
|
localAddress?: string;
|
|
|
|
proxyAuth?: string;
|
|
|
|
headers?: { [key: string]: any };
|
|
|
|
ca?: Buffer[];
|
|
|
|
servername?: string;
|
|
|
|
key?: Buffer;
|
|
|
|
cert?: Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface HttpsOverHttpsOptions {
|
|
|
|
maxSockets?: number;
|
|
|
|
ca?: Buffer[];
|
|
|
|
key?: Buffer;
|
|
|
|
cert?: Buffer;
|
|
|
|
proxy?: HttpsProxyOptions;
|
|
|
|
}
|
|
|
|
|
2021-02-02 04:54:34 +03:00
|
|
|
export function createTunnel(
|
|
|
|
isRequestHttps: boolean,
|
|
|
|
isProxyHttps: boolean,
|
2021-02-11 22:12:16 +03:00
|
|
|
tunnelOptions: HttpsOverHttpsOptions
|
2021-02-02 04:54:34 +03:00
|
|
|
): http.Agent | https.Agent {
|
2019-06-24 19:01:20 +03:00
|
|
|
if (isRequestHttps && isProxyHttps) {
|
|
|
|
return tunnel.httpsOverHttps(tunnelOptions);
|
|
|
|
} else if (isRequestHttps && !isProxyHttps) {
|
|
|
|
return tunnel.httpsOverHttp(tunnelOptions);
|
|
|
|
} else if (!isRequestHttps && isProxyHttps) {
|
|
|
|
return tunnel.httpOverHttps(tunnelOptions);
|
|
|
|
} else {
|
|
|
|
return tunnel.httpOverHttp(tunnelOptions);
|
|
|
|
}
|
|
|
|
}
|