adamvr/MQTT.js — pull request #122 by pyhrus (2013-08-07 09:16:36+00:00)
Title: Added optional Proxy protocol parser

Added optional Proxy protocol parser, for exposing remote client's IP if mqtt server is sitting
behind a proxy server. This protocol is currently supported by AWS ELB and HAproxy

Protocol documentation : http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt

Sample HAproxy config :

```
global
log 127.0.0.1 local1 info
maxconn 4096
user haproxy
group haproxy
ca-base ../ssl
crt-base ../ssl

defaults
log global
option tcplog
maxconn 4096
timeout connect 5s
timeout client 30s
timeout server 30s
timeout tunnel 1d

frontend public
bind :8443 ssl crt eaddrinuse_me.pem
mode tcp
option tcplog
log global
default_backend mqtt

backend mqtt
server node1 127.0.0.1:1883 send-proxy
```

Sample MQTT.js server:

```javascript
var mqtt = require('mqtt');

mqtt.createServer(function(client) {
var self = this;

if (!self.clients) self.clients = {};

client.on('connect', function(packet) {
self.clients[packet.clientId] = client;
client.id = packet.clientId;
console.log('CONNECT : client id : {' + client.id + '}');
console.log('CONNECT : client IP : {' + client.remoteAddress + '}');
client.connack({returnCode: 0});
});

client.on('pingreq', function(packet) {
console.log('PINGREQ(%s): %s', client.id, new Date());
client.pingresp();
});

client.on('disconnect', function(packet) {
console.log("DISCONNECT: client id: " + client.id);
client.stream.end();
});

client.on('close', function(packet) {
console.log("CLOSE: client id: " + client.id);
delete self.clients[client.id];
});

client.on('error', function(e) {
client.stream.end();
console.log(e);
});
}).setConfig({
parseProxyProtocol : true
}).listen(1883);
```