Skip to content

Commit bb15a2d

Browse files
gforcegdamccorm
authored andcommitted
support $no_proxy (#192)
* no_proxy env support * unit test no_proxy env support
1 parent 3b4a5f7 commit bb15a2d

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

api/WebApi.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import lim = require("./interfaces/LocationsInterfaces");
3838
import crypto = require('crypto');
3939
import fs = require('fs');
4040
import os = require('os');
41+
import url = require('url');
4142

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

100-
// try get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
101-
if (!this.options.proxy || !this.options.proxy.proxyUrl) {
102-
if (global['_vsts_task_lib_proxy']) {
103-
let proxyFromEnv: VsoBaseInterfaces.IProxyConfiguration = {
104-
proxyUrl: global['_vsts_task_lib_proxy_url'],
105-
proxyUsername: global['_vsts_task_lib_proxy_username'],
106-
proxyPassword: this._readTaskLibSecrets(global['_vsts_task_lib_proxy_password']),
107-
proxyBypassHosts: JSON.parse(global['_vsts_task_lib_proxy_bypass'] || "[]"),
108-
};
109-
110-
this.options.proxy = proxyFromEnv;
101+
if (!this.isNoProxyHost(this.serverUrl)) {
102+
// try to get proxy setting from environment variable set by VSTS-Task-Lib if there is no proxy setting in the options
103+
if (!this.options.proxy || !this.options.proxy.proxyUrl) {
104+
if (global['_vsts_task_lib_proxy']) {
105+
let proxyFromEnv: VsoBaseInterfaces.IProxyConfiguration = {
106+
proxyUrl: global['_vsts_task_lib_proxy_url'],
107+
proxyUsername: global['_vsts_task_lib_proxy_username'],
108+
proxyPassword: this._readTaskLibSecrets(global['_vsts_task_lib_proxy_password']),
109+
proxyBypassHosts: JSON.parse(global['_vsts_task_lib_proxy_bypass'] || "[]"),
110+
};
111+
112+
this.options.proxy = proxyFromEnv;
113+
}
111114
}
112115
}
113116

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

139-
if(requestSettings) {
142+
if (requestSettings) {
140143
userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
141144
}
142145
else {
@@ -331,6 +334,19 @@ export class WebApi {
331334
return new workitemtrackingprocessdefinitionm.WorkItemTrackingProcessDefinitionsApi(serverUrl, handlers, this.options);
332335
}
333336

337+
/**
338+
* Determines if the domain is exluded for proxy via the no_proxy env var
339+
* @param url: the server url
340+
*/
341+
public isNoProxyHost = function(_url: string) {
342+
const noProxyDomains = (process.env.no_proxy || '')
343+
.split(',')
344+
.map(v => v.toLowerCase());
345+
const serverUrl = url.parse(_url).host.toLowerCase();
346+
// return true if the no_proxy includes the host
347+
return noProxyDomains.indexOf(serverUrl) !== -1;
348+
}
349+
334350
private async _getResourceAreaUrl(serverUrl: string, resourceId: string): Promise<string> {
335351
if (!resourceId) {
336352
return serverUrl;

test/units/tests.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,12 @@ describe('WebApi Units', function () {
269269
assert.equal(res.apiVersion, '1');
270270
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate');
271271
});
272-
});
272+
273+
it('supports no_proxy environment variable', async() => {
274+
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'), null);
275+
process.env.no_proxy='dev.azure.com,my-tfs-instance.host'
276+
assert.equal(myWebApi.isNoProxyHost('https://dev.azure.com/myproject'), true);
277+
assert.equal(myWebApi.isNoProxyHost('https://my-tfs-instance.host/myproject'), true);
278+
assert.equal(myWebApi.isNoProxyHost('https://my-other-tfs-instance.host/myproject'), false);
279+
});
280+
});

0 commit comments

Comments
 (0)