1
mirror of https://github.com/jakejarvis/rdapper.git synced 2025-10-18 14:24:29 -04:00

Enhance WHOIS fallback error handling and add IANA WHOIS parsing utilities

Updated the error messages in the lookupDomain function to provide clearer guidance when RDAP is unavailable or when no WHOIS server is discovered for a TLD. Introduced new utility functions to parse IANA WHOIS responses, improving the discovery process for WHOIS servers and registration information URLs.
This commit is contained in:
2025-09-26 11:48:02 -04:00
parent e873fb7629
commit 5bc6debe30
3 changed files with 73 additions and 10 deletions

View File

@@ -1,6 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
"extends": ["config:recommended"]
}

View File

@@ -8,6 +8,8 @@ import { whoisQuery } from "./whois/client.js";
import {
extractWhoisReferral,
ianaWhoisServerForTld,
getIanaWhoisTextForTld,
parseIanaRegistrationInfoUrl,
} from "./whois/discovery.js";
import { normalizeWhois } from "./whois/normalize.js";
import { WHOIS_TLD_EXCEPTIONS } from "./whois/servers.js";
@@ -54,7 +56,7 @@ export async function lookupDomain(
if (opts?.rdapOnly) {
return {
ok: false,
error: "RDAP not available or failed for this TLD",
error: `RDAP not available or failed for TLD '${tld}'. Many TLDs do not publish RDAP; try WHOIS fallback (omit rdapOnly).`,
};
}
}
@@ -62,7 +64,18 @@ export async function lookupDomain(
// WHOIS fallback path
const whoisServer = await ianaWhoisServerForTld(tld, opts);
if (!whoisServer) {
return { ok: false, error: "No WHOIS server discovered for TLD" };
// Provide a clearer, actionable message
const ianaText = await getIanaWhoisTextForTld(tld, opts);
const regUrl = ianaText
? parseIanaRegistrationInfoUrl(ianaText)
: undefined;
const hint = regUrl
? ` See registration info at ${regUrl}.`
: "";
return {
ok: false,
error: `No WHOIS server discovered for TLD '${tld}'. This registry may not publish public WHOIS over port 43.${hint}`,
};
}
// Query the TLD server first; if it returns a referral, we follow it below.
let res = await whoisQuery(whoisServer, domain, opts);

View File

@@ -2,6 +2,62 @@ import type { LookupOptions } from "../types.js";
import { whoisQuery } from "./client.js";
import { WHOIS_TLD_EXCEPTIONS } from "./servers.js";
/**
* Parse the IANA WHOIS response for a TLD and extract the WHOIS server
* without crossing line boundaries. Some TLDs (e.g. .np) leave the field
* blank, in which case this returns undefined.
*/
export function parseIanaWhoisServer(text: string): string | undefined {
// Search lines in priority order: whois, refer, whois server
const fields = ["whois", "refer", "whois server"];
const lines = String(text).split(/\r?\n/);
for (const field of fields) {
for (const raw of lines) {
const line = raw.trimEnd();
// Match beginning of line, allowing leading spaces, case-insensitive
const re = new RegExp(`^\\s*${field}\\s*:\\s*(.*?)$`, "i");
const m = line.match(re);
if (m) {
const value = (m[1] || "").trim();
if (value) return value;
}
}
}
return undefined;
}
/**
* Parse a likely registration information URL from an IANA WHOIS response.
* Looks at lines like:
* remarks: Registration information: http://example.tld
* url: https://registry.example
*/
export function parseIanaRegistrationInfoUrl(
text: string,
): string | undefined {
const lines = String(text).split(/\r?\n/);
for (const raw of lines) {
const line = raw.trim();
if (!/^\s*(remarks|url|website)\s*:/i.test(line)) continue;
const urlMatch = line.match(/https?:\/\/\S+/i);
if (urlMatch?.[0]) return urlMatch[0];
}
return undefined;
}
/** Fetch raw IANA WHOIS text for a TLD (best-effort). */
export async function getIanaWhoisTextForTld(
tld: string,
options?: LookupOptions,
): Promise<string | undefined> {
try {
const res = await whoisQuery("whois.iana.org", tld.toLowerCase(), options);
return res.text;
} catch {
return undefined;
}
}
/**
* Best-effort discovery of the authoritative WHOIS server for a TLD via IANA root DB.
*/
@@ -18,11 +74,7 @@ export async function ianaWhoisServerForTld(
try {
const res = await whoisQuery("whois.iana.org", key, options);
const txt = res.text;
const m =
txt.match(/^whois:\s*(\S+)/im) ||
txt.match(/^refer:\s*(\S+)/im) ||
txt.match(/^whois server:\s*(\S+)/im);
const server = m?.[1];
const server = parseIanaWhoisServer(txt);
if (server) return normalizeServer(server);
} catch {
// fallthrough to exceptions/guess