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 support for MQTT TLS #241

Merged
merged 6 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -29,6 +29,9 @@ module.exports = {
stop_on_match: true,
},
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