Skip to content

Commit

Permalink
New TLS monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubli committed May 9, 2022
1 parent 15820c6 commit 6d92a60
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
8 changes: 7 additions & 1 deletion server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dayjs.extend(timezone);
const axios = require("axios");
const { Prometheus } = require("../prometheus");
const { log, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util");
const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog, mqttAsync } = require("../util-server");
const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog, mqttAsync, tlsAsync } = require("../util-server");
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
const { Notification } = require("../notification");
Expand Down Expand Up @@ -434,6 +434,12 @@ class Monitor extends BeanModel {
interval: this.interval,
});
bean.status = UP;
} else if (this.type === "tls") {
bean.msg = await tlsAsync(this.hostname, this.port, {
keyword: this.keyword,
interval: this.interval,
});
bean.status = UP;
} else {
bean.msg = "Unknown Monitor Type";
bean.status = PENDING;
Expand Down
56 changes: 56 additions & 0 deletions server/util-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const chardet = require("chardet");
const fs = require("fs");
const nodeJsUtil = require("util");
const mqtt = require("mqtt");
const tls = require("tls");

// From ping-lite
exports.WIN = /^win/.test(process.platform);
Expand Down Expand Up @@ -172,6 +173,61 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
});
};

exports.tlsAsync = function (hostname, port, options = {}) {
return new Promise((resolve, reject) => {
const { keyword, interval = 60 } = options;

const timeoutID = setTimeout(() => {
log.debug("tls", "Timeout triggered");
client.end();
reject(new Error("Timeout"));
}, interval * 1000 * 0.8);

log.debug("tls", "Connecting to " + hostname + ":" + port);

let client = tls.connect(port, hostname, {});

client.on("secureConnect", () => {
const cipher = client.getCipher();
log.debug("tls", "Connected: authorized: " + client.authorized + ", cipher: " + (cipher ? cipher.name : '<none>'));
});

client.on("error", (error) => {
log.debug("tls", "Error: " + error);
client.destroy();
clearTimeout(timeoutID);
reject(error);
});

client.on("data", (data) => {
const dataString = data.toString().replace(/^\s+|\s+$/g, "");
log.debug("tls", "Data: '" + dataString + "', keyword: '" + (keyword || "") + "'");
client.end();
clearTimeout(timeoutID);
let success = false;
let message = undefined;
if (keyword) {
if (dataString.includes(keyword)) {
success = true;
message = "Data contains keyword";
} else {
message = "Data does not contain keyword";
}
} else {
message = "Data received";
success = true;
}
if (success) {
log.debug("tls", "Success: " + message);
resolve(message);
} else {
log.debug("tls", "Failure: " + message);
reject(new Error(message));
}
});
});
};

/**
* Resolves a given record using the specified DNS server
* @param {string} hostname The hostname of the record to lookup
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<a v-if="monitor.type === 'http' || monitor.type === 'keyword' " :href="monitor.url" target="_blank">{{ monitor.url }}</a>
<span v-if="monitor.type === 'port'">TCP Ping {{ monitor.hostname }}:{{ monitor.port }}</span>
<span v-if="monitor.type === 'ping'">Ping: {{ monitor.hostname }}</span>
<span v-if="monitor.type === 'keyword'">
<span v-if="monitor.type === 'keyword' || monitor.type === 'tls'">
<br>
<span>{{ $t("Keyword") }}:</span> <span class="keyword">{{ monitor.keyword }}</span>
</span>
Expand Down
13 changes: 8 additions & 5 deletions src/pages/EditMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<option value="mqtt">
MQTT
</option>
<option value="tls">
TLS
</option>
</select>
</div>

Expand All @@ -61,7 +64,7 @@
</div>

<!-- Keyword -->
<div v-if="monitor.type === 'keyword' " class="my-3">
<div v-if="monitor.type === 'keyword' || monitor.type === 'tls'" class="my-3">
<label for="keyword" class="form-label">{{ $t("Keyword") }}</label>
<input id="keyword" v-model="monitor.keyword" type="text" class="form-control" required>
<div class="form-text">
Expand All @@ -70,15 +73,15 @@
</div>

<!-- Hostname -->
<!-- TCP Port / Ping / DNS / Steam / MQTT only -->
<div v-if="monitor.type === 'port' || monitor.type === 'ping' || monitor.type === 'dns' || monitor.type === 'steam' || monitor.type === 'mqtt'" class="my-3">
<!-- TCP Port / Ping / DNS / Steam / MQTT / TLS only -->
<div v-if="monitor.type === 'port' || monitor.type === 'ping' || monitor.type === 'dns' || monitor.type === 'steam' || monitor.type === 'mqtt' || monitor.type === 'tls'" class="my-3">
<label for="hostname" class="form-label">{{ $t("Hostname") }}</label>
<input id="hostname" v-model="monitor.hostname" type="text" class="form-control" :pattern="`${ipRegexPattern}|${hostnameRegexPattern}`" required>
</div>

<!-- Port -->
<!-- For TCP Port / Steam / MQTT Type -->
<div v-if="monitor.type === 'port' || monitor.type === 'steam' || monitor.type === 'mqtt'" class="my-3">
<!-- For TCP Port / Steam / MQTT / TLS Type -->
<div v-if="monitor.type === 'port' || monitor.type === 'steam' || monitor.type === 'mqtt' || monitor.type === 'tls'" class="my-3">
<label for="port" class="form-label">{{ $t("Port") }}</label>
<input id="port" v-model="monitor.port" type="number" class="form-control" required min="0" max="65535" step="1">
</div>
Expand Down

0 comments on commit 6d92a60

Please sign in to comment.