2023-06-21 00:01:45 +02:00
|
|
|
const https = require('https');
|
|
|
|
|
|
|
|
exports.handler = async function (event, context) {
|
|
|
|
const { url } = event.queryStringParameters;
|
|
|
|
|
2023-07-07 21:56:58 +02:00
|
|
|
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 => {
|
2023-07-07 21:56:58 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-07-07 21:56:58 +02:00
|
|
|
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;
|
2023-07-07 21:56:58 +02:00
|
|
|
resolve({
|
|
|
|
statusCode: 200,
|
2023-07-08 18:00:49 +02:00
|
|
|
body: JSON.stringify(certWithoutRaw),
|
2023-07-07 21:56:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
});
|
|
|
|
};
|