2023-06-21 00:01:45 +02:00
|
|
|
const https = require('https');
|
2023-08-09 23:33:36 +02:00
|
|
|
const middleware = require('./_common/middleware');
|
2023-06-21 00:01:45 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
const fetchSiteCertificateHandler = async (url) => {
|
2023-07-10 00:23:50 +02:00
|
|
|
try {
|
|
|
|
const response = await new Promise((resolve, reject) => {
|
|
|
|
const req = https.request(url, res => {
|
2023-08-09 23:33:36 +02:00
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
// Check if the SSL handshake was authorized
|
|
|
|
if (!res.socket.authorized) {
|
2023-08-09 23:33:36 +02:00
|
|
|
reject(new Error(`SSL handshake not authorized. Reason: ${res.socket.authorizationError}`));
|
2023-07-07 21:56:58 +02:00
|
|
|
} else {
|
2023-07-10 00:23:50 +02:00
|
|
|
let cert = res.socket.getPeerCertificate(true);
|
|
|
|
if (!cert || Object.keys(cert).length === 0) {
|
2023-08-09 23:33:36 +02:00
|
|
|
reject(new Error("No certificate presented by the server."));
|
2023-07-10 00:23:50 +02:00
|
|
|
} else {
|
|
|
|
// omit the raw and issuerCertificate fields
|
|
|
|
const { raw, issuerCertificate, ...certWithoutRaw } = cert;
|
2023-08-09 23:33:36 +02:00
|
|
|
resolve(certWithoutRaw);
|
2023-07-10 00:23:50 +02:00
|
|
|
}
|
2023-07-07 21:56:58 +02:00
|
|
|
}
|
2023-07-10 00:23:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', error => {
|
2023-08-09 23:33:36 +02:00
|
|
|
reject(new Error(`Error fetching site certificate: ${error.message}`));
|
2023-07-10 00:23:50 +02:00
|
|
|
});
|
2023-06-21 00:01:45 +02:00
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
req.end();
|
2023-06-21 00:01:45 +02:00
|
|
|
});
|
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
2023-08-09 23:33:36 +02:00
|
|
|
throw new Error(error.message);
|
2023-07-10 00:23:50 +02:00
|
|
|
}
|
2023-06-21 00:01:45 +02:00
|
|
|
};
|
2023-08-09 23:33:36 +02:00
|
|
|
|
|
|
|
exports.handler = middleware(fetchSiteCertificateHandler);
|