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

Commit 954ee7a

Browse files
committed
Add support for sending events through proxy
For both HTTP and HTTPS endpoints. Uses tunnel-agent for https.
1 parent ff785f0 commit 954ee7a

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

lib/transports.js

+57-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ var timeoutReq = require('timed-out');
66

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

10-
var agentOptions = {keepAlive: true, maxSockets: 100};
11+
var agentOptions = {
12+
keepAlive: true,
13+
maxSockets: 100
14+
};
1115
var httpAgent = new http.Agent(agentOptions);
1216
var httpsAgent = new https.Agent(agentOptions);
1317

@@ -21,7 +25,7 @@ function HTTPTransport(options) {
2125
this.agent = httpAgent;
2226
}
2327
util.inherits(HTTPTransport, Transport);
24-
HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
28+
HTTPTransport.prototype.send = function (client, message, headers, eventId, cb) {
2529
var options = {
2630
hostname: client.dsn.host,
2731
path: client.dsn.path + 'api/' + client.dsn.project_id + '/store/',
@@ -31,22 +35,39 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
3135
ca: client.ca,
3236
agent: this.agent
3337
};
38+
39+
// set path apprpriately when using http endpoint + proxy, set proxy headers appropriately when using https endpoint + proxy
40+
if (this.options.hasOwnProperty('proxyHost')) {
41+
if (client.dsn.protocol === 'http') {
42+
options.path = 'http://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/';
43+
delete options.hostname; // only 'host' should be set when using proxy
44+
} else {
45+
this.options.agent.proxyOptions.headers = {
46+
'Content-Type': 'application/octet-stream',
47+
host: client.dsn.host + ':' + client.dsn.port
48+
}
49+
}
50+
}
51+
3452
for (var key in this.options) {
3553
if (this.options.hasOwnProperty(key)) {
3654
options[key] = this.options[key];
3755
}
3856
}
3957

4058
// prevent off heap memory explosion
41-
var _name = this.agent.getName({host: client.dsn.host, port: client.dsn.port});
59+
var _name = this.agent.getName({
60+
host: client.dsn.host,
61+
port: client.dsn.port
62+
});
4263
var _requests = this.agent.requests[_name];
4364
if (_requests && Object.keys(_requests).length > client.maxReqQueueCount) {
4465
// other feedback strategy
4566
client.emit('error', new Error('client req queue is full..'));
4667
return;
4768
}
4869

49-
var req = this.transport.request(options, function(res) {
70+
var req = this.transport.request(options, function (res) {
5071
res.setEncoding('utf8');
5172
if (res.statusCode >= 200 && res.statusCode < 300) {
5273
client.emit('logged', eventId);
@@ -63,17 +84,16 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
6384
client.emit('error', e);
6485
cb && cb(e);
6586
}
66-
6787
// force the socket to drain
68-
var noop = function() {};
88+
var noop = function () {};
6989
res.on('data', noop);
7090
res.on('end', noop);
7191
});
7292

7393
timeoutReq(req, client.sendTimeout * 1000);
7494

7595
var cbFired = false;
76-
req.on('error', function(e) {
96+
req.on('error', function (e) {
7797
client.emit('error', e);
7898
if (!cbFired) {
7999
cb && cb(e);
@@ -89,10 +109,40 @@ function HTTPSTransport(options) {
89109
this.options = options || {};
90110
this.agent = httpsAgent;
91111
}
112+
113+
function HTTPProxyTransport(options) {
114+
this.defaultPort = 80;
115+
this.transport = http;
116+
this.options = options || {};
117+
this.agent = httpAgent;
118+
this.options.host = options.proxyHost;
119+
this.options.port = options.proxyPort;
120+
}
121+
122+
function HTTPSProxyTransport(options) {
123+
this.transport = https;
124+
this.options = options || {};
125+
this.agent = httpsAgent;
126+
this.options.agent = tunnel['httpsOverHttp']({
127+
proxy: {
128+
host: options.proxyHost,
129+
port: options.proxyPort,
130+
proxyAuth: null // TODO: Add ability to specify creds/auth
131+
},
132+
keepAlive: agentOptions.keepAlive,
133+
maxSockets: agentOptions.maxSockets
134+
});
135+
}
136+
92137
util.inherits(HTTPSTransport, HTTPTransport);
138+
util.inherits(HTTPProxyTransport, HTTPTransport);
139+
util.inherits(HTTPSProxyTransport, HTTPTransport);
93140

94141
module.exports.http = new HTTPTransport();
95142
module.exports.https = new HTTPSTransport();
96143
module.exports.Transport = Transport;
97144
module.exports.HTTPTransport = HTTPTransport;
98145
module.exports.HTTPSTransport = HTTPSTransport;
146+
147+
module.exports.HTTPProxyTransport = HTTPProxyTransport;
148+
module.exports.HTTPSProxyTransport = HTTPSProxyTransport;

package.json

+1
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)