// DKIM Management functionality
const DKIMManagement = {
// Check DNS records for a domain
checkDomainDNS: async function(domain, selector, checkDkimUrl, checkSpfUrl) {
const dkimStatus = document.getElementById(`dkim-status-${domain.replace('.', '-')}`);
const spfStatus = document.getElementById(`spf-status-${domain.replace('.', '-')}`);
// Show loading state
dkimStatus.innerHTML = 'Checking...';
spfStatus.innerHTML = 'Checking...';
try {
// Check DKIM DNS
const dkimResponse = await fetch(checkDkimUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
domain: domain,
selector: selector
})
});
const dkimResult = await dkimResponse.json();
// Check SPF DNS
const spfResponse = await fetch(checkSpfUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
domain: domain
})
});
const spfResult = await spfResponse.json();
// Get DKIM key status from the card class
const domainCard = document.getElementById(`domain-${domain.replace('.', '-')}`);
const isActive = domainCard && domainCard.classList.contains('dkim-active');
// Update DKIM status based on active state and DNS visibility
if (isActive) {
if (dkimResult.success) {
dkimStatus.innerHTML = '✓ Active & Configured';
} else {
dkimStatus.innerHTML = 'Active but DNS not found';
}
} else {
dkimStatus.innerHTML = 'Disabled';
}
// Update SPF status
if (spfResult.success) {
spfStatus.innerHTML = '✓ Found';
} else {
spfStatus.innerHTML = '✗ Not found';
}
// Show detailed results in modal
this.showDNSResults(domain, dkimResult, spfResult);
} catch (error) {
console.error('DNS check error:', error);
dkimStatus.innerHTML = 'Error';
spfStatus.innerHTML = 'Error';
}
},
// Show DNS check results in modal
showDNSResults: function(domain, dkimResult, spfResult) {
// Clean up record strings by removing extra quotes and normalizing whitespace
function cleanRecordDisplay(record) {
if (!record) return '';
return record
.replace(/^["']|["']$/g, '') // Remove outer quotes
.replace(/\\n/g, '') // Remove newlines
.replace(/\s+/g, ' ') // Normalize whitespace
.trim(); // Remove leading/trailing space
}
const dkimRecordsHtml = dkimResult.records ?
dkimResult.records.map(record =>
`
${cleanRecordDisplay(record)}
`
).join('') : '';
const spfRecordHtml = spfResult.spf_record ?
`
${cleanRecordDisplay(spfResult.spf_record)}
` : '';
const resultsHtml = `
DNS Check Results for ${domain}
DKIM Record
Status: ${dkimResult.success ? 'Found' : 'Not Found'}
Message: ${dkimResult.message}
${dkimResult.records ? `
Records:
${dkimRecordsHtml}
` : ''}
SPF Record
Status: ${spfResult.success ? 'Found' : 'Not Found'}
Message: ${spfResult.message}
${spfResult.spf_record ? `
Current SPF:
${spfRecordHtml}
` : ''}
`;
document.getElementById('dnsResults').innerHTML = resultsHtml;
new bootstrap.Modal(document.getElementById('dnsResultModal')).show();
},
// Check all domains' DNS records
checkAllDNS: async function(checkDkimUrl, checkSpfUrl) {
const domains = document.querySelectorAll('[id^="domain-"]');
const results = [];
// Show a progress indicator
showToast('Checking DNS records for all domains...', 'info');
for (const domainCard of domains) {
try {
const domainId = domainCard.id.split('-')[1];
// Extract domain name from the card header
const domainHeaderText = domainCard.querySelector('h5').textContent.trim();
const domainName = domainHeaderText.split('\n')[0].trim().replace(/^\s*\S+\s+/, ''); // Remove icon
const selectorElement = domainCard.querySelector('code');
if (selectorElement) {
const selector = selectorElement.textContent;
// Check DKIM DNS
const dkimResponse = await fetch(checkDkimUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `domain=${encodeURIComponent(domainName)}&selector=${encodeURIComponent(selector)}`
});
const dkimResult = await dkimResponse.json();
// Check SPF DNS
const spfResponse = await fetch(checkSpfUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `domain=${encodeURIComponent(domainName)}`
});
const spfResult = await spfResponse.json();
results.push({
domain: domainName,
dkim: dkimResult,
spf: spfResult
});
// Update individual status indicators
const dkimStatus = document.getElementById(`dkim-status-${domainName.replace('.', '-')}`);
const spfStatus = document.getElementById(`spf-status-${domainName.replace('.', '-')}`);
if (dkimStatus) {
if (dkimResult.success) {
dkimStatus.innerHTML = '✓ Configured';
} else {
dkimStatus.innerHTML = '✗ Not found';
}
}
if (spfStatus) {
if (spfResult.success) {
spfStatus.innerHTML = '✓ Found';
} else {
spfStatus.innerHTML = '✗ Not found';
}
}
// Small delay between checks to avoid overwhelming the DNS server
await new Promise(resolve => setTimeout(resolve, 300));
}
} catch (error) {
console.error('Error checking DNS for domain:', error);
}
}
// Show combined results in modal
this.showAllDNSResults(results);
},
// Show combined DNS check results
showAllDNSResults: function(results) {
let tableRows = '';
results.forEach(result => {
const dkimIcon = result.dkim.success ? '' : '';
const spfIcon = result.spf.success ? '' : '';
tableRows += `
| ${result.domain} |
${dkimIcon}
${result.dkim.success ? 'Configured' : 'Not Found'}
|
${spfIcon}
${result.spf.success ? 'Found' : 'Not Found'}
|
DKIM: ${result.dkim.message}
SPF: ${result.spf.message}
|
`;
});
const resultsHtml = `
DNS Check Results for All Domains
| Domain |
DKIM Status |
SPF Status |
Details |
${tableRows}
DKIM: Verifies email signatures for authenticity
SPF: Authorizes servers that can send email for your domain
`;
document.getElementById('dnsResults').innerHTML = resultsHtml;
new bootstrap.Modal(document.getElementById('dnsResultModal')).show();
}
};