Added authentication logic

pull/875/head
Tarun Singh 3 years ago
parent 3721d11259
commit 3f5133d1ba

@ -7,4 +7,13 @@ ALTER TABLE monitor
ALTER TABLE monitor
ADD mqtt_success_message VARCHAR(255);
ALTER TABLE monitor
ADD mqtt_port NUMBER(10);
ALTER TABLE monitor
ADD mqtt_username VARCHAR(255);
ALTER TABLE monitor
ADD mqtt_password VARCHAR(255);
COMMIT;

@ -321,7 +321,7 @@ class Monitor extends BeanModel {
}
} else if (this.type === "mqtt") {
try {
bean.msg = await mqttAsync(this.url, this.topic, this.successMessage);
bean.msg = await mqttAsync(this.url, this.mqttPort, this.mqttUsername, this.mqttPassword, this.mqttTopic, this.mqttSuccessMessage);
bean.status = UP;
} catch (error) {
bean.status = DOWN;

@ -90,21 +90,33 @@ exports.pingAsync = function (hostname, ipv6 = false) {
});
};
exports.mqttAsync = function (hostname, topic, okMessage) {
exports.mqttAsync = function (hostname, port = undefined, username = undefined, password = undefined, topic, okMessage) {
return new Promise((resolve, reject) => {
try {
let client = mqtt.connect(hostname);
console.log({
hostname,
port,
username,
password
});
let client = mqtt.connect(hostname, {
port,
username,
password
});
client.on("connect", () => {
console.log(`Connected to ${hostname}:${port}, ${username}, ${password}`);
client.subscribe(topic);
});
client.on("message", (messageTopic, message) => {
console.log(messageTopic);
if (messageTopic == topic && message.toString() !== okMessage) {
client.end();
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
} else {
client.end();
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
if (messageTopic == topic) {
if (message.toString() === okMessage) {
client.end();
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
} else {
client.end();
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
}
}
});
} catch (error) {

@ -126,9 +126,24 @@
<input id="url" v-model="monitor.url" type="text" class="form-control" pattern="https?://.+" required>
</div>
<div class="my-3">
<label for="mqttPort" class="form-label">{{ $t("port") }}</label>
<input id="mqttPort" v-model="monitor.mqttPort" type="text" class="form-control">
</div>
<div class="my-3">
<label for="mqttUsername" class="form-label">{{ $t("username") }}</label>
<input id="mqttUsername" v-model="monitor.mqttUsername" type="text" class="form-control">
</div>
<div class="my-3">
<label for="mqttPassword" class="form-label">{{ $t("password") }}</label>
<input id="mqttPassword" v-model="monitor.mqttPassword" type="text" class="form-control">
</div>
<div class="my-3">
<label for="mqttTopic" class="form-label">{{ $t("topic") }}</label>
<input id="mqttTopic" v-model="monitor.topic" type="text" class="form-control" required>
<input id="mqttTopic" v-model="monitor.mqttTopic" type="text" class="form-control" required>
<div class="form-text">
{{ $t("topicExplanation") }}
</div>
@ -136,7 +151,7 @@
<div class="my-3">
<label for="mqttSuccessMessage" class="form-label">{{ $t("successMessage") }}</label>
<input id="mqttSuccessMessage" v-model="monitor.successMessage" type="text" class="form-control" required>
<input id="mqttSuccessMessage" v-model="monitor.mqttSuccessMessage" type="text" class="form-control" required>
<div class="form-text">
{{ $t("successMessageExplanation") }}
</div>

Loading…
Cancel
Save