Makes some functions more robust

This commit is contained in:
Alicia Sykes 2023-08-10 14:27:06 +01:00
parent f96c7ba25f
commit 95b13240c7
3 changed files with 19 additions and 21 deletions

View File

@ -1,4 +1,5 @@
const net = require('net');
const middleware = require('./_common/middleware');
// A list of commonly used ports.
const PORTS = [
@ -12,7 +13,7 @@ async function checkPort(port, domain) {
return new Promise((resolve, reject) => {
const socket = new net.Socket();
socket.setTimeout(1500); // you may want to adjust the timeout
socket.setTimeout(1500);
socket.once('connect', () => {
socket.destroy();
@ -33,13 +34,9 @@ async function checkPort(port, domain) {
});
}
exports.handler = async (event, context) => {
const domain = (event.queryStringParameters || event.query).url;
const handler = async (url, event, context) => {
const domain = url.replace(/(^\w+:|^)\/\//, '');
if (!domain) {
return errorResponse('Missing domain parameter.');
}
const delay = ms => new Promise(res => setTimeout(res, ms));
const timeout = delay(9000);
@ -88,3 +85,5 @@ const errorResponse = (message, statusCode = 444) => {
body: JSON.stringify({ error: message }),
};
};
exports.handler = middleware(handler);

View File

@ -1,11 +1,11 @@
const commonMiddleware = require('./_common/middleware');
const dns = require('dns').promises;
const URL = require('url-parse');
exports.handler = async (event, context) => {
const handler = async (url, event, context) => {
try {
let domain = event.queryStringParameters.url;
const parsedUrl = new URL(domain);
domain = parsedUrl.hostname || parsedUrl.pathname;
const domain = new URL(url).hostname || new URL(url).pathname;
// Get MX records
const mxRecords = await dns.resolveMx(domain);
@ -56,19 +56,13 @@ exports.handler = async (event, context) => {
}
return {
statusCode: 200,
body: JSON.stringify({
mxRecords,
txtRecords: emailTxtRecords,
mailServices,
}),
};
};
} catch (error) {
if (error.code === 'ENOTFOUND' || error.code === 'ENODATA') {
return {
statusCode: 200,
body: JSON.stringify({ skipped: 'No mail server in use on this domain' }),
};
return { skipped: 'No mail server in use on this domain' };
} else {
return {
statusCode: 500,
@ -77,3 +71,5 @@ exports.handler = async (event, context) => {
}
}
};
module.exports.handler = commonMiddleware(handler);

View File

@ -1,8 +1,9 @@
const commonMiddleware = require('./_common/middleware');
const axios = require('axios');
const cheerio = require('cheerio');
exports.handler = async (event, context) => {
let url = event.queryStringParameters.url;
const handler = async (url) => {
// Check if url includes protocol
if (!url.startsWith('http://') && !url.startsWith('https://')) {
@ -66,3 +67,5 @@ exports.handler = async (event, context) => {
};
}
};
exports.handler = commonMiddleware(handler);