@@ -6,8 +6,12 @@ var timeoutReq = require('timed-out');
6
6
7
7
var http = require ( 'http' ) ;
8
8
var https = require ( 'https' ) ;
9
+ var tunnel = require ( 'tunnel-agent' ) ;
9
10
10
- var agentOptions = { keepAlive : true , maxSockets : 100 } ;
11
+ var agentOptions = {
12
+ keepAlive : true ,
13
+ maxSockets : 100
14
+ } ;
11
15
var httpAgent = new http . Agent ( agentOptions ) ;
12
16
var httpsAgent = new https . Agent ( agentOptions ) ;
13
17
@@ -21,7 +25,7 @@ function HTTPTransport(options) {
21
25
this . agent = httpAgent ;
22
26
}
23
27
util . inherits ( HTTPTransport , Transport ) ;
24
- HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
28
+ HTTPTransport . prototype . send = function ( client , message , headers , eventId , cb ) {
25
29
var options = {
26
30
hostname : client . dsn . host ,
27
31
path : client . dsn . path + 'api/' + client . dsn . project_id + '/store/' ,
@@ -31,22 +35,39 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
31
35
ca : client . ca ,
32
36
agent : this . agent
33
37
} ;
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
+
34
52
for ( var key in this . options ) {
35
53
if ( this . options . hasOwnProperty ( key ) ) {
36
54
options [ key ] = this . options [ key ] ;
37
55
}
38
56
}
39
57
40
58
// 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
+ } ) ;
42
63
var _requests = this . agent . requests [ _name ] ;
43
64
if ( _requests && Object . keys ( _requests ) . length > client . maxReqQueueCount ) {
44
65
// other feedback strategy
45
66
client . emit ( 'error' , new Error ( 'client req queue is full..' ) ) ;
46
67
return ;
47
68
}
48
69
49
- var req = this . transport . request ( options , function ( res ) {
70
+ var req = this . transport . request ( options , function ( res ) {
50
71
res . setEncoding ( 'utf8' ) ;
51
72
if ( res . statusCode >= 200 && res . statusCode < 300 ) {
52
73
client . emit ( 'logged' , eventId ) ;
@@ -63,17 +84,16 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) {
63
84
client . emit ( 'error' , e ) ;
64
85
cb && cb ( e ) ;
65
86
}
66
-
67
87
// force the socket to drain
68
- var noop = function ( ) { } ;
88
+ var noop = function ( ) { } ;
69
89
res . on ( 'data' , noop ) ;
70
90
res . on ( 'end' , noop ) ;
71
91
} ) ;
72
92
73
93
timeoutReq ( req , client . sendTimeout * 1000 ) ;
74
94
75
95
var cbFired = false ;
76
- req . on ( 'error' , function ( e ) {
96
+ req . on ( 'error' , function ( e ) {
77
97
client . emit ( 'error' , e ) ;
78
98
if ( ! cbFired ) {
79
99
cb && cb ( e ) ;
@@ -89,10 +109,40 @@ function HTTPSTransport(options) {
89
109
this . options = options || { } ;
90
110
this . agent = httpsAgent ;
91
111
}
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
+
92
137
util . inherits ( HTTPSTransport , HTTPTransport ) ;
138
+ util . inherits ( HTTPProxyTransport , HTTPTransport ) ;
139
+ util . inherits ( HTTPSProxyTransport , HTTPTransport ) ;
93
140
94
141
module . exports . http = new HTTPTransport ( ) ;
95
142
module . exports . https = new HTTPSTransport ( ) ;
96
143
module . exports . Transport = Transport ;
97
144
module . exports . HTTPTransport = HTTPTransport ;
98
145
module . exports . HTTPSTransport = HTTPSTransport ;
146
+
147
+ module . exports . HTTPProxyTransport = HTTPProxyTransport ;
148
+ module . exports . HTTPSProxyTransport = HTTPSProxyTransport ;
0 commit comments