diff --git a/server/model/monitor.js b/server/model/monitor.js index 58decbcba..674b36944 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -24,6 +24,7 @@ const { CookieJar } = require("tough-cookie"); const { HttpsCookieAgent } = require("http-cookie-agent/http"); const https = require("https"); const http = require("http"); +const tls = require("tls"); const rootCertificates = rootCertificatesFingerprints(); @@ -580,6 +581,45 @@ class Monitor extends BeanModel { } else if (this.type === "port") { bean.ping = await tcping(this.hostname, this.port); + if (this.isEnabledExpiryNotification()) { + const host = this.hostname; + const port = this.port || 443; + try { + const options = { + host, + port, + servername: host, + }; + + // Convert TLS connect to a Promise and await it + const tlsInfoObject = await new Promise((resolve, reject) => { + const socket = tls.connect(options); + + socket.on("secureConnect", () => { + try { + const info = checkCertificate(socket); + socket.end(); + resolve(info); + } catch (error) { + socket.end(); + reject(error); + } + }); + + socket.on("error", (error) => { + reject(error); + }); + + socket.setTimeout(10000, () => { + socket.end(); + reject(new Error("Connection timed out")); + }); + }); + await this.handleTlsInfo(tlsInfoObject); + } catch (error) { + console.log("Retrieve certificate failed"); + } + } bean.msg = ""; bean.status = UP; diff --git a/server/monitor-types/tls.js b/server/monitor-types/tls.js deleted file mode 100644 index ea9b239ed..000000000 --- a/server/monitor-types/tls.js +++ /dev/null @@ -1,77 +0,0 @@ -const { MonitorType } = require("./monitor-type"); -const { UP, DOWN } = require("../../src/util"); -const { checkCertificate, setting, setSetting } = require("../util-server"); -const tls = require("tls"); - -class TlsCertificateMonitorType extends MonitorType { - name = "tlsCheck"; - - /** - * @inheritdoc - */ - async check(monitor, heartbeat, server) { - const host = monitor.hostname; - const port = monitor.port || 443; - let notifyDays = await setting("tlsExpiryNotifyDays"); - if (notifyDays == null || !Array.isArray(notifyDays)) { - // Reset Default - await setSetting("tlsExpiryNotifyDays", [ 7, 14, 21 ], "general"); - notifyDays = [ 7, 14, 21 ]; - } - - try { - const options = { - host, - port, - servername: host, - }; - - // Convert TLS connect to a Promise and await it - const tlsInfoObject = await new Promise((resolve, reject) => { - const socket = tls.connect(options); - - socket.on("secureConnect", () => { - try { - const info = checkCertificate(socket); - socket.end(); - resolve(info); - } catch (error) { - socket.end(); - reject(error); - } - }); - - socket.on("error", (error) => { - reject(error); - }); - - socket.setTimeout(10000, () => { - socket.end(); - reject(new Error("Connection timed out")); - }); - }); - - const certInfo = tlsInfoObject.certInfo; - - await monitor.updateTlsInfo(tlsInfoObject); - const alertDays = notifyDays.filter(targetDays => targetDays >= certInfo.daysRemaining); - - if (alertDays.length === 0) { - heartbeat.status = UP; - heartbeat.msg = ""; - } else { - const alertDay = Math.min(...alertDays); - heartbeat.status = DOWN; - heartbeat.msg = `Certificate expires in less thant ${alertDay} days`; - } - } catch (error) { - heartbeat.status = DOWN; - heartbeat.msg = `Error checking SSL certificate: ${error.message}`; - } - } -} - -module.exports = { - TlsCertificateMonitorType, -}; - diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 2d65573e7..cdaa83dfc 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -117,7 +117,6 @@ class UptimeKumaServer { UptimeKumaServer.monitorTypeList["snmp"] = new SNMPMonitorType(); UptimeKumaServer.monitorTypeList["mongodb"] = new MongodbMonitorType(); UptimeKumaServer.monitorTypeList["rabbitmq"] = new RabbitMqMonitorType(); - UptimeKumaServer.monitorTypeList["tls"] = new TlsCertificateMonitorType(); // Allow all CORS origins (polling) in development let cors = undefined; @@ -557,5 +556,4 @@ const { GroupMonitorType } = require("./monitor-types/group"); const { SNMPMonitorType } = require("./monitor-types/snmp"); const { MongodbMonitorType } = require("./monitor-types/mongodb"); const { RabbitMqMonitorType } = require("./monitor-types/rabbitmq"); -const { TlsCertificateMonitorType } = require("./monitor-types/tls"); const Monitor = require("./model/monitor"); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 70cec0325..a8b481de3 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -21,9 +21,6 @@ - @@ -285,7 +282,7 @@ -