mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 18:34:55 +08:00
Feat: Add Barebones certificate info display
This commit is contained in:
@@ -6,7 +6,7 @@ dayjs.extend(utc)
|
||||
dayjs.extend(timezone)
|
||||
const axios = require("axios");
|
||||
const {UP, DOWN, PENDING} = require("../util");
|
||||
const {tcping, ping} = require("../util-server");
|
||||
const {tcping, ping, checkCertificate} = require("../util-server");
|
||||
const {R} = require("redbean-node");
|
||||
const {BeanModel} = require("redbean-node/dist/bean-model");
|
||||
const {Notification} = require("../notification")
|
||||
@@ -79,6 +79,9 @@ class Monitor extends BeanModel {
|
||||
})
|
||||
bean.msg = `${res.status} - ${res.statusText}`
|
||||
bean.ping = dayjs().valueOf() - startTime;
|
||||
if (this.url.startsWith("https")) {
|
||||
Monitor.sendCertInfo(checkCertificate(res), io, this.id, this.user_id);
|
||||
}
|
||||
|
||||
if (this.type === "http") {
|
||||
bean.status = UP;
|
||||
@@ -218,6 +221,14 @@ class Monitor extends BeanModel {
|
||||
io.to(userID).emit("avgPing", monitorID, avgPing);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param checkCertificateResult : Object return result of checkCertificate
|
||||
*/
|
||||
static async sendCertInfo(checkCertificateResult, io, monitorID, userID) {
|
||||
io.to(userID).emit("certInfo", monitorID, checkCertificateResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uptime with calculation
|
||||
* Calculation based on:
|
||||
|
@@ -70,3 +70,53 @@ exports.getSettings = async function (type) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ssl-checker by @dyaa
|
||||
// param: res - response object from axios
|
||||
// return an object containing the certificate information
|
||||
|
||||
const getDaysBetween = (validFrom, validTo) =>
|
||||
Math.round(Math.abs(+validFrom - +validTo) / 8.64e7);
|
||||
|
||||
const getDaysRemaining = (validFrom, validTo) => {
|
||||
const daysRemaining = getDaysBetween(validFrom, validTo);
|
||||
if (new Date(validTo).getTime() < new Date().getTime()) {
|
||||
return -daysRemaining;
|
||||
}
|
||||
return daysRemaining;
|
||||
};
|
||||
|
||||
exports.checkCertificate = function (res) {
|
||||
const {
|
||||
valid_from,
|
||||
valid_to,
|
||||
subjectaltname,
|
||||
issuer,
|
||||
fingerprint,
|
||||
} = res.request.res.socket.getPeerCertificate(false);
|
||||
|
||||
if (!valid_from || !valid_to || !subjectaltname) {
|
||||
reject(new Error('No certificate'));
|
||||
return;
|
||||
}
|
||||
|
||||
const valid = res.request.res.socket.authorized || false;
|
||||
|
||||
const validTo = new Date(valid_to);
|
||||
|
||||
const validFor = subjectaltname
|
||||
.replace(/DNS:|IP Address:/g, "")
|
||||
.split(", ");
|
||||
|
||||
const daysRemaining = getDaysRemaining(new Date(), validTo);
|
||||
|
||||
return {
|
||||
valid,
|
||||
validFor,
|
||||
validTo,
|
||||
daysRemaining,
|
||||
issuer,
|
||||
fingerprint,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user