Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6098aeb
update dependencies
getlarge Oct 14, 2020
cd5b668
refactor protocol decoder and export extractSocketDetails
getlarge Oct 14, 2020
efeef3d
update example, tests and docs
getlarge Oct 14, 2020
8ab1350
update types
getlarge Oct 14, 2020
3a5a81e
change lib path and update test
getlarge Oct 14, 2020
a98ee24
quick fix
getlarge Oct 14, 2020
fa6a946
fix port getters for Proxy v2
getlarge Oct 14, 2020
7fc3cbc
Update createServer signature
getlarge Oct 14, 2020
e43176b
Fix test end
getlarge Oct 14, 2020
0ae0871
update doc
getlarge Oct 15, 2020
5306908
fix test issues with node 10
getlarge Oct 15, 2020
81f6521
chore: drop nodejs 8, added nodejs 14
robertsLando Oct 26, 2020
ace99b9
refactor tests
getlarge Oct 26, 2020
27a1d23
minor improvements
getlarge Oct 26, 2020
0780d46
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge Oct 26, 2020
07e7f88
chore(ci): disable parallel tests
robertsLando Oct 26, 2020
bea03af
chore(ci): removed parallel from coverall
robertsLando Oct 26, 2020
31d027e
Fix test not ending
getlarge Oct 26, 2020
835a412
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge Oct 26, 2020
c789d19
Reenable parallel tests
getlarge Oct 26, 2020
7310ac2
test fix
robertsLando Oct 26, 2020
ddfabb9
udpate aedes
getlarge Oct 26, 2020
18f9212
replace spread operator
getlarge Oct 27, 2020
52ca790
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge Oct 27, 2020
0982081
fix package.json
getlarge Oct 27, 2020
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

Protocol decoder for Aedes MQTT Broker

The purpose of this module is to be used inside [aedes-server-factory](https://github.com/moscajs/aedes-server-factory) `bindConnection` function, which is called when the server receives a connection from client ( before CONNECT packet). The client object state is in default and its connected state is false.
The function extract socket details and if aedes-server-factory `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and proxy protocol (v1 and v2) to retrieve information in client.connDetails.
The purpose of this module is to be used inside [aedes-server-factory](https://github.com/moscajs/aedes-server-factory) `bindConnection` function, which is called when the server receives a connection from client (before CONNECT packet). The client object state is in default and its connected state is false.
The function extract socket details and if `aedes-server-factory` `trustProxy` option is set to true, it will first parse http headers (x-real-ip | x-forwarded-for) and/or proxy protocol (v1 and v2), then passing the informations to `aedes` that will assign them to `client.connDetails`.

The function `protocolDecoder` and `extractSocketDetails` returns [ConnectionDetails](./types/index.d.ts), if the object contains data property, it will be parsed as an mqtt-packet.
The function `protocolDecoder` and `extractSocketDetails` returns [ConnectionDetails](./types/index.d.ts), if the object contains `data` property, it will be parsed as an [mqtt-packet](https://github.com/mqttjs/mqtt-packet).

## Install

Expand Down Expand Up @@ -46,8 +46,6 @@ var server = createServer(broker, { trustProxy: true, protocolDecoder })
server.listen(port, function () {
console.log('server listening on port', port)
})


```

## License
Expand Down
23 changes: 11 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ test('websocket clients have access to the ipAddress from the socket (if no ip h
var client = mqtt.connect(`ws://localhost:${port}`)

function finish () {
Comment thread
robertsLando marked this conversation as resolved.
Outdated
client.end(true)
broker.close()
server.close()
client.end()
t.end()
}
})
Expand Down Expand Up @@ -289,9 +289,9 @@ test('websocket proxied clients have access to the ipAddress from x-real-ip head
})

function finish () {
client.end(true)
broker.close()
server.close()
client.end()
t.end()
}
})
Expand Down Expand Up @@ -328,9 +328,9 @@ test('websocket proxied clients have access to the ipAddress from x-forwarded-fo
})

function finish () {
client.end(true)
broker.close()
server.close()
client.end()
t.end()
}
})
Expand All @@ -356,29 +356,28 @@ test('tcp proxied (protocol v1) clients buffer contains MQTT packet and proxy he

var broker = aedes({
preConnect: function (client, packet, done) {
function cb () {
done(null, true)
setImmediate(finish)
}

if (client.connDetails.data) {
const parser = mqttPacket.parser({ protocolVersion: 3 })
parser.on('packet', (parsedPacket) => {
t.equal(JSON.stringify(parsedPacket), JSON.stringify(packet))
cb()
done(null, true)
})
parser.on('error', () => {
t.fail('no valid MQTT packet extracted from TCP buffer')
cb()
done(null, true)
})
parser.parse(client.connDetails.data)
} else {
t.fail('no MQTT packet extracted from TCP buffer')
cb()
done(null, true)
}
}
})

broker.on('clientDisconnect', function () {
setImmediate(finish)
})

var server = createServer(broker, { trustProxy: true, extractSocketDetails, protocolDecoder })
server.listen(brokerPort, function (err) {
t.error(err, 'no error')
Expand Down Expand Up @@ -422,7 +421,7 @@ test('tcp proxied (protocol v1) clients buffer contains MQTT packet and proxy he

var client = net.connect({
port: proxyPort,
timeout: 200
timeout: 150
}, function () {
client.write(buf)
})
Expand Down