From 916d44e3d2a17bb9d5178f347ddad9796b988e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ota=CC=81vio=20Ribeiro?= Date: Thu, 6 Dec 2012 02:07:11 -0200 Subject: [PATCH] Routing Proxy was not sending x-forward-*. Fixing It... --- lib/node-http-proxy/routing-proxy.js | 74 +++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/node-http-proxy/routing-proxy.js b/lib/node-http-proxy/routing-proxy.js index d00b722a7..fd0d1f9e0 100644 --- a/lib/node-http-proxy/routing-proxy.js +++ b/lib/node-http-proxy/routing-proxy.js @@ -163,6 +163,10 @@ RoutingProxy.prototype.close = function () { }); }; +function getProto(req) { + return req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http'); +} + // // ### function proxyRequest (req, res, [port, host, paused]) // #### @req {ServerRequest} Incoming HTTP Request to proxy. @@ -176,7 +180,41 @@ RoutingProxy.prototype.close = function () { // RoutingProxy.prototype.proxyRequest = function (req, res, options) { options = options || {}; - + + // + // Add common proxy headers to the request so that they can + // be availible to the proxy target server. If the proxy is + // part of proxy chain it will append the address: + // + // * `x-forwarded-for`: IP Address of the original request + // * `x-forwarded-proto`: Protocol of the original request + // * `x-forwarded-port`: Port of the original request. + // + if (this.enable.xforward && req.connection && req.socket) { + if (req.headers['x-forwarded-for']){ + var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress; + req.headers['x-forwarded-for'] += addressToAppend; + } + else { + req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress; + } + + if (req.headers['x-forwarded-port']){ + var portToAppend = "," + req.connection.remotePort || req.socket.remotePort; + req.headers['x-forwarded-port'] += portToAppend; + } + else { + req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort; + } + + if (req.headers['x-forwarded-proto']){ + var protoToAppend = "," + getProto(req); + req.headers['x-forwarded-proto'] += protoToAppend; + } + else { + req.headers['x-forwarded-proto'] = getProto(req); + } + } var location; // @@ -248,6 +286,40 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) { RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options) { options = options || {}; + // + // Add common proxy headers to the request so that they can + // be availible to the proxy target server. If the proxy is + // part of proxy chain it will append the address: + // + // * `x-forwarded-for`: IP Address of the original request + // * `x-forwarded-proto`: Protocol of the original request + // * `x-forwarded-port`: Port of the original request. + // + if (this.enable.xforward && req.connection && req.socket) { + if (req.headers['x-forwarded-for']){ + var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress; + req.headers['x-forwarded-for'] += addressToAppend; + } + else { + req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress; + } + + if (req.headers['x-forwarded-port']){ + var portToAppend = "," + req.connection.remotePort || req.socket.remotePort; + req.headers['x-forwarded-port'] += portToAppend; + } + else { + req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort; + } + + if (req.headers['x-forwarded-proto']){ + var protoToAppend = "," + getProto(req); + req.headers['x-forwarded-proto'] += protoToAppend; + } + else { + req.headers['x-forwarded-proto'] = getProto(req); + } + } var location, proxy, key;