web-check/server/lambda/ssl-check.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-06-21 00:01:45 +02:00
const https = require('https');
exports.handler = async function (event, context) {
const { url } = event.queryStringParameters;
const errorResponse = (message, statusCode = 444) => {
return {
statusCode: statusCode,
body: JSON.stringify({ error: message }),
};
};
2023-06-21 00:01:45 +02:00
if (!url) {
2023-07-08 18:00:49 +02:00
return errorResponse('url query parameter is required');
2023-06-21 00:01:45 +02:00
}
return new Promise((resolve, reject) => {
const req = https.request(url, res => {
// Check if the SSL handshake was authorized
if (!res.socket.authorized) {
resolve(errorResponse(`SSL handshake not authorized. Reason: ${res.socket.authorizationError}`));
} else {
2023-07-08 18:00:49 +02:00
let cert = res.socket.getPeerCertificate(true);
if (!cert || Object.keys(cert).length === 0) {
resolve(errorResponse("No certificate presented by the server."));
} else {
2023-07-08 18:00:49 +02:00
// omit the raw and issuerCertificate fields
const { raw, issuerCertificate, ...certWithoutRaw } = cert;
resolve({
statusCode: 200,
2023-07-08 18:00:49 +02:00
body: JSON.stringify(certWithoutRaw),
});
}
}
2023-06-21 00:01:45 +02:00
});
req.on('error', (error) => {
2023-07-08 18:00:49 +02:00
resolve(errorResponse(`Error fetching site certificate: ${error.message}`, 500));
2023-06-21 00:01:45 +02:00
});
req.end();
});
};