Add ssl-checker, refactor fetchMonitorCertificate

This commit is contained in:
Alex Holliday committed 2024-10-17 11:57:37 +08:00
1 parent d8e2b06eca
commit 79cf169ede
5 files changed
+42 -114

No files matched your search

+7 -24
View File
@@ -12,32 +12,15 @@ const handleError = (error, serviceName, method, status = 500) => {
return error;
};
const fetchMonitorCertificate = async (tls, monitor) => {
return new Promise((resolve, reject) => {
const fetchMonitorCertificate = async (sslChecker, monitor) => {
try {
const monitorUrl = new URL(monitor.url);
const hostname = monitorUrl.hostname;
try {
let socket = tls.connect(
{
port: 443,
host: hostname,
servername: hostname, // this is required in case the server enabled SNI
},
() => {
try {
let x509Certificate = socket.getPeerX509Certificate();
resolve(x509Certificate);
} catch (error) {
reject(error);
} finally {
socket.end();
}
}
);
} catch (error) {
reject(error);
}
});
const cert = await sslChecker(hostname);
return cert;
} catch (error) {
throw error;
}
};
export { handleValidationError, handleError, fetchMonitorCertificate };
+3 -7
View File
@@ -12,18 +12,14 @@ import {
getMonitorStatsByIdQueryValidation,
getCertificateParamValidation,
} from "../validation/joi.js";
import * as tls from "tls";
import sslChecker from "ssl-checker";
const SERVICE_NAME = "monitorController";
import { errorMessages, successMessages } from "../utils/messages.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import logger from "../utils/logger.js";
import {
handleError,
handleValidationError,
fetchMonitorCertificate,
} from "./controllerUtils.js";
import { handleError, handleValidationError } from "./controllerUtils.js";
/**
* Returns all monitors
@@ -87,7 +83,7 @@ const getMonitorCertificate = async (req, res, next, fetchMonitorCertificate) =>
try {
const { monitorId } = req.params;
const monitor = await req.db.getMonitorById(monitorId);
const certificate = await fetchMonitorCertificate(tls, monitor);
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
if (certificate && certificate.validTo) {
return res.status(200).json({
success: true,