web-check/api/mail-config.js

83 lines
3.1 KiB
JavaScript
Raw Normal View History

import dns from 'dns';
import URL from 'url-parse';
import middleware from './_common/middleware.js';
2023-08-10 15:27:06 +02:00
2024-05-18 16:01:52 +02:00
// TODO: Fix.
const mailConfigHandler = async (url, event, context) => {
2023-07-29 20:18:10 +02:00
try {
2023-08-10 15:27:06 +02:00
const domain = new URL(url).hostname || new URL(url).pathname;
2023-07-29 20:18:10 +02:00
// Get MX records
const mxRecords = await dns.resolveMx(domain);
// Get TXT records
const txtRecords = await dns.resolveTxt(domain);
// Filter for only email related TXT records (SPF, DKIM, DMARC, and certain provider verifications)
const emailTxtRecords = txtRecords.filter(record => {
const recordString = record.join('');
return (
recordString.startsWith('v=spf1') ||
recordString.startsWith('v=DKIM1') ||
recordString.startsWith('v=DMARC1') ||
recordString.startsWith('protonmail-verification=') ||
recordString.startsWith('google-site-verification=') || // Google Workspace
recordString.startsWith('MS=') || // Microsoft 365
recordString.startsWith('zoho-verification=') || // Zoho
recordString.startsWith('titan-verification=') || // Titan
recordString.includes('bluehost.com') // BlueHost
);
});
// Identify specific mail services
const mailServices = emailTxtRecords.map(record => {
const recordString = record.join('');
if (recordString.startsWith('protonmail-verification=')) {
return { provider: 'ProtonMail', value: recordString.split('=')[1] };
} else if (recordString.startsWith('google-site-verification=')) {
return { provider: 'Google Workspace', value: recordString.split('=')[1] };
} else if (recordString.startsWith('MS=')) {
return { provider: 'Microsoft 365', value: recordString.split('=')[1] };
} else if (recordString.startsWith('zoho-verification=')) {
return { provider: 'Zoho', value: recordString.split('=')[1] };
} else if (recordString.startsWith('titan-verification=')) {
return { provider: 'Titan', value: recordString.split('=')[1] };
} else if (recordString.includes('bluehost.com')) {
return { provider: 'BlueHost', value: recordString };
} else {
return null;
}
}).filter(record => record !== null);
// Check MX records for Yahoo
const yahooMx = mxRecords.filter(record => record.exchange.includes('yahoodns.net'));
if (yahooMx.length > 0) {
mailServices.push({ provider: 'Yahoo', value: yahooMx[0].exchange });
}
2024-03-09 13:49:38 +01:00
// Check MX records for Mimecast
const mimecastMx = mxRecords.filter(record => record.exchange.includes('mimecast.com'));
if (mimecastMx.length > 0) {
mailServices.push({ provider: 'Mimecast', value: mimecastMx[0].exchange });
}
2023-07-29 20:18:10 +02:00
return {
mxRecords,
txtRecords: emailTxtRecords,
mailServices,
2023-08-10 15:27:06 +02:00
};
2023-07-29 20:18:10 +02:00
} catch (error) {
if (error.code === 'ENOTFOUND' || error.code === 'ENODATA') {
2023-08-10 15:27:06 +02:00
return { skipped: 'No mail server in use on this domain' };
2023-07-29 20:18:10 +02:00
} else {
return {
statusCode: 500,
2024-05-18 16:01:52 +02:00
body: { error: error.message },
2023-07-29 20:18:10 +02:00
};
}
}
};
2023-08-10 15:27:06 +02:00
export const handler = middleware(mailConfigHandler);
export default handler;