@@ -122,17 +122,36 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
122
122
123
123
//
124
124
// Add common proxy headers to the request so that they can
125
- // be availible to the proxy target server:
125
+ // be availible to the proxy target server. If the proxy is
126
+ // part of proxy chain it will append the address:
126
127
//
127
128
// * `x-forwarded-for`: IP Address of the original request
128
129
// * `x-forwarded-proto`: Protocol of the original request
129
130
// * `x-forwarded-port`: Port of the original request.
130
131
//
131
-
132
132
if ( this . enable . xforward && req . connection && req . socket ) {
133
- req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . socket . remoteAddress ;
134
- req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . socket . remotePort ;
135
- req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'https' : 'http' ;
133
+
134
+ if ( req . headers [ 'x-forwarded-for' ] ) {
135
+ var addressToAppend = "," + req . connection . remoteAddress || req . socket . remoteAddress ;
136
+ req . headers [ 'x-forwarded-for' ] += addressToAppend ;
137
+ } else {
138
+ req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . socket . remoteAddress ;
139
+ }
140
+
141
+ if ( req . headers [ 'x-forwarded-port' ] ) {
142
+ var portToAppend = "," + req . connection . remotePort || req . socket . remotePort ;
143
+ req . headers [ 'x-forwarded-port' ] += portToAppend ;
144
+ } else {
145
+ req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . socket . remotePort ;
146
+ }
147
+
148
+ if ( req . headers [ 'x-forwarded-proto' ] ) {
149
+ var protoToAppend = "," + req . connection . pair ? 'https' : 'http' ;
150
+ req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
151
+ } else {
152
+ req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'https' : 'http' ;
153
+ }
154
+
136
155
}
137
156
138
157
//
@@ -299,6 +318,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
299
318
// `req` write it to the `reverseProxy` request.
300
319
//
301
320
req . on ( 'data' , function ( chunk ) {
321
+
302
322
if ( ! errState ) {
303
323
var flushed = reverseProxy . write ( chunk ) ;
304
324
if ( ! flushed ) {
@@ -375,16 +395,36 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
375
395
376
396
//
377
397
// Add common proxy headers to the request so that they can
378
- // be availible to the proxy target server:
398
+ // be availible to the proxy target server. If the proxy is
399
+ // part of proxy chain it will append the address:
379
400
//
380
401
// * `x-forwarded-for`: IP Address of the original request
381
402
// * `x-forwarded-proto`: Protocol of the original request
382
403
// * `x-forwarded-port`: Port of the original request.
383
404
//
384
405
if ( this . enable . xforward && req . connection && req . connection . socket ) {
385
- req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . connection . socket . remoteAddress ;
386
- req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . connection . socket . remotePort ;
387
- req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'https' : 'http' ;
406
+
407
+ if ( req . headers [ 'x-forwarded-for' ] ) {
408
+ var addressToAppend = "," + req . connection . remoteAddress || req . connection . socket . remoteAddress ;
409
+ req . headers [ 'x-forwarded-for' ] += addressToAppend ;
410
+ } else {
411
+ req . headers [ 'x-forwarded-for' ] = req . connection . remoteAddress || req . connection . socket . remoteAddress ;
412
+ }
413
+
414
+ if ( req . headers [ 'x-forwarded-port' ] ) {
415
+ var portToAppend = "," + req . connection . remotePort || req . connection . socket . remotePort ;
416
+ req . headers [ 'x-forwarded-port' ] += portToAppend ;
417
+ } else {
418
+ req . headers [ 'x-forwarded-port' ] = req . connection . remotePort || req . connection . socket . remotePort ;
419
+ }
420
+
421
+ if ( req . headers [ 'x-forwarded-proto' ] ) {
422
+ var protoToAppend = "," + req . connection . pair ? 'https' : 'http' ;
423
+ req . headers [ 'x-forwarded-proto' ] += protoToAppend ;
424
+ } else {
425
+ req . headers [ 'x-forwarded-proto' ] = req . connection . pair ? 'https' : 'http' ;
426
+ }
427
+
388
428
}
389
429
390
430
//
0 commit comments