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

Commit ed0172a

Browse files
committed
Add support for sending event requests via proxy
1 parent 6dc8bc1 commit ed0172a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lib/transports.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var timeoutReq = require('timed-out');
66

77
var http = require('http');
88
var https = require('https');
9+
var Tls = require('tls');
910

1011
function Transport() {}
1112
util.inherits(Transport, events.EventEmitter);
@@ -17,6 +18,7 @@ function HTTPTransport(options) {
1718
}
1819
util.inherits(HTTPTransport, Transport);
1920
HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
21+
2022
var options = {
2123
hostname: client.dsn.host,
2224
path: client.dsn.path + 'api/' + client.dsn.project_id + '/store/',
@@ -25,6 +27,17 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
2527
port: client.dsn.port || this.defaultPort,
2628
ca: client.ca
2729
};
30+
31+
// set path apprpriately when using proxy
32+
if (this.options.hasOwnProperty('host')) {
33+
if (client.dsn.protocol === 'http') {
34+
options.path = 'http://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/'
35+
} else {
36+
options.path = client.dsn.host + ':' + client.dsn.port;
37+
}
38+
delete options.hostname; // only 'host' should be set when using proxy
39+
}
40+
2841
for (var key in this.options) {
2942
if (this.options.hasOwnProperty(key)) {
3043
options[key] = this.options[key];
@@ -64,6 +77,22 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
6477
cbFired = true;
6578
}
6679
});
80+
81+
// TLS connection for proxying to HTTPS endpoint
82+
/* req.on('connect', function (res, socket, head) {
83+
var cts = Tls.connect({
84+
host: client.dsn.host,
85+
socket: socket
86+
}, function () {
87+
cts.write('GET /welcome HTTP/1.1rnHost: sentry.iornrn');
88+
});
89+
90+
cts.on('data', function (data) {
91+
// console.log(data.toString());
92+
});
93+
});
94+
req.end(); */
95+
6796
req.end(message);
6897
};
6998

@@ -72,10 +101,37 @@ function HTTPSTransport(options) {
72101
this.transport = https;
73102
this.options = options || {};
74103
}
104+
105+
function HTTPProxyTransport(options) {
106+
// this.defaultPort = 80;
107+
this.transport = http;
108+
this.options = options || {};
109+
110+
// set host and port for proxy
111+
this.options.host = options.proxyHost;
112+
this.options.port = options.proxyPort;
113+
}
114+
115+
function HTTPSProxyTransport(options) {
116+
// Not working yet ):
117+
this.defaultPort = 443;
118+
this.transport = http;
119+
this.options = options || {};
120+
121+
this.options.host = options.proxyHost;
122+
this.options.port = options.proxyPort;
123+
this.options.method = 'CONNECT';
124+
}
125+
75126
util.inherits(HTTPSTransport, HTTPTransport);
127+
util.inherits(HTTPProxyTransport, HTTPTransport);
128+
util.inherits(HTTPSProxyTransport, HTTPTransport);
76129

77130
module.exports.http = new HTTPTransport();
78131
module.exports.https = new HTTPSTransport();
79132
module.exports.Transport = Transport;
80133
module.exports.HTTPTransport = HTTPTransport;
81134
module.exports.HTTPSTransport = HTTPSTransport;
135+
136+
module.exports.HTTPProxyTransport = HTTPProxyTransport;
137+
module.exports.HTTPSProxyTransport = HTTPSProxyTransport;

0 commit comments

Comments
 (0)