From c196c34840f39bb97ba094dc8f642d6f77ce84e1 Mon Sep 17 00:00:00 2001 From: David Twigger Date: Thu, 5 Jan 2023 08:57:48 +0100 Subject: [PATCH] Add mqtt, mqtts, ws and wss protocols to the mqtt monitor --- server/util-server.js | 9 +++++---- src/pages/EditMonitor.vue | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/server/util-server.js b/server/util-server.js index ffd171b4..fb96abe7 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -135,7 +135,7 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { const { port, username, password, interval = 20 } = options; // Adds MQTT protocol to the hostname if not already present - if (!/^(?:http|mqtt)s?:\/\//.test(hostname)) { + if (!/^(?:http|mqtt|ws)s?:\/\//.test(hostname)) { hostname = "mqtt://" + hostname; } @@ -145,10 +145,11 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { reject(new Error("Timeout")); }, interval * 1000 * 0.8); - log.debug("mqtt", "MQTT connecting"); + const mqttUrl = `${hostname}:${port}` + + log.debug("mqtt", `MQTT connecting to ${mqttUrl}`); - let client = mqtt.connect(hostname, { - port, + let client = mqtt.connect(mqttUrl, { username, password }); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index c9d5ad2f..315d1e6c 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -604,7 +604,8 @@ export default { // Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ ipRegexPattern: "((^\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\s*$)|(^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$))", // Source: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address - hostnameRegexPattern: "^(([a-zA-Z0-9_]|[a-zA-Z0-9_][a-zA-Z0-9\\-_]*[a-zA-Z0-9_])\\.)*([A-Za-z0-9_]|[A-Za-z0-9_][A-Za-z0-9\\-_]*[A-Za-z0-9_])$" + // Modified to accept http, https, mqtt, mqtts, ws and wss protocols accepted by mqtt.js (https://github.com/mqttjs/MQTT.js/#connect) + hostnameRegexPattern: "^((http|mqtt|ws)s?:\/\/)?([a-zA-Z0-9])?(([a-zA-Z0-9_]|[a-zA-Z0-9_][a-zA-Z0-9\\-_]*[a-zA-Z0-9_])\\.)*([A-Za-z0-9_]|[A-Za-z0-9_][A-Za-z0-9\\-_]*[A-Za-z0-9_])$" }; },