Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enable http Agent when calling Breeze #863

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/AzureVirtualMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class AzureVirtualMachine {
} else {
callback(vmInfo);
}
});
}, false, false);
if (req) {
req.on('error', (error: Error) => {
// Unable to contact endpoint.
Expand Down
2 changes: 1 addition & 1 deletion Library/CorrelationIdManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CorrelationIdManager {
clearTimeout(CorrelationIdManager._handle);
CorrelationIdManager._handle = undefined;
}
});
}, true, false);
if (req) {
req.on('error', (error: Error) => {
// Unable to contact endpoint.
Expand Down
87 changes: 45 additions & 42 deletions Library/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,17 @@ class Util {
* @param {string} requestUrl url endpoint
* @param {Object} requestOptions Request option
* @param {Function} requestCallback callback on request
* @param {boolean} useProxy Use proxy URL from config
* @param {boolean} useAgent Set Http Agent in request
* @returns {http.ClientRequest} request object
*/
public static makeRequest(
config: Config,
requestUrl: string,
requestOptions: http.RequestOptions | https.RequestOptions,
requestCallback: (res: http.IncomingMessage) => void): http.ClientRequest {
requestCallback: (res: http.IncomingMessage) => void,
useProxy= true,
useAgent= true): http.ClientRequest {

if (requestUrl && requestUrl.indexOf('//') === 0) {
requestUrl = 'https:' + requestUrl;
Expand All @@ -311,54 +315,53 @@ class Util {
};

var proxyUrl: string = undefined;

if (requestUrlParsed.protocol === 'https:') {
proxyUrl = config.proxyHttpsUrl || undefined;
}
if (requestUrlParsed.protocol === 'http:') {
proxyUrl = config.proxyHttpUrl || undefined;
}

if (proxyUrl) {
if (proxyUrl.indexOf('//') === 0) {
proxyUrl = 'http:' + proxyUrl;
if (useProxy) {
if (requestUrlParsed.protocol === 'https:') {
proxyUrl = config.proxyHttpsUrl || undefined;
}
try {
var proxyUrlParsed = new url.URL(proxyUrl);
// https is not supported at the moment
if (proxyUrlParsed.protocol === 'https:') {
Logging.info("Proxies that use HTTPS are not supported");
proxyUrl = undefined;
} else {
options = {
...options,
host: proxyUrlParsed.hostname,
port: proxyUrlParsed.port || "80",
path: requestUrl,
headers: {
...options.headers,
Host: requestUrlParsed.hostname,
},
};
}
if (requestUrlParsed.protocol === 'http:') {
proxyUrl = config.proxyHttpUrl || undefined;
}
catch (err) {
Logging.warn("Wrong proxy URL provided");
if (proxyUrl) {
if (proxyUrl.indexOf('//') === 0) {
proxyUrl = 'http:' + proxyUrl;
}
try {
var proxyUrlParsed = new url.URL(proxyUrl);
// https is not supported at the moment
if (proxyUrlParsed.protocol === 'https:') {
Logging.info("Proxies that use HTTPS are not supported");
proxyUrl = undefined;
} else {
options = {
...options,
host: proxyUrlParsed.hostname,
port: proxyUrlParsed.port || "80",
path: requestUrl,
headers: {
...options.headers,
Host: requestUrlParsed.hostname,
},
};
}
}
catch (err) {
Logging.warn("Wrong proxy URL provided");
}
}

}

var isHttps = requestUrlParsed.protocol === 'https:' && !proxyUrl;

if (isHttps && config.httpsAgent !== undefined) {
options.agent = config.httpsAgent;
} else if (!isHttps && config.httpAgent !== undefined) {
options.agent = config.httpAgent;
} else if (isHttps) {
// HTTPS without a passed in agent. Use one that enforces our TLS rules
options.agent = Util._useKeepAlive ? Util.keepAliveAgent : Util.tlsRestrictedAgent;
if (useAgent) {
if (isHttps && config.httpsAgent !== undefined) {
options.agent = config.httpsAgent;
} else if (!isHttps && config.httpAgent !== undefined) {
options.agent = config.httpAgent;
} else if (isHttps) {
// HTTPS without a passed in agent. Use one that enforces our TLS rules
options.agent = Util._useKeepAlive ? Util.keepAliveAgent : Util.tlsRestrictedAgent;
}
}

if (isHttps) {
return https.request(<any>options, requestCallback);
} else {
Expand Down