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 ALTER TABLE monitor
ADD mqtt_success_message VARCHAR(255); 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; COMMIT;

@ -321,7 +321,7 @@ class Monitor extends BeanModel {
} }
} else if (this.type === "mqtt") { } else if (this.type === "mqtt") {
try { 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; bean.status = UP;
} catch (error) { } catch (error) {
bean.status = DOWN; 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) => { return new Promise((resolve, reject) => {
try { try {
let client = mqtt.connect(hostname); console.log({
hostname,
port,
username,
password
});
let client = mqtt.connect(hostname, {
port,
username,
password
});
client.on("connect", () => { client.on("connect", () => {
console.log(`Connected to ${hostname}:${port}, ${username}, ${password}`);
client.subscribe(topic); client.subscribe(topic);
}); });
client.on("message", (messageTopic, message) => { client.on("message", (messageTopic, message) => {
console.log(messageTopic); if (messageTopic == topic) {
if (messageTopic == topic && message.toString() !== okMessage) { if (message.toString() === okMessage) {
client.end(); client.end();
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`)); resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
} else { } else {
client.end(); client.end();
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`); reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
}
} }
}); });
} catch (error) { } catch (error) {

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

Loading…
Cancel
Save