Skip to content

Commit

Permalink
feat: support for MQTT TLS (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
konturn authored Oct 20, 2022
1 parent effad62 commit 7f37b78
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ mqtt:
password:
client_id:
tls:
# cert chains in PEM format: /path/to/client.crt
cert:
# private keys in PEM format: /path/to/client.key
key:
# optionally override the trusted CA certificates: /path/to/ca.crt
ca:
# if true the server will reject any connection which is not authorized with the list of supplied CAs
reject_unauthorized: false
topics:
# mqtt topic for frigate message subscription
frigate: frigate/events
Expand Down
3 changes: 3 additions & 0 deletions api/src/constants/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module.exports = {
min_area: 0,
},
mqtt: {
tls: {
reject_unauthorized: false,
},
topics: {
frigate: 'frigate/events',
matches: 'double-take/matches',
Expand Down
46 changes: 28 additions & 18 deletions api/src/util/mqtt.util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const filesystem = require('fs');
const { v4: uuidv4 } = require('uuid');
const axios = require('axios');
const mqtt = require('mqtt');
Expand Down Expand Up @@ -86,24 +87,33 @@ const processMessage = ({ topic, message }) => {

module.exports.connect = () => {
if (!MQTT || !MQTT.HOST) return;
CLIENT = mqtt.connect(`mqtt://${MQTT.HOST}`, {
reconnectPeriod: 10000,
username: MQTT.USERNAME || MQTT.USER,
password: MQTT.PASSWORD || MQTT.PASS,
clientId: MQTT.CLIENT_ID || `double-take-${Math.random().toString(16).substr(2, 8)}`,
});

CLIENT.on('connect', () => {
logStatus('connected', console.log);
this.publish({ topic: 'double-take/errors' });
this.available('online');
this.subscribe();
})
.on('error', (err) => logStatus(err.message, console.error))
.on('offline', () => logStatus('offline', console.error))
.on('disconnect', () => logStatus('disconnected', console.error))
.on('reconnect', () => logStatus('reconnecting', console.warn))
.on('message', async (topic, message) => processMessage({ topic, message }).init());

try {
CLIENT = mqtt.connect(`mqtt://${MQTT.HOST}`, {
reconnectPeriod: 10000,
username: MQTT.USERNAME || MQTT.USER,
password: MQTT.PASSWORD || MQTT.PASS,
clientId: MQTT.CLIENT_ID || `double-take-${Math.random().toString(16).substr(2, 8)}`,
key: MQTT.TLS.KEY ? filesystem.readFileSync(MQTT.TLS.KEY) : null,
cert: MQTT.TLS.CERT ? filesystem.readFileSync(MQTT.TLS.CERT) : null,
ca: MQTT.TLS.CA ? filesystem.readFileSync(MQTT.TLS.CA) : null,
rejectUnauthorized: MQTT.TLS.REJECT_UNAUTHORIZED === true,
});

CLIENT.on('connect', () => {
logStatus('connected', console.log);
this.publish({ topic: 'double-take/errors' });
this.available('online');
this.subscribe();
})
.on('error', (err) => logStatus(err.message, console.error))
.on('offline', () => logStatus('offline', console.error))
.on('disconnect', () => logStatus('disconnected', console.error))
.on('reconnect', () => logStatus('reconnecting', console.warn))
.on('message', async (topic, message) => processMessage({ topic, message }).init());
} catch (error) {
logStatus(error.message, console.error);
}
};

module.exports.available = async (state) => {
Expand Down

0 comments on commit 7f37b78

Please sign in to comment.