Skip to content

Commit a03b3ef

Browse files
gregjhoganbryanmacfarlane
authored andcommitted
Add HTTPS request proxy support (#42)
* adding HTTPS request proxy support * fix tunnel module resolution
1 parent 083e3e1 commit a03b3ef

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

api/HttpClient.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
/// <reference path="../node/tunnel.d.ts"/>
5+
46
import url = require("url");
57

68
import http = require("http");
79
import https = require("https");
10+
import tunnel = require("tunnel");
811
import ifm = require('./interfaces/common/VsoBaseInterfaces');
912

1013
http.globalAgent.maxSockets = 100;
@@ -95,43 +98,46 @@ export class HttpClient implements ifm.IHttpClient {
9598
this.isSsl = usingSsl;
9699

97100
var proxyUrl: url.Url;
98-
if (process.env.HTTP_PROXY) {
101+
if (process.env.HTTPS_PROXY && usingSsl) {
102+
proxyUrl = url.parse(process.env.HTTPS_PROXY);
103+
} else if (process.env.HTTP_PROXY) {
99104
proxyUrl = url.parse(process.env.HTTP_PROXY);
100-
prot = proxyUrl.protocol === 'https:' ? https: http;
101105
}
102106

103-
var options: any = { headers: {}};
107+
var options: any = {
108+
host: parsedUrl.hostname,
109+
port: parsedUrl.port || defaultPort,
110+
path: (parsedUrl.pathname || '') + (parsedUrl.search || ''),
111+
method: method,
112+
headers: headers || {}
113+
};
114+
115+
//options.headers["Accept"] = contentType;
116+
options.headers["User-Agent"] = this.userAgent;
104117

105118
var useProxy = proxyUrl && proxyUrl.hostname;
106119
if (useProxy) {
107-
// TODO: support proxy-authorization
108-
options = {
109-
host: proxyUrl.hostname,
110-
port: proxyUrl.port || 8888,
111-
path: requestUrl,
112-
method: method,
113-
headers: {}
114-
}
115-
}
116-
else {
117-
options = {
118-
host: parsedUrl.hostname,
119-
port: parsedUrl.port || defaultPort,
120-
path: (parsedUrl.pathname || '') + (parsedUrl.search || ''),
121-
method: method,
122-
headers: {}
120+
var agentOptions: tunnel.TunnelOptions = {
121+
maxSockets: http.globalAgent.maxSockets,
122+
proxy: {
123+
// TODO: support proxy-authorization
124+
//proxyAuth: "user:password",
125+
host: proxyUrl.hostname,
126+
port: proxyUrl.port
127+
}
128+
};
129+
130+
var tunnelAgent: Function;
131+
var overHttps = proxyUrl.protocol === 'https:';
132+
if (usingSsl) {
133+
tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp;
134+
} else {
135+
tunnelAgent = overHttps ? tunnel.httpOverHttps : tunnel.httpOverHttp;
123136
}
137+
138+
options.agent = tunnelAgent(agentOptions);
124139
}
125140

126-
options.headers = headers;
127-
128-
if (useProxy) {
129-
options.headers['Host'] = parsedUrl.hostname;
130-
}
131-
132-
//options.headers["Accept"] = contentType;
133-
options.headers["User-Agent"] = this.userAgent;
134-
135141
if (this.handlers) {
136142
this.handlers.forEach((handler) => {
137143
handler.prepareRequest(options);

node/tunnel.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
declare module "tunnel" {
2+
export interface TunnelAgent {
3+
}
4+
5+
export interface TunnelOptions {
6+
maxSockets: number;
7+
proxy: ProxyOptions;
8+
}
9+
10+
export interface ProxyOptions {
11+
host: string;
12+
port: string;
13+
localAddress?: string;
14+
proxyAuth?: string;
15+
headers?: any;
16+
}
17+
18+
export function httpOverHttp(options: TunnelOptions): TunnelAgent;
19+
export function httpOverHttps(options: TunnelOptions): TunnelAgent;
20+
export function httpsOverHttp(options: TunnelOptions): TunnelAgent;
21+
export function httpsOverHttps(options: TunnelOptions): TunnelAgent;
22+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"license": "MIT",
2323
"dependencies": {
2424
"q": "^1.0.1",
25-
"underscore": "^1.8.3"
25+
"underscore": "^1.8.3",
26+
"tunnel": "0.0.4"
2627
},
2728
"devDependencies": {
2829
"shelljs": "^0.7.0"

0 commit comments

Comments
 (0)