Created
February 5, 2025 02:23
-
-
Save greenido/1af3eda3596adb1348aa0b62a099f907 to your computer and use it in GitHub Desktop.
Checking the DNS or CloudFlare (both 9.9.9.9 and 1.1.1.3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dns = require('dns'); | |
const { performance } = require('perf_hooks'); | |
// Configure DNS servers | |
const quad9 = { address: '9.9.9.9', name: 'Quad9' }; | |
const cloudflareSecure = { address: '1.1.1.3', name: 'Cloudflare Security' }; | |
// Test URLs | |
const safeUrls = [ | |
'google.com', | |
'microsoft.com', | |
'amazon.com', | |
'github.com', | |
'netflix.com' | |
]; | |
// These are OFFICIAL test domains that should trigger blocks | |
const suspiciousUrls = [ | |
'githb.co', // REAL ungly domain | |
'ck.getcookiestxt.com', // REAL ungly domain | |
'blockedsite.mypdns.cloud', // Known test domain | |
'test.quad9.net', // Quad9 test domain | |
'sim1.testmessaging.com', // Test phishing domain | |
'blocked.cleanweb.malwaredomains.com' // Test malware domain | |
]; | |
async function checkDomain(domain, dnsServer) { | |
return new Promise((resolve) => { | |
const startTime = performance.now(); | |
const resolver = new dns.Resolver(); | |
resolver.setServers([dnsServer.address]); | |
resolver.resolve4(domain, (err, addresses) => { | |
const endTime = performance.now(); | |
const responseTime = (endTime - startTime).toFixed(2); | |
// More detailed error checking | |
let status = 'ALLOWED'; | |
if (err) { | |
if (err.code === 'NXDOMAIN' || err.code === 'SERVFAIL') { | |
status = 'BLOCKED'; | |
} else if (err.code === 'ENOTFOUND') { | |
status = 'DOMAIN NOT FOUND'; | |
} else { | |
status = `ERROR: ${err.code}`; | |
} | |
} | |
resolve({ | |
domain, | |
dnsServer: dnsServer.name, | |
status, | |
responseTime: `${responseTime}ms`, | |
addresses: addresses || [] | |
}); | |
}); | |
}); | |
} | |
async function runTests() { | |
console.log('Starting DNS Security Tests\n'); | |
// Test all URLs against both DNS servers | |
const allUrls = [...safeUrls, ...suspiciousUrls]; | |
const allTests = []; | |
for (const domain of allUrls) { | |
allTests.push(checkDomain(domain, quad9)); | |
allTests.push(checkDomain(domain, cloudflareSecure)); | |
} | |
const results = await Promise.all(allTests); | |
// Process and display results | |
console.log('=== Safe URLs ==='); | |
for (const domain of safeUrls) { | |
const quad9Result = results.find(r => r.domain === domain && r.dnsServer === quad9.name); | |
const cloudflareResult = results.find(r => r.domain === domain && r.dnsServer === cloudflareSecure.name); | |
console.log(`\nDomain: ${domain}`); | |
console.log(`${quad9.name}: ${quad9Result.status} (${quad9Result.responseTime})`); | |
if (quad9Result.addresses.length > 0) console.log(` IP: ${quad9Result.addresses[0]}`); | |
console.log(`${cloudflareSecure.name}: ${cloudflareResult.status} (${cloudflareResult.responseTime})`); | |
if (cloudflareResult.addresses.length > 0) console.log(` IP: ${cloudflareResult.addresses[0]}`); | |
} | |
console.log('\n=== Suspicious URLs ==='); | |
for (const domain of suspiciousUrls) { | |
const quad9Result = results.find(r => r.domain === domain && r.dnsServer === quad9.name); | |
const cloudflareResult = results.find(r => r.domain === domain && r.dnsServer === cloudflareSecure.name); | |
console.log(`\nDomain: ${domain}`); | |
console.log(`${quad9.name}: ${quad9Result.status} (${quad9Result.responseTime})`); | |
if (quad9Result.addresses.length > 0) console.log(` IP: ${quad9Result.addresses[0]}`); | |
console.log(`${cloudflareSecure.name}: ${cloudflareResult.status} (${cloudflareResult.responseTime})`); | |
if (cloudflareResult.addresses.length > 0) console.log(` IP: ${cloudflareResult.addresses[0]}`); | |
} | |
// Calculate and display statistics | |
const quad9Times = results | |
.filter(r => r.dnsServer === quad9.name) | |
.map(r => parseFloat(r.responseTime)); | |
const cloudflareTimes = results | |
.filter(r => r.dnsServer === cloudflareSecure.name) | |
.map(r => parseFloat(r.responseTime)); | |
const avgQuad9Time = (quad9Times.reduce((a, b) => a + b, 0) / quad9Times.length).toFixed(2); | |
const avgCloudflareTime = (cloudflareTimes.reduce((a, b) => a + b, 0) / cloudflareTimes.length).toFixed(2); | |
console.log('\n=== Performance Summary ==='); | |
console.log(`${quad9.name} average response time: ${avgQuad9Time}ms`); | |
console.log(`${cloudflareSecure.name} average response time: ${avgCloudflareTime}ms`); | |
} | |
// Run the tests | |
runTests().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment