import type { ProxyData, WhoIsOptions, WhoIsResponse } from './types' import Net from 'node:net' import { log } from '@stacksjs/logging' import { IANA_CHK_URL, PARAMETERS, SERVERS } from './constants' import { SocksClient } from './socks' import type { SocksClientOptions } from './socks' import { ProxyType } from './types' import { shallowCopy } from './utils' /** * Find the WhoIs server for the TLD from IANA WhoIs service. The TLD is be searched and the HTML response is parsed to extract the WhoIs server * * @param tld TLD of the domain * @returns WhoIs server which hosts the information for the domains of the TLD */ export async function findWhoIsServer(tld: string): Promise { const chkURL = IANA_CHK_URL + tld try { const res = await fetch(chkURL) if (res.ok) return parseIanaWhoisServer(await res.text()) } catch (err) { console.error('Error in getting WhoIs server data from IANA', err) } return '' } /** * The whois server named in an IANA TLD record, or `''` when there is none. * * Split out of {@link findWhoIsServer} so the parsing can be tested without * reaching IANA over the network. It was inlined, so the only coverage it had * was a test that made a live request - which turned this repository's CI red * whenever IANA was slow, with `Expected: > 0` as the whole explanation. * * IANA's response is a plain-text record with one `field: value` per line. The * pattern is anchored to the start of a line: `whois:` also appears inside the * prose of some records ("...contact the whois: server..."), and an unanchored * match picked the first of those instead of the field. * * @example * ```ts * parseIanaWhoisServer('domain: app\nwhois: whois.nic.google\n') // 'whois.nic.google' * parseIanaWhoisServer('domain: example\n') // '' * ``` */ export function parseIanaWhoisServer(body: string): string { // Spaces and tabs only between the field and its value. A TLD with no whois // server (RDAP only, like .dev) has an empty `whois:` line, and `\s+` ran // over the line break into the next field, so it answered "status:". return body.match(/^whois:[ \t]+(\S+)/m)?.[1] ?? '' } /** * Get whois server of the tld from servers list * * @param tld TLD of the domain * @returns WhoIs server which hosts the information for the domains of the TLD */ export function getWhoIsServer(tld: keyof typeof SERVERS): string | undefined { if (tld === 'com') return 'whois.verisign-grs.com' return SERVERS[tld] } /** * Extract TLD from domain name. * If the TLD is in whois-servers.json file, then the TLD is returned. * If TLD is not found within the file, then determined by taking the last element after splitting the domain name from '.' * * @param domain Domain name * @returns TLD */ export function getTLD(domain: string): keyof typeof SERVERS { const domainParts = domain.split('.') for (let i = domainParts.length - 1; i > 0; i--) { const possibleTLD = domainParts.slice(i).join('.') as keyof typeof SERVERS if (SERVERS[possibleTLD]) { return possibleTLD } } // If no match found in SERVERS, return the last part return domainParts[domainParts.length - 1] as keyof typeof SERVERS } // get whois query parameters if exist on parameters.json for whois server export function getParameters(server: string): string | undefined { return (PARAMETERS as { [key: string]: string })[server] } /** * Parse collected raw WhoIs data * * @class */ export class WhoIsParser { /** * Iterated through the complete text and returns extracted values * * @param rawData raw text from WhoIs server * @param outputData Data which needs to be extracted from the raw text (key/value pairs). Keys are used to extract from raw text and values are filled. * @returns Filled {@link outputData} */ private static iterParse(rawData: string, outputData: any) { const assign = (field: string, value: string): void => { const current = outputData[field] if (Array.isArray(current)) current.push(value) else outputData[field] = value } // A key whose value is on the following line (`Registrar:` with the name // under it), as some registries print it. let pending: string | null = null // A line at a time, the field being everything before its first colon and // the value everything after. It used to read a character at a time and // drop every colon it met, so `2024-08-14T07:01:34Z` came back as // `2024-08-14T070134Z`, which no date parser reads, and a status's // `https://icann.org/...` as `https//icann.org/...`. for (const rawLine of rawData.split(/\r?\n/)) { const line = rawLine.trim() if (line.startsWith('>>>') || line.startsWith('Record maintained by')) break if (!line) continue const colon = line.indexOf(':') const field = colon === -1 ? null : line.slice(0, colon).trim() if (field !== null && field in outputData) { const value = line.slice(colon + 1).trim() if (value) { assign(field, value) pending = null } else { pending = field } continue } if (pending !== null) { assign(pending, line) pending = null } } return outputData } /** * Parse the raw WhoIs text and returns extracted values * * @param rawData raw text from WhoIs server * @param outputData Data which needs to be extracted from the raw text (key/value pairs). Keys are used to extract from raw text and values are filled. * @returns Filled {@link outputData} */ public static parseData(rawData: string, outputData: any | null): any { if (!outputData) { outputData = { 'Domain Name': '', 'Creation Date': '', 'Updated Date': '', 'Registry Expiry Date': '', 'Domain Status': [], 'Registrar': '', } } outputData = WhoIsParser.iterParse(rawData, outputData) return outputData } } /** * Connects to the provided {@link server}:{@link port} through TCP (through a proxy if a proxy is given), run the WhoIs query and returns the response * * @param domain Domain name * @param queryOptions Query options which can be used with the specific WhoIs server to get the complete response * @param server WhoIs server * @param port WhoIs server port * @param encoding Encoding used by the WhoIs server * @param proxy {@link ProxyData} * @returns The {string} WhoIs response for the query. Empty string is returned for errors */ export async function tcpWhois( domain: string, queryOptions: string, server: string, port: number, encoding: string, proxy: ProxyData | null, ): Promise { const decoder = new TextDecoder(encoding) const encoder = new TextEncoder() if (!proxy) { const socket = new Net.Socket() // 30s socket timeout — without this, an unresponsive WHOIS server // (or a network partition) leaves the lookup hanging indefinitely // and any caller awaiting it freezes alongside it. socket.setTimeout(30_000) return new Promise((resolve, reject) => { try { socket.connect({ port, host: server }, () => { if (queryOptions !== '') socket.write(encoder.encode(`${queryOptions} ${domain}\r\n`)) else socket.write(encoder.encode(`${domain}\r\n`)) }) socket.on('data', (data) => { // A socket's `data` is `string | Buffer` depending on whether an // encoding was set; only the buffer needs decoding. resolve(typeof data === 'string' ? data : decoder.decode(new Uint8Array(data.buffer, data.byteOffset, data.byteLength))) }) socket.on('error', (error) => { reject(error) }) socket.on('timeout', () => { socket.destroy() reject(new Error(`WHOIS request to ${server}:${port} timed out after 30s`)) }) } catch (e) { reject(e) } }) } const options: SocksClientOptions = { proxy: { host: proxy.ip, port: proxy.port, type: proxy.type === ProxyType.SOCKS5 ? 5 : 4, }, command: 'connect', destination: { host: server, port, }, } if (proxy.username && proxy.password) { options.proxy.userId = proxy.username options.proxy.password = proxy.password } return new Promise((resolve, reject) => { SocksClient.createConnection(options, (err: any, info: any) => { if (err) { reject(err) } else { if (!info) reject(new Error('No socket info received!')) if (queryOptions !== '') { info?.socket.write(encoder.encode(`${queryOptions} ${domain}\r\n`)) } else { info?.socket.write(encoder.encode(`${domain}\r\n`)) } info?.socket.on('data', (data: any) => { resolve(decoder.decode(data)) }) info?.socket.resume() } }) }) } /** * Collect WhoIs data for the mentioned {@link domain}. Parse the received response if {@link parse} is true, accordingly. * * @param domain Domain name * @param parse Whether the raw text needs to be parsed/formatted or not * @param options {@link WhoIsOptions} * @returns {@link WhoIsResponse} Returns a {@link WhoIsResponse} object which contains the raw text and parsed data (if parse is true) */ export async function whois( domain: string, parse = false, options: WhoIsOptions | null = null, ): Promise { let tld: string let port = 43 let server = '' let proxy: ProxyData | null let encoding = 'utf-8' if (!options) { tld = getTLD(domain) proxy = null } else { tld = options.tld ? options.tld : getTLD(domain) encoding = options.encoding ? options.encoding : 'utf-8' proxy = options.proxy ? options.proxy : null server = options.server ? options.server : '' port = options.serverPort ? options.serverPort : 43 } if (server === '') { let serverData = getWhoIsServer(tld as keyof typeof SERVERS) if (!serverData) { log.debug(`No WhoIs server found for TLD: ${tld}! Attempting IANA WhoIs database for server!`) serverData = await findWhoIsServer(tld) if (!serverData) { log.debug('WhoIs server could not be found!') return { _raw: '', parsedData: null, } } log.debug(`WhoIs sever found for ${tld}: ${server}`) } server = serverData } const qOptions = getParameters(server) const queryOptions = qOptions || '' try { log.debug(`Attempting WHOIS lookup for ${domain} on server ${server}`) const rawData = await tcpWhois(domain, queryOptions, server, port, encoding, proxy) log.debug(`Raw WHOIS data received:`, rawData) if (!parse) { const parsedData = WhoIsParser.parseData(rawData, null) return { _raw: rawData, parsedData, } } let outputData: any | null = null if (options?.parseData) outputData = shallowCopy(options.parseData) try { const parsedData = WhoIsParser.parseData(rawData, outputData) return { _raw: rawData, parsedData, } } catch (err) { console.error('Error in parsing WhoIs data!', err) return { _raw: rawData, parsedData: null, } } } catch (err: any) { log.debug(`Error in WHOIS lookup for ${domain} on server ${server}`, err) return { _raw: '', parsedData: null, } } } export function lookup(domain: string, options: WhoIsOptions | null = null): Promise { log.debug(`Lookup called for ${domain}`) return whois(domain, true, options) } /** * Collects (and parse/format if set to be true) for the provided {@link domains}. If {@link parallel} is set to be true, multiple threads will be used to batch process the domains according to {@link threads} mentioned. * If options.parsedData is mentioned, then it will be used to parse all the responses. * If a proxy is mentioned in {@link options}, then the proxy will be used to collect all the WhoIs data. * * @param domains Domains Names * @param parallel Whether data should be collected parallally or not * @param threads Batch size (for parallel processing) * @param parse Whether the raw text needs to be parsed/formatted or not * @param options {@link WhoIsOptions} * @returns Array of {@link WhoIsResponse} for all the domains. Order is not guaranteed */ export async function batchWhois( domains: string[], parallel = false, threads = 1, parse = false, options: WhoIsOptions | null = null, ): Promise { let response: WhoIsResponse[] = [] if (parallel) { if (threads > domains.length) threads = domains.length for (let i = 0; i < domains.length; i += threads) { const batch = domains.slice(i, i + threads) // Accumulate batch results — the previous `response = await Promise.all(...)` // overwrote the array each iteration, so a 100-domain batch with // threads=10 returned only the last 10 entries instead of all 100. const batchResults = await Promise.all( batch.map(async (domain) => { return await whois(domain, parse, options) }), ) response.push(...batchResults) } } else { for (let i = 0; i < domains.length; i++) { const res = await whois(domains[i] as string, parse, options) response.push(res) } } return response } export * from './constants' export * from './types' export { SocksClient } from './socks' export type { SocksClientOptions } from './socks'