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

feat: support SNI on TLS #1055

Merged
merged 3 commits into from
Feb 24, 2020
Merged
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
1 change: 1 addition & 0 deletions lib/connect/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function buildBuilder (mqttClient, opts) {
var connection
opts.port = opts.port || 8883
opts.host = opts.hostname || opts.host || 'localhost'
opts.servername = opts.host

opts.rejectUnauthorized = opts.rejectUnauthorized !== false

Expand Down
29 changes: 29 additions & 0 deletions test/secure_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var KEY = path.join(__dirname, 'helpers', 'tls-key.pem')
var CERT = path.join(__dirname, 'helpers', 'tls-cert.pem')
var WRONG_CERT = path.join(__dirname, 'helpers', 'wrong-cert.pem')
var Server = require('./server')
var assert = require('chai').assert

var server = new Server.SecureServer({
key: fs.readFileSync(KEY),
Expand Down Expand Up @@ -153,5 +154,33 @@ describe('MqttSecureClient', function () {
done()
})
})

it.only('should support SNI on the TLS connection', function (done) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not have landed as it has a only still in the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I'll revert it, I just wasn't getting Travis CI to even identify the branch to run testing.

var hostname, client
server.removeAllListeners('secureConnection') // clear eventHandler
server.once('secureConnection', function (tlsSocket) { // one time eventHandler
assert.equal(tlsSocket.servername, hostname) // validate SNI set
server.setupConnection(tlsSocket)
})


hostname = 'localhost'
client = mqtt.connect({
protocol: 'mqtts',
port: port,
ca: [fs.readFileSync(CERT)],
rejectUnauthorized: true,
host: hostname
})

client.on('error', function (err) {
done(err)
})

server.once('connect', function () {
server.on('secureConnection', server.setupConnection) // reset eventHandler
done()
})
})
})
})
4 changes: 3 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var MqttServer
var FastMqttServer
var MqttSecureServer

function setupConnection (duplex) {
var setupConnection = function (duplex) {
var that = this
var connection = new Connection(duplex, function () {
that.emit('client', connection)
Expand Down Expand Up @@ -91,3 +91,5 @@ MqttSecureServer = module.exports.SecureServer =
return this
}
inherits(MqttSecureServer, tls.Server)
MqttSecureServer.prototype.setupConnection = setupConnection