@@ -6,8 +6,12 @@ var timeoutReq = require('timed-out');
66
77var http = require ( 'http' ) ;
88var 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+ } ;
1115var httpAgent = new http . Agent ( agentOptions ) ;
1216var httpsAgent = new https . Agent ( agentOptions ) ;
1317
@@ -21,7 +25,8 @@ function HTTPTransport(options) {
2125 this . agent = httpAgent ;
2226}
2327util . inherits ( HTTPTransport , Transport ) ;
24- HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
28+ HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
29+
2530 var options = {
2631 hostname : client . dsn . host ,
2732 path : client . dsn . path + 'api/' + client . dsn . project_id + '/store/' ,
@@ -31,22 +36,39 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
3136 ca : client . ca ,
3237 agent : this . agent
3338 } ;
39+
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/' ;
44+ 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+ }
51+ }
52+
3453 for ( var key in this . options ) {
3554 if ( this . options . hasOwnProperty ( key ) ) {
3655 options [ key ] = this . options [ key ] ;
3756 }
3857 }
3958
4059 // prevent off heap memory explosion
41- var _name = this . agent . getName ( { host : client . dsn . host , port : client . dsn . port } ) ;
60+ var _name = this . agent . getName ( {
61+ host : client . dsn . host ,
62+ port : client . dsn . port
63+ } ) ;
4264 var _requests = this . agent . requests [ _name ] ;
4365 if ( _requests && Object . keys ( _requests ) . length > client . maxReqQueueCount ) {
4466 // other feedback strategy
4567 client . emit ( 'error' , new Error ( 'client req queue is full..' ) ) ;
4668 return ;
4769 }
4870
49- var req = this . transport . request ( options , function ( res ) {
71+ var req = this . transport . request ( options , function ( res ) {
5072 res . setEncoding ( 'utf8' ) ;
5173 if ( res . statusCode >= 200 && res . statusCode < 300 ) {
5274 client . emit ( 'logged' , eventId ) ;
@@ -63,17 +85,16 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
6385 client . emit ( 'error' , e ) ;
6486 cb && cb ( e ) ;
6587 }
66-
6788 // force the socket to drain
68- var noop = function ( ) { } ;
89+ var noop = function ( ) { } ;
6990 res . on ( 'data' , noop ) ;
7091 res . on ( 'end' , noop ) ;
7192 } ) ;
7293
7394 timeoutReq ( req , client . sendTimeout * 1000 ) ;
7495
7596 var cbFired = false ;
76- req . on ( 'error' , function ( e ) {
97+ req . on ( 'error' , function ( e ) {
7798 client . emit ( 'error' , e ) ;
7899 if ( ! cbFired ) {
79100 cb && cb ( e ) ;
@@ -89,10 +110,40 @@ function HTTPSTransport(options) {
89110 this . options = options || { } ;
90111 this . agent = httpsAgent ;
91112}
113+
114+ function HTTPProxyTransport ( options ) {
115+ this . defaultPort = 80 ;
116+ this . transport = http ;
117+ this . options = options || { } ;
118+ this . agent = httpAgent ;
119+ this . options . host = options . proxyHost ;
120+ this . options . port = options . proxyPort ;
121+ }
122+
123+ function HTTPSProxyTransport ( options ) {
124+ this . transport = https ;
125+ this . options = options || { } ;
126+ this . agent = httpsAgent ;
127+ this . options . agent = tunnel [ 'httpsOverHttp' ] ( {
128+ proxy : {
129+ host : options . proxyHost ,
130+ port : options . proxyPort ,
131+ proxyAuth : null // TODO: Add ability to specify creds/auth
132+ } ,
133+ keepAlive : agentOptions . keepAlive ,
134+ maxSockets : agentOptions . maxSockets
135+ } ) ;
136+ }
137+
92138util . inherits ( HTTPSTransport , HTTPTransport ) ;
139+ util . inherits ( HTTPProxyTransport , HTTPTransport ) ;
140+ util . inherits ( HTTPSProxyTransport , HTTPTransport ) ;
93141
94142module . exports . http = new HTTPTransport ( ) ;
95143module . exports . https = new HTTPSTransport ( ) ;
96144module . exports . Transport = Transport ;
97145module . exports . HTTPTransport = HTTPTransport ;
98146module . exports . HTTPSTransport = HTTPSTransport ;
147+
148+ module . exports . HTTPProxyTransport = HTTPProxyTransport ;
149+ module . exports . HTTPSProxyTransport = HTTPSProxyTransport ;
0 commit comments