From e1a6f3682c91ba05aa60fde75f49475bd905d9e1 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 5 Jan 2025 21:57:49 +0800 Subject: [PATCH] Feat: improve domain alive checking by logging whois --- Build/lib/is-domain-alive.test.ts | 32 +++++++------------------------ Build/lib/is-domain-alive.ts | 29 ++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/Build/lib/is-domain-alive.test.ts b/Build/lib/is-domain-alive.test.ts index f597148c7..5fd819df5 100644 --- a/Build/lib/is-domain-alive.test.ts +++ b/Build/lib/is-domain-alive.test.ts @@ -1,13 +1,13 @@ import { describe, it } from 'mocha'; -import { isDomainAlive, whoisExists } from './is-domain-alive'; +import { isDomainAlive, noWhois } from './is-domain-alive'; import { expect } from 'expect'; import process from 'node:process'; describe('whoisExists', () => { it('.cryptocrawler.io', () => { - expect(whoisExists({ + expect(noWhois({ 'whois.nic.io': { 'Domain Status': [], 'Name Server': [], @@ -18,11 +18,11 @@ describe('whoisExists', () => { 'Terms of Use: Access to WHOIS' ] } - })).toBe(false); + })).toBe('Domain not found.'); }); it('.tunevideo.ru', () => { - expect(whoisExists({ + expect(noWhois({ 'whois.tcinet.ru': { 'Domain Status': [], 'Name Server': [], @@ -36,25 +36,7 @@ describe('whoisExists', () => { 'Last updated on 2025-01-05T11:03:01Z' ] } - })).toBe(false); - }); - - it('.myqloud.com', () => { - expect(whoisExists({ - 'whois.tcinet.ru': { - 'Domain Status': [], - 'Name Server': [], - text: [ - '% TCI Whois Service. Terms of use:', - '% https://tcinet.ru/documents/whois_ru_rf.pdf (in Russian)', - '% https://tcinet.ru/documents/whois_su.pdf (in Russian)', - '', - 'No entries found for the selected source(s).', - '', - 'Last updated on 2025-01-05T11:03:01Z' - ] - } - })).toBe(false); + })).toBe('No entries found for the selected source(s).'); }); }); @@ -93,8 +75,8 @@ describe('isDomainAlive', function () { // expect((await isDomainAlive('.tayfundogdas.me', true))[1]).toEqual(true); // }); - it('hamdandates.com', async () => { + it('9s6q.cn', async () => { process.env.DEBUG = 'true'; - expect((await isDomainAlive('.hamdandates.com', true))[1]).toEqual(false); + expect((await isDomainAlive('.9s6q.cn', true))[1]).toEqual(false); }); }); diff --git a/Build/lib/is-domain-alive.ts b/Build/lib/is-domain-alive.ts index 208ca5129..a3bc7455c 100644 --- a/Build/lib/is-domain-alive.ts +++ b/Build/lib/is-domain-alive.ts @@ -218,12 +218,13 @@ async function isApexDomainAlive(apexDomain: string): Promise<[string, boolean]> console.log(JSON.stringify(whois, null, 2)); } - if (whoisExists(whois)) { + const whoisError = noWhois(whois); + if (!whoisError) { console.log(picocolors.gray('[domain alive]'), picocolors.gray('whois found'), { domain: apexDomain }); return onDomainAlive(apexDomain); } - console.log(picocolors.red('[domain dead]'), 'whois not found', { domain: apexDomain }); + console.log(picocolors.red('[domain dead]'), 'whois not found', { domain: apexDomain, whoisError }); return onDomainDead(apexDomain); } @@ -243,7 +244,7 @@ const whoisNotFoundKeywordTest = createKeywordFilter([ // whois server can redirect, so whoiser might/will get info from multiple whois servers // some servers (like TLD whois servers) might have cached/outdated results // we can only make sure a domain is alive once all response from all whois servers demonstrate so -export function whoisExists(whois: whoiser.WhoisSearchResult) { +export function noWhois(whois: whoiser.WhoisSearchResult): null | string { let empty = true; for (const key in whois) { @@ -262,8 +263,12 @@ export function whoisExists(whois: whoiser.WhoisSearchResult) { } if (key === 'text') { - if (Array.isArray(whois.text) && whois.text.some(value => whoisNotFoundKeywordTest(value.toLowerCase()))) { - return false; + if (Array.isArray(whois.text)) { + for (const value of whois.text) { + if (whoisNotFoundKeywordTest(value.toLowerCase())) { + return value; + } + } } continue; } @@ -274,11 +279,19 @@ export function whoisExists(whois: whoiser.WhoisSearchResult) { continue; } - if (typeof whois[key] === 'object' && !Array.isArray(whois[key]) && !whoisExists(whois[key])) { - return false; + if (typeof whois[key] === 'object' && !Array.isArray(whois[key])) { + const tmp = noWhois(whois[key]); + if (tmp) { + return tmp; + } + continue; } } } - return !empty; + if (empty) { + return 'whois is empty'; + } + + return null; }