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: add quic support #1298

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if ((typeof process !== 'undefined' && process.title !== 'browser') || typeof __
protocols.ssl = require('./tls')
protocols.tls = require('./tls')
protocols.mqtts = require('./tls')
protocols.quic = require('./quic')
} else {
protocols.wx = require('./wx')
protocols.wxs = require('./wx')
Expand Down Expand Up @@ -85,7 +86,7 @@ function connect (brokerUrl, opts) {

if (opts.cert && opts.key) {
if (opts.protocol) {
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
if (['mqtts', 'wss', 'wxs', 'alis', "quic"].indexOf(opts.protocol) === -1) {
switch (opts.protocol) {
case 'mqtt':
opts.protocol = 'mqtts'
Expand Down
56 changes: 56 additions & 0 deletions lib/connect/quic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict'
var {createQuicSocket} = require('net')
var debug = require('debug')('mqttjs:quic')

function buildBuilder(mqttClient, opts) {

opts.port = opts.port || 8885
opts.host = opts.hostname || opts.host || 'localhost'
opts.servername = opts.host

opts.rejectUnauthorized = opts.rejectUnauthorized !== false

delete opts.path

debug('port %d host %s rejectUnauthorized %b', opts.port, opts.host, opts.rejectUnauthorized)

const socket = createQuicSocket({
client: {
alpn: opts.alpn || 'mqtt',
}
})

const req = socket.connect({
address: opts.host,
port: opts.port || 8885,
cert: opts.cert,
key: opts.key,
idleTimeout:0 //disable timeout
})

var stream = req.openStream()

req.on('secure', function (servername) {
// servername is checked for any string for authorisation acceptance
if (opts.rejectUnauthorized && !servername) {
req.emit('error', new Error('TLS not authorized'))
} else {
req.removeListener('error', handleTLSErrors)
}
})

function handleTLSErrors(err) {

if (opts.rejectUnauthorized) {
mqttClient.emit('error', err)
}

stream.end()
socket.close()
}

req.on('error', handleTLSErrors)
return stream
}

module.exports = buildBuilder