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 };