1 | 'use strict'
|
2 | var tls = require('tls')
|
3 |
|
4 | function buildBuilder (mqttClient, opts) {
|
5 | var connection
|
6 | opts.port = opts.port || 8883
|
7 | opts.host = opts.hostname || opts.host || 'localhost'
|
8 |
|
9 | opts.rejectUnauthorized = opts.rejectUnauthorized !== false
|
10 |
|
11 | delete opts.path
|
12 |
|
13 | connection = tls.connect(opts)
|
14 |
|
15 | connection.on('secureConnect', function () {
|
16 | if (opts.rejectUnauthorized && !connection.authorized) {
|
17 | connection.emit('error', new Error('TLS not authorized'))
|
18 | } else {
|
19 | connection.removeListener('error', handleTLSerrors)
|
20 | }
|
21 | })
|
22 |
|
23 | function handleTLSerrors (err) {
|
24 |
|
25 | if (opts.rejectUnauthorized) {
|
26 | mqttClient.emit('error', err)
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | connection.end()
|
35 | }
|
36 |
|
37 | connection.on('error', handleTLSerrors)
|
38 | return connection
|
39 | }
|
40 |
|
41 | module.exports = buildBuilder
|