2023-06-21 00:01:45 +02:00
|
|
|
const https = require('https');
|
|
|
|
|
|
|
|
exports.handler = async function (event, context) {
|
|
|
|
const { url } = event.queryStringParameters;
|
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
const errorResponse = (message, statusCode = 500) => {
|
2023-07-07 21:56:58 +02:00
|
|
|
return {
|
|
|
|
statusCode: statusCode,
|
|
|
|
body: JSON.stringify({ error: message }),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-21 00:01:45 +02:00
|
|
|
if (!url) {
|
2023-07-10 00:23:50 +02:00
|
|
|
return errorResponse('URL query parameter is required', 400);
|
2023-06-21 00:01:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
try {
|
|
|
|
const response = await new Promise((resolve, reject) => {
|
|
|
|
const req = https.request(url, res => {
|
2023-07-07 21:56:58 +02:00
|
|
|
|
2023-07-10 00:23:50 +02:00
|
|
|
// Check if the SSL handshake was authorized
|
|
|
|
if (!res.socket.authorized) {
|
|
|
|
resolve(errorResponse(`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) {
|
|
|
|
resolve(errorResponse("No certificate presented by the server."));
|
|
|
|
} else {
|
|
|
|
// omit the raw and issuerCertificate fields
|
|
|
|
const { raw, issuerCertificate, ...certWithoutRaw } = cert;
|
|
|
|
resolve({
|
|
|
|
statusCode: 200,
|
|
|
|
body: JSON.stringify(certWithoutRaw),
|
|
|
|
});
|
|
|
|
}
|
2023-07-07 21:56:58 +02:00
|
|
|
}
|
2023-07-10 00:23:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', error => {
|
|
|
|
resolve(errorResponse(`Error fetching site certificate: ${error.message}`));
|
|
|
|
});
|
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) {
|
|
|
|
return errorResponse(`Unexpected error occurred: ${error.message}`);
|
|
|
|
}
|
2023-06-21 00:01:45 +02:00
|
|
|
};
|