diff --git a/server/model/monitor.js b/server/model/monitor.js index 80cff6f9..64fd4578 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -533,6 +533,12 @@ class Monitor extends BeanModel { } } + let tlsInfo; + // Store TLS Info when key material is received + options.httpsAgent.on("keylog", async (line, tlsSocket) => { + tlsInfo = checkCertificate(tlsSocket); + }); + log.debug("monitor", `[${this.name}] Axios Options: ${JSON.stringify(options)}`); log.debug("monitor", `[${this.name}] Axios Request`); @@ -542,29 +548,22 @@ class Monitor extends BeanModel { bean.msg = `${res.status} - ${res.statusText}`; bean.ping = dayjs().valueOf() - startTime; - // Check certificate if https is used - let certInfoStartTime = dayjs().valueOf(); + // Store certificate and check for expiry if https is used if (this.getUrl()?.protocol === "https:") { - log.debug("monitor", `[${this.name}] Check cert`); - try { - let tlsInfoObject = checkCertificate(res); - tlsInfo = await this.updateTlsInfo(tlsInfoObject); - - if (!this.getIgnoreTls() && this.isEnabledExpiryNotification()) { - log.debug("monitor", `[${this.name}] call checkCertExpiryNotifications`); - await this.checkCertExpiryNotifications(tlsInfoObject); - } - - } catch (e) { - if (e.message !== "No TLS certificate in response") { - log.error("monitor", "Caught error"); - log.error("monitor", e.message); + // No way to listen for the `secureConnection` event, so we do it here + const tlssocket = res.request.res.socket; + if (tlssocket) { + tlsInfo.valid = tlssocket.authorized || false; + if (!tlssocket.authorized) { + tlsInfo.authorizationError = tlssocket.authorizationError; } } - } - if (process.env.TIMELOGGER === "1") { - log.debug("monitor", "Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms"); + await this.updateTlsInfo(tlsInfo); + if (!this.getIgnoreTls() && this.isEnabledExpiryNotification()) { + log.debug("monitor", `[${this.name}] call checkCertExpiryNotifications`); + await this.checkCertExpiryNotifications(tlsInfo); + } } if (process.env.UPTIME_KUMA_LOG_RESPONSE_BODY_MONITOR_ID === this.id) { diff --git a/server/util-server.js b/server/util-server.js index 8add5bc5..056469ee 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -653,21 +653,27 @@ const parseCertificateInfo = function (info) { /** * Check if certificate is valid - * @param {object} res Response object from axios + * @param {tls.TLSSocket} socket TLSSocket, which may or may not be connected * @returns {object} Object containing certificate information - * @throws No socket was found to check certificate for */ -exports.checkCertificate = function (res) { - if (!res.request.res.socket) { - throw new Error("No socket found"); +exports.checkCertificate = function (socket) { + // Return null if there is no socket + if (socket === undefined || socket == null) { + return null; } - const info = res.request.res.socket.getPeerCertificate(true); - const valid = res.request.res.socket.authorized || false; + let certInfoStartTime = dayjs().valueOf(); + + const info = socket.getPeerCertificate(true); + const valid = socket.authorized || false; log.debug("cert", "Parsing Certificate Info"); const parsedInfo = parseCertificateInfo(info); + if (process.env.TIMELOGGER === "1") { + log.debug("monitor", "Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms"); + } + return { valid: valid, certInfo: parsedInfo diff --git a/src/components/CertificateInfo.vue b/src/components/CertificateInfo.vue index cb1a8291..fd9d0775 100644 --- a/src/components/CertificateInfo.vue +++ b/src/components/CertificateInfo.vue @@ -1,20 +1,34 @@ @@ -25,16 +39,11 @@ export default { CertificateInfoRow, }, props: { - /** Object representing certificate */ - certInfo: { + /** Object representing TLS information */ + tlsInfo: { type: Object, required: true, }, - /** Is the TLS certificate valid? */ - valid: { - type: Boolean, - required: true, - }, }, }; diff --git a/src/pages/Details.vue b/src/pages/Details.vue index 231870ef..21c3e38e 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -174,7 +174,7 @@
- +