2022-07-09 21:29:36 +02:00
|
|
|
const dns = require('dns');
|
|
|
|
|
|
|
|
/* Lambda function to fetch the IP address of a given URL */
|
|
|
|
exports.handler = function (event, context, callback) {
|
2023-07-10 00:23:50 +02:00
|
|
|
const addressParam = event.queryStringParameters.url;
|
|
|
|
|
|
|
|
if (!addressParam) {
|
|
|
|
callback(null, errorResponse('Address parameter is missing.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-09 21:29:36 +02:00
|
|
|
const address = decodeURIComponent(addressParam)
|
2023-07-10 00:23:50 +02:00
|
|
|
.replaceAll('https://', '')
|
|
|
|
.replaceAll('http://', '');
|
|
|
|
|
2022-07-09 21:29:36 +02:00
|
|
|
dns.lookup(address, (err, ip, family) => {
|
|
|
|
if (err) {
|
2023-07-10 00:23:50 +02:00
|
|
|
callback(null, errorResponse(err.message));
|
2022-07-09 21:29:36 +02:00
|
|
|
} else {
|
2023-07-10 00:23:50 +02:00
|
|
|
callback(null, {
|
2022-07-09 21:29:36 +02:00
|
|
|
statusCode: 200,
|
2023-07-10 00:23:50 +02:00
|
|
|
body: JSON.stringify({ ip, family }),
|
2022-07-09 21:29:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2023-07-10 00:23:50 +02:00
|
|
|
|
|
|
|
const errorResponse = (message, statusCode = 444) => {
|
|
|
|
return {
|
|
|
|
statusCode: statusCode,
|
|
|
|
body: JSON.stringify({ error: message }),
|
|
|
|
};
|
|
|
|
};
|