Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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);
Expand All @@ -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.
**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.
23 changes: 20 additions & 3 deletions proxywrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,38 @@ 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() {
return parseInt(header[4], 10);
}
});

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));
Expand All @@ -160,4 +177,4 @@ exports.proxy = function(iface) {
}

return exports;
}
}