mirror of
https://github.com/jakejarvis/rdapper.git
synced 2026-09-23 01:25:31 -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:
+57
-5
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user