Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createServer implementation #2

Merged
merged 19 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it"
},
"pre-commit": [
"lint"
"test"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -56,21 +56,21 @@
"@types/node": "^12.12.25",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"aedes": "git+https://git@github.com/moscajs/aedes.git#master",
"aedes": "^0.43.0",
"faucet": "0.0.1",
"license-checker": "^25.0.1",
"mqtt": "^3.0.0",
"mqtt-packet": "^6.3.0",
"nyc": "^15.0.0",
"pre-commit": "^1.2.2",
"release-it": "^12.4.3",
"release-it": "^14.1.0",
"snazzy": "^8.0.0",
"standard": "^14.3.1",
"tape": "^4.13.0",
"typescript": "^3.7.5"
},
"dependencies": {
"aedes-protocol-decoder": "git+https://git@github.com/moscajs/aedes-protocol-decoder.git#master",
"aedes-protocol-decoder": "^1.0.0",
"ws": "^7.3.1"
}
}
181 changes: 181 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
'use strict'

var test = require('tape').test
var aedes = require('aedes')
var mqtt = require('mqtt')
var mqttPacket = require('mqtt-packet')
var net = require('net')
var proxyProtocol = require('proxy-protocol-js')
var createServer = require('./lib/server-factory')

// This test suite will be really effective after updating aedes and protocol-decoder module,
// to retrieve conn details in the client @preConnect

getlarge marked this conversation as resolved.
Show resolved Hide resolved
test('tcp clients have access to the connection details from the socket', function (t) {
t.plan(2)

var port = 4883
var broker = aedes({
preConnect: function (client, packet, done) {
if (packet) {
t.equal(packet.cmd, 'connect')
} else {
t.fail('no ip address present')
}
done(null, true)
setImmediate(finish)
}
})

var server = createServer({ trustProxy: true }, broker.handle)

server.listen(port, function (err) {
t.error(err, 'no error')
})

var client = mqtt.connect({
port,
keepalive: 0,
clientId: 'mqtt-client',
clean: false
})

function finish () {
client.end()
broker.close()
server.close()
t.end()
}
})

test('tcp proxied clients have access to the connection details from the proxy header', function (t) {
t.plan(2)

var port = 4883
var clientIp = '192.168.0.140'
var connectPacket = {
cmd: 'connect',
protocolId: 'MQIsdp',
protocolVersion: 3,
clean: true,
clientId: 'my-client-proxyV1',
keepalive: 0
}

var buf = mqttPacket.generate(connectPacket)
var src = new proxyProtocol.Peer(clientIp, 12345)
var dst = new proxyProtocol.Peer('127.0.0.1', port)
var protocol = new proxyProtocol.V1BinaryProxyProtocol(
proxyProtocol.INETProtocol.TCP4,
src,
dst,
buf
).build()

var broker = aedes({
preConnect: function (client, packet, done) {
if (packet) {
t.equal(packet.cmd, 'connect')
} else {
t.fail('no ip address present')
}
done(null, true)
setImmediate(finish)
}
})

var server = createServer({ trustProxy: true }, broker.handle)

server.listen(port, function (err) {
t.error(err, 'no error')
})

var client = net.connect(
{
port,
timeout: 0
},
function () {
client.write(protocol)
}
)

function finish () {
client.end()
broker.close()
server.close()
t.end()
}
})

test('websocket clients have access to the connection details from the socket', function (t) {
t.plan(2)

// var clientIp = '::ffff:127.0.0.1'
var port = 4883
var broker = aedes({
preConnect: function (client, packet, done) {
if (packet) {
t.equal(packet.cmd, 'connect')
} else {
t.fail('no ip address present')
}
done(null, true)
setImmediate(finish)
}
})

var server = createServer({ trustProxy: true, ws: true }, broker.handle)

server.listen(port, function (err) {
t.error(err, 'no error')
})

var client = mqtt.connect(`ws://localhost:${port}`)

function finish () {
broker.close()
server.close()
client.end()
t.end()
}
})

test('websocket proxied clients have access to the connection details', function (t) {
t.plan(2)

var clientIp = '192.168.0.140'
var port = 4883
var broker = aedes({
preConnect: function (client, packet, done) {
if (packet) {
t.equal(packet.cmd, 'connect')
} else {
t.fail('no ip address present')
}
done(null, true)
setImmediate(finish)
}
})

var server = createServer({ trustProxy: true, ws: true }, broker.handle)

server.listen(port, function (err) {
t.error(err, 'no error')
})

var client = mqtt.connect(`ws://localhost:${port}`, {
wsOptions: {
headers: {
'X-Real-Ip': clientIp
}
}
})

function finish () {
broker.close()
server.close()
client.end()
t.end()
}
})