Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit bf9dbab

Browse files
committed
Use tunnel-agent for proxying https request
1 parent 3b1c6e7 commit bf9dbab

File tree

2 files changed

+36
-54
lines changed

2 files changed

+36
-54
lines changed

lib/transports.js

Lines changed: 35 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ var timeoutReq = require('timed-out');
66

77
var http = require('http');
88
var https = require('https');
9-
var Tls = require('tls');
9+
var tunnel = require('tunnel-agent');
1010

11-
var agentOptions = {keepAlive: true, maxSockets: 100};
11+
var agentOptions = {
12+
keepAlive: true,
13+
maxSockets: 100
14+
};
1215
var httpAgent = new http.Agent(agentOptions);
1316
var httpsAgent = new https.Agent(agentOptions);
1417

@@ -22,7 +25,7 @@ function HTTPTransport(options) {
2225
this.agent = httpAgent;
2326
}
2427
util.inherits(HTTPTransport, Transport);
25-
HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
28+
HTTPTransport.prototype.send = function (client, message, headers, eventId, cb) {
2629

2730
var options = {
2831
hostname: client.dsn.host,
@@ -34,14 +37,17 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
3437
agent: this.agent
3538
};
3639

37-
// set path apprpriately when using proxy
38-
if (this.options.hasOwnProperty('host')) {
39-
if (client.dsn.protocol === 'http') {
40-
options.path = 'http://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/'
41-
} else {
42-
options.path = client.dsn.host + ':' + client.dsn.port;
43-
}
40+
// set path apprpriately when using http endpoint + proxy, set proxy headers appropriately when using https endpoint + proxy
41+
if (this.options.hasOwnProperty('proxyHost')) {
42+
if (client.dsn.protocol === 'http') {
43+
options.path = 'http://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/';
4444
delete options.hostname; // only 'host' should be set when using proxy
45+
} else {
46+
this.options.agent.proxyOptions.headers = {
47+
'Content-Type': 'application/octet-stream',
48+
host: client.dsn.host + ':' + client.dsn.port
49+
}
50+
}
4551
}
4652

4753
for (var key in this.options) {
@@ -50,16 +56,7 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
5056
}
5157
}
5258

53-
// prevent off heap memory explosion
54-
var _name = this.agent.getName({host: client.dsn.host, port: client.dsn.port});
55-
var _requests = this.agent.requests[_name];
56-
if (_requests && Object.keys(_requests).length > client.maxReqQueueCount) {
57-
// other feedback strategy
58-
client.emit('error', new Error('client req queue is full..'));
59-
return;
60-
}
61-
62-
var req = this.transport.request(options, function(res) {
59+
var req = this.transport.request(options, function (res) {
6360
res.setEncoding('utf8');
6461
if (res.statusCode >= 200 && res.statusCode < 300) {
6562
client.emit('logged', eventId);
@@ -76,68 +73,52 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
7673
client.emit('error', e);
7774
cb && cb(e);
7875
}
79-
8076
// force the socket to drain
81-
var noop = function() {};
77+
var noop = function () {};
8278
res.on('data', noop);
8379
res.on('end', noop);
8480
});
8581

8682
timeoutReq(req, client.sendTimeout * 1000);
8783

8884
var cbFired = false;
89-
req.on('error', function(e) {
85+
req.on('error', function (e) {
9086
client.emit('error', e);
9187
if (!cbFired) {
9288
cb && cb(e);
9389
cbFired = true;
9490
}
9591
});
9692

97-
// TLS connection for proxying to HTTPS endpoint
98-
/* req.on('connect', function (res, socket, head) {
99-
var cts = Tls.connect({
100-
host: client.dsn.host,
101-
socket: socket
102-
}, function () {
103-
cts.write('GET /welcome HTTP/1.1rnHost: sentry.iornrn');
104-
});
105-
106-
cts.on('data', function (data) {
107-
// console.log(data.toString());
108-
});
109-
});
110-
req.end(); */
111-
11293
req.end(message);
11394
};
11495

11596
function HTTPSTransport(options) {
11697
this.defaultPort = 443;
11798
this.transport = https;
11899
this.options = options || {};
119-
this.agent = httpsAgent;
120100
}
121101

122102
function HTTPProxyTransport(options) {
123-
// this.defaultPort = 80;
124-
this.transport = http;
125-
this.options = options || {};
126-
127-
// set host and port for proxy
128-
this.options.host = options.proxyHost;
129-
this.options.port = options.proxyPort;
103+
// this.defaultPort = 80;
104+
this.transport = http;
105+
this.options = options || {};
106+
this.options.host = options.proxyHost;
107+
this.options.port = options.proxyPort;
130108
}
131109

132110
function HTTPSProxyTransport(options) {
133-
// Not working yet ):
134-
this.defaultPort = 443;
135-
this.transport = http;
136-
this.options = options || {};
137-
138-
this.options.host = options.proxyHost;
139-
this.options.port = options.proxyPort;
140-
this.options.method = 'CONNECT';
111+
this.transport = https;
112+
this.options = options || {};
113+
this.options.agent = tunnel['httpsOverHttp']({
114+
proxy: {
115+
host: options.proxyHost,
116+
port: options.proxyPort,
117+
proxyAuth: null // TODO: Add ability to specify creds/auth
118+
},
119+
keepAlive: agentOptions.keepAlive,
120+
maxSockets: agentOptions.maxSockets
121+
});
141122
}
142123

143124
util.inherits(HTTPSTransport, HTTPTransport);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"lsmod": "1.0.0",
3535
"stack-trace": "0.0.9",
3636
"timed-out": "4.0.1",
37+
"tunnel-agent": "^0.6.0",
3738
"uuid": "3.0.0"
3839
},
3940
"devDependencies": {

0 commit comments

Comments
 (0)