Skip to content

Commit 621f9b4

Browse files
committed
Allowing the common proxy headers' value to be appended in proxy chain scenarios.
1 parent 1e33434 commit 621f9b4

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

lib/node-http-proxy/http-proxy.js

+49-9
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,36 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
122122

123123
//
124124
// 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:
126127
//
127128
// * `x-forwarded-for`: IP Address of the original request
128129
// * `x-forwarded-proto`: Protocol of the original request
129130
// * `x-forwarded-port`: Port of the original request.
130131
//
131-
132132
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+
136155
}
137156

138157
//
@@ -299,6 +318,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
299318
// `req` write it to the `reverseProxy` request.
300319
//
301320
req.on('data', function (chunk) {
321+
302322
if (!errState) {
303323
var flushed = reverseProxy.write(chunk);
304324
if (!flushed) {
@@ -375,16 +395,36 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
375395

376396
//
377397
// 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:
379400
//
380401
// * `x-forwarded-for`: IP Address of the original request
381402
// * `x-forwarded-proto`: Protocol of the original request
382403
// * `x-forwarded-port`: Port of the original request.
383404
//
384405
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+
388428
}
389429

390430
//

0 commit comments

Comments
 (0)