From 5ac74c2a87bdf5fc6af1c6f8fe827d57e962bd1c Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Mon, 19 Aug 2013 16:01:25 -0500 Subject: [PATCH] Also setting destination values On the changes, besides the mentioned above, I changed `remoteAddress` and `remotePort` to `sourceAddress` and `sourcePort` respectively, which seemed to be more related to the original spec. Then I added `destinationAddres` and `destinationPort`. --- README.md | 14 +++++++++++--- proxywrap.js | 23 ++++++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d557fb1..135794b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ node-proxywrap ============== -This module wraps node's various `Server` interfaces so that they are compatible with the [PROXY protocol](http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt). It automatically parses the PROXY headers and resets `socket.remoteAddress` and `socket.remotePort` so that they have the correct values. +This module wraps node's various `Server` interfaces so that they are compatible with the [PROXY protocol](http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt). It automatically parses the PROXY headers and resets the following socket properties so that they have the correct values: + + socket.sourceAddress + socket.sourcePort + socket.destinationAddress + socket.destinationPort + +Install it with: npm install proxywrap @@ -23,7 +30,8 @@ proxywrap is a drop-in replacement. Here's a simple Express app: , srv = proxiedHttp.createServer(app); // instead of http.createServer(app) app.get('/', function(req, res) { - res.send('IP = ' + req.connection.remoteAddress + ':' + req.connection.remotePort); + res.send('FROM IP = ' + req.connection.sourceAddress + ':' + req.connection.sourcePort); + res.send(' TO IP = ' + req.connection.destinationAddress + ':' + req.connection.destinationPort); }); srv.listen(80); @@ -36,4 +44,4 @@ You can do the same with `net` (raw TCP streams), `https`, and `spdy`. It will var proxiedSpdy = require('proxywrap').proxy(require('spdy').server); -**Warning:** *All* traffic to your proxied server MUST use the PROXY protocol. If the first five bytes received aren't `PROXY`, the connection will be dropped. Obviously, the node server accepting PROXY connections should not be exposed directly to the internet; only the proxy (whether ELB, HAProxy, or something else) should be able to connect to node. \ No newline at end of file +**Warning:** *All* traffic to your proxied server MUST use the PROXY protocol. If the first five bytes received aren't `PROXY`, the connection will be dropped. Obviously, the node server accepting PROXY connections should not be exposed directly to the internet; only the proxy (whether ELB, HAProxy, or something else) should be able to connect to node. diff --git a/proxywrap.js b/proxywrap.js index 0e22d8e..c953bd4 100644 --- a/proxywrap.js +++ b/proxywrap.js @@ -121,14 +121,23 @@ exports.proxy = function(iface) { var hlen = header.length; header = header.split(' '); - Object.defineProperty(socket, 'remoteAddress', { + Object.defineProperty(socket, 'sourceAddress', { enumerable: false, configurable: true, get: function() { return header[2]; } }); - Object.defineProperty(socket, 'remotePort', { + + Object.defineProperty(socket, 'destinationAddress', { + enumerable: false, + configurable: true, + get: function() { + return header[3]; + } + }); + + Object.defineProperty(socket, 'sourcePort', { enumerable: false, configurable: true, get: function() { @@ -136,6 +145,14 @@ exports.proxy = function(iface) { } }); + Object.defineProperty(socket, 'destinationPort', { + enumerable: false, + configurable: true, + get: function() { + return parseInt(header[5], 10); + } + }); + // unshifting will fire the readable event socket.emit = realEmit; socket.unshift(buf.slice(crlf+2)); @@ -160,4 +177,4 @@ exports.proxy = function(iface) { } return exports; -} \ No newline at end of file +}