From d6af9162c14a0738d51dbb5426fcc00cf58a2a03 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sat, 9 Sep 2023 12:14:55 +0200 Subject: [PATCH] Chore: Extracted the dns monitor to its own monitor-type (#3413) * extracted the dns monitor to its own monitor-type * linting fixes * another formatting fix * Fix: Improve dnsMessage handling (#3614) * fixed docs * fixed formatting changes --- server/model/monitor.js | 42 +-------------------------- server/monitor-types/dns.js | 56 ++++++++++++++++++++++++++++++++++++ server/uptime-kuma-server.js | 2 ++ 3 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 server/monitor-types/dns.js diff --git a/server/model/monitor.js b/server/model/monitor.js index 85d98d181..58c3f5eb8 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -5,7 +5,7 @@ const { Prometheus } = require("../prometheus"); const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, TimeLogger, MAX_INTERVAL_SECOND, MIN_INTERVAL_SECOND, SQL_DATETIME_FORMAT } = require("../../src/util"); -const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, mqttAsync, setSetting, httpNtlm, radius, grpcQuery, +const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, mqttAsync, setSetting, httpNtlm, radius, grpcQuery, redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, } = require("../util-server"); const { R } = require("redbean-node"); @@ -584,46 +584,6 @@ class Monitor extends BeanModel { bean.ping = await ping(this.hostname, this.packetSize); bean.msg = ""; bean.status = UP; - } else if (this.type === "dns") { - let startTime = dayjs().valueOf(); - let dnsMessage = ""; - - let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.port, this.dns_resolve_type); - bean.ping = dayjs().valueOf() - startTime; - - if (this.dns_resolve_type === "A" || this.dns_resolve_type === "AAAA" || this.dns_resolve_type === "TXT" || this.dns_resolve_type === "PTR") { - dnsMessage += "Records: "; - dnsMessage += dnsRes.join(" | "); - } else if (this.dns_resolve_type === "CNAME") { - dnsMessage += dnsRes[0]; - } else if (this.dns_resolve_type === "CAA") { - dnsMessage += dnsRes[0].issue; - } else if (this.dns_resolve_type === "MX") { - dnsRes.forEach(record => { - dnsMessage += `Hostname: ${record.exchange} - Priority: ${record.priority} | `; - }); - dnsMessage = dnsMessage.slice(0, -2); - } else if (this.dns_resolve_type === "NS") { - dnsMessage += "Servers: "; - dnsMessage += dnsRes.join(" | "); - } else if (this.dns_resolve_type === "SOA") { - dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; - } else if (this.dns_resolve_type === "SRV") { - dnsRes.forEach(record => { - dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; - }); - dnsMessage = dnsMessage.slice(0, -2); - } - - if (this.dnsLastResult !== dnsMessage && dnsMessage !== undefined) { - R.exec("UPDATE `monitor` SET dns_last_result = ? WHERE id = ? ", [ - dnsMessage, - this.id - ]); - } - - bean.msg = dnsMessage; - bean.status = UP; } else if (this.type === "push") { // Type: Push log.debug("monitor", `[${this.name}] Checking monitor at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`); const bufferTime = 1000; // 1s buffer to accommodate clock differences diff --git a/server/monitor-types/dns.js b/server/monitor-types/dns.js new file mode 100644 index 000000000..d4cd02ab5 --- /dev/null +++ b/server/monitor-types/dns.js @@ -0,0 +1,56 @@ +const { MonitorType } = require("./monitor-type"); +const { UP } = require("../../src/util"); +const dayjs = require("dayjs"); +const { dnsResolve } = require("../util-server"); +const { R } = require("redbean-node"); + +class DnsMonitorType extends MonitorType { + + name = "dns"; + + /** + * @inheritdoc + */ + async check(monitor, heartbeat, _server) { + let startTime = dayjs().valueOf(); + let dnsMessage = ""; + + let dnsRes = await dnsResolve(monitor.hostname, monitor.dns_resolve_server, monitor.port, monitor.dns_resolve_type); + heartbeat.ping = dayjs().valueOf() - startTime; + + if (monitor.dns_resolve_type === "A" || monitor.dns_resolve_type === "AAAA" || monitor.dns_resolve_type === "TXT" || monitor.dns_resolve_type === "PTR") { + dnsMessage += "Records: "; + dnsMessage += dnsRes.join(" | "); + } else if (monitor.dns_resolve_type === "CNAME" || monitor.dns_resolve_type === "PTR") { + dnsMessage += dnsRes[0]; + } else if (monitor.dns_resolve_type === "CAA") { + dnsMessage += dnsRes[0].issue; + } else if (monitor.dns_resolve_type === "MX") { + dnsRes.forEach(record => { + dnsMessage += `Hostname: ${record.exchange} - Priority: ${record.priority} | `; + }); + dnsMessage = dnsMessage.slice(0, -2); + } else if (monitor.dns_resolve_type === "NS") { + dnsMessage += "Servers: "; + dnsMessage += dnsRes.join(" | "); + } else if (monitor.dns_resolve_type === "SOA") { + dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; + } else if (monitor.dns_resolve_type === "SRV") { + dnsRes.forEach(record => { + dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; + }); + dnsMessage = dnsMessage.slice(0, -2); + } + + if (monitor.dns_last_result !== dnsMessage && dnsMessage !== undefined) { + await R.exec("UPDATE `monitor` SET dns_last_result = ? WHERE id = ? ", [ dnsMessage, monitor.id ]); + } + + heartbeat.msg = dnsMessage; + heartbeat.status = UP; + } +} + +module.exports = { + DnsMonitorType, +}; diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index ff34e8ac7..da3d08bdf 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -110,6 +110,7 @@ class UptimeKumaServer { // Set Monitor Types UptimeKumaServer.monitorTypeList["real-browser"] = new RealBrowserMonitorType(); UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing(); + UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType(); this.io = new Server(this.httpServer); } @@ -419,3 +420,4 @@ module.exports = { // Must be at the end to avoid circular dependencies const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor-type"); const { TailscalePing } = require("./monitor-types/tailscale-ping"); +const { DnsMonitorType } = require("./monitor-types/dns");