Skip to content
Open
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
343 changes: 0 additions & 343 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const { test } = require('tap')
const http = require('http')
const ws = require('websocket-stream')
const mqtt = require('mqtt')
const mqttPacket = require('mqtt-packet')
const net = require('net')
const proxyProtocol = require('proxy-protocol-js')
const { protocolDecoder } = require('aedes-protocol-decoder')
const { setup, connect, delay } = require('./helper')
const aedes = require('../')

Expand Down Expand Up @@ -559,342 +555,3 @@ test('websocket clients have access to the request object', function (t) {
server.close()
})
})

// test ipAddress property presence when trustProxy is enabled
test('tcp clients have access to the ipAddress from the socket', function (t) {
t.plan(2)

const port = 4883
const broker = aedes({
preConnect: function (client, done) {
if (client && client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal('::ffff:127.0.0.1', client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = net.createServer(broker.handle)
server.listen(port, function (err) {
t.error(err, 'no error')
})

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

t.tearDown(() => {
client.end(true)
broker.close()
server.close()
})
})

test('tcp proxied (protocol v1) clients have access to the ipAddress(v4)', function (t) {
t.plan(2)

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

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

const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = net.createServer(broker.handle)
server.listen(port, function (err) {
t.error(err, 'no error')
})

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

t.tearDown(() => {
client.end()
broker.close()
server.close()
})
})

test('tcp proxied (protocol v2) clients have access to the ipAddress(v4)', function (t) {
t.plan(2)

const port = 4883
const clientIp = '192.168.0.140'
const packet = {
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client-proxyV2'
}

const protocol = new proxyProtocol.V2ProxyProtocol(
proxyProtocol.Command.LOCAL,
proxyProtocol.TransportProtocol.DGRAM,
new proxyProtocol.IPv4ProxyAddress(
proxyProtocol.IPv4Address.createFrom(clientIp.split('.')),
12345,
proxyProtocol.IPv4Address.createFrom([127, 0, 0, 1]),
port
),
mqttPacket.generate(packet)
).build()

const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = net.createServer(broker.handle)
server.listen(port, function (err) {
t.error(err, 'no error')
})

const client = net.createConnection(
{
port,
timeout: 0
}, function () {
client.write(Buffer.from(protocol))
}
)

t.tearDown(() => {
client.end()
broker.close()
server.close()
})
})

test('tcp proxied (protocol v2) clients have access to the ipAddress(v6)', function (t) {
t.plan(2)

const port = 4883
const clientIpArray = [0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 168, 1, 128]
const clientIp = '::ffff:c0a8:180:'
const packet = {
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client-proxyV2'
}

const protocol = new proxyProtocol.V2ProxyProtocol(
proxyProtocol.Command.PROXY,
proxyProtocol.TransportProtocol.STREAM,
new proxyProtocol.IPv6ProxyAddress(
proxyProtocol.IPv6Address.createFrom(clientIpArray),
12345,
proxyProtocol.IPv6Address.createWithEmptyAddress(),
port
),
mqttPacket.generate(packet)
).build()

const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = net.createServer(broker.handle)
server.listen(port, function (err) {
t.error(err, 'no error')
})

const client = net.createConnection(
{
port,
timeout: 0
}, function () {
client.write(Buffer.from(protocol))
}
)

t.tearDown(() => {
client.end()
broker.close()
server.close()
})
})

test('websocket clients have access to the ipAddress from the socket (if no ip header)', function (t) {
t.plan(2)

const clientIp = '::ffff:127.0.0.1'
const port = 4883
const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)

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

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

t.tearDown(() => {
client.end(true)
broker.close()
server.close()
})
})

test('websocket proxied clients have access to the ipAddress from x-real-ip header', function (t) {
t.plan(2)

const clientIp = '192.168.0.140'
const port = 4883
const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)

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

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

t.tearDown(() => {
client.end(true)
broker.close()
server.close()
})
})

test('websocket proxied clients have access to the ipAddress from x-forwarded-for header', function (t) {
t.plan(2)

const clientIp = '192.168.0.140'
const port = 4883
const broker = aedes({
preConnect: function (client, done) {
if (client.connDetails && client.connDetails.ipAddress) {
client.ip = client.connDetails.ipAddress
t.equal(clientIp, client.ip)
} else {
t.fail('no ip address present')
}
done(null, true)
},
decodeProtocol: protocolDecoder,
trustProxy: true
})

const server = http.createServer()
ws.createServer({
server: server
}, broker.handle)

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

const client = mqtt.connect(`ws://localhost:${port}`, {
wsOptions: {
headers: {
'X-Forwarded-For': clientIp
}
}
})

t.tearDown(() => {
client.end(true)
broker.close()
server.close()
})
})