Skip to content
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
40 changes: 28 additions & 12 deletions api/WebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import lim = require("./interfaces/LocationsInterfaces");
import crypto = require('crypto');
import fs = require('fs');
import os = require('os');
import url = require('url');

/**
* Methods to return handler objects (see handlers folder)
Expand Down Expand Up @@ -97,17 +98,19 @@ export class WebApi {
this.authHandler = authHandler;
this.options = options || {};

// try get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
if (!this.options.proxy || !this.options.proxy.proxyUrl) {
if (global['_vsts_task_lib_proxy']) {
let proxyFromEnv: VsoBaseInterfaces.IProxyConfiguration = {
proxyUrl: global['_vsts_task_lib_proxy_url'],
proxyUsername: global['_vsts_task_lib_proxy_username'],
proxyPassword: this._readTaskLibSecrets(global['_vsts_task_lib_proxy_password']),
proxyBypassHosts: JSON.parse(global['_vsts_task_lib_proxy_bypass'] || "[]"),
};

this.options.proxy = proxyFromEnv;
if (!this.isNoProxyHost(this.serverUrl)) {
// try to get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
if (!this.options.proxy || !this.options.proxy.proxyUrl) {
if (global['_vsts_task_lib_proxy']) {
let proxyFromEnv: VsoBaseInterfaces.IProxyConfiguration = {
proxyUrl: global['_vsts_task_lib_proxy_url'],
proxyUsername: global['_vsts_task_lib_proxy_username'],
proxyPassword: this._readTaskLibSecrets(global['_vsts_task_lib_proxy_password']),
proxyBypassHosts: JSON.parse(global['_vsts_task_lib_proxy_bypass'] || "[]"),
};

this.options.proxy = proxyFromEnv;
}
}
}

Expand Down Expand Up @@ -136,7 +139,7 @@ export class WebApi {
const osName: string = os.platform();
const osVersion: string = os.release();

if(requestSettings) {
if (requestSettings) {
userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
}
else {
Expand Down Expand Up @@ -331,6 +334,19 @@ export class WebApi {
return new workitemtrackingprocessdefinitionm.WorkItemTrackingProcessDefinitionsApi(serverUrl, handlers, this.options);
}

/**
* Determines if the domain is exluded for proxy via the no_proxy env var
* @param url: the server url
*/
public isNoProxyHost = function(_url: string) {
const noProxyDomains = (process.env.no_proxy || '')
.split(',')
.map(v => v.toLowerCase());
const serverUrl = url.parse(_url).host.toLowerCase();
// return true if the no_proxy includes the host
return noProxyDomains.indexOf(serverUrl) !== -1;
}

private async _getResourceAreaUrl(serverUrl: string, resourceId: string): Promise<string> {
if (!resourceId) {
return serverUrl;
Expand Down
10 changes: 9 additions & 1 deletion test/units/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,12 @@ describe('WebApi Units', function () {
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate');
});
});

it('supports no_proxy environment variable', async() => {
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'), null);
process.env.no_proxy='dev.azure.com,my-tfs-instance.host'
assert.equal(myWebApi.isNoProxyHost('https://dev.azure.com/myproject'), true);
assert.equal(myWebApi.isNoProxyHost('https://my-tfs-instance.host/myproject'), true);
assert.equal(myWebApi.isNoProxyHost('https://my-other-tfs-instance.host/myproject'), false);
});
});