Skip to content

Commit

Permalink
Fix and improve MQTT broker URL validation
Browse files Browse the repository at this point in the history
README was actually wrong, and we should allow mqtts:// too
  • Loading branch information
Jalle19 committed Aug 13, 2023
1 parent 4e5c0e5 commit a30fc87
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Options:
[boolean] [default: true]
-a, --httpListenAddress The address to listen (HTTP) [default: "0.0.0.0"]
-p, --httpPort The port to listen on (HTTP) [default: 8080]
-m, --mqttBrokerUrl The URL to the MQTT broker, e.g. tcp://localhost:18
-m, --mqttBrokerUrl The URL to the MQTT broker, e.g. mqtt://localhost:18
83. Omit to disable MQTT support.
--mqttUsername The username to use when connecting to the MQTT bro
ker. Omit to disable authentication.
Expand Down
2 changes: 1 addition & 1 deletion app/mqtt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const handleMessage = async (modbusClient, mqttClient, topicName, payload
}

export const validateBrokerUrl = (brokerUrl) => {
return brokerUrl.startsWith('tcp://')
return brokerUrl.startsWith('mqtt://') || brokerUrl.startsWith('mqtts://')
}

const createBinaryValue = (value) => {
Expand Down
5 changes: 3 additions & 2 deletions tests/mqtt.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { validateBrokerUrl } from '../app/mqtt.mjs'

test('validateMqttUrl', () => {
expect(validateBrokerUrl('tcp://localhost:1883')).toEqual(true)
expect(validateBrokerUrl('tcp://localhost')).toEqual(true)
expect(validateBrokerUrl('mqtt://localhost:1883')).toEqual(true)
expect(validateBrokerUrl('mqtts://localhost:1883')).toEqual(true)
expect(validateBrokerUrl('mqtt://localhost')).toEqual(true)
expect(validateBrokerUrl('localhost:1883')).toEqual(false)
expect(validateBrokerUrl('localhost')).toEqual(false)
})

0 comments on commit a30fc87

Please sign in to comment.