Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Get WHOIS info for domains.
- `options` - Object of options to use, all optional:
- `host` - WHOIS server to query. Default: WHOIS server from IANA
- `timeout` - WHOIS server request timeout in ms. Default: 1500
- `encoding` - Encoding used to decode the WHOIS response
- `follow` - How many WHOIS server to query. 1 = registry server (faster), 2 = registry + registrar (more domain details). Default: 2
- `raw` - Return the raw WHOIS result in response. Added to `__raw`
- `ignorePrivacy` - Show or hide the WHOIS protected data from response, accepts boolean. Default: true
Expand Down Expand Up @@ -118,6 +119,7 @@ Get WHOIS info for IPs
- `options` - Object of options to use, all optional:
- `host` - WHOIS server to query. Default: WHOIS server from IANA
- `timeout` - WHOIS server request timeout in ms. Default: 1500
- `encoding` - Encoding used to decode the WHOIS response
- `raw` - Return the raw WHOIS result in response. Added to `__raw`

```js
Expand Down Expand Up @@ -160,6 +162,7 @@ Get WHOIS info for an AS number
- `options` - Object of options to use, all optional:
- `host` - WHOIS server to query. Default: WHOIS server from IANA
- `timeout` - WHOIS server request timeout in ms. Default: 1500
- `encoding` - Encoding used to decode the WHOIS response
- `raw` - Return the raw WHOIS result in response. Added to `__raw`

```js
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export interface TldWhoisResponse {
export interface DomainWhoisOptions {
host?: string
timeout?: number
encoding?: BufferEncoding
follow?: 1 | 2
raw?: boolean
ignorePrivacy?: boolean
whoisQuery?: (host: string, query: string, timeout?: number) => Promise<string>
whoisQuery?: (host: string, query: string, timeout?: number, encoding?: BufferEncoding) => Promise<string>
}
14 changes: 8 additions & 6 deletions src/whoiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ const misspelledWhoisServer = {
* @param host WHOIS server hostname
* @param query Query string
* @param timeout Timeout duration in milliseconds
* @param encoding Encoding used to decode the response, for servers that don't reply in UTF-8. Example: 'latin1'. Default: UTF-8
* @returns Result of the WHOIS query
*/
export function whoisQuery(host: string, query: string, timeout: number = 5000): Promise<string> {
export function whoisQuery(host: string, query: string, timeout: number = 5000, encoding?: BufferEncoding): Promise<string> {
return new Promise((resolve, reject) => {
let data = ''
const socket = net.connect({ host, port: 43, family: 4 }, () => socket.write(query + '\r\n'))
socket.setTimeout(timeout)
if (encoding) socket.setEncoding(encoding)
socket.on('data', (chunk) => (data += chunk))
socket.on('close', () => resolve(data))
socket.on('timeout', () => socket.destroy(new Error('Timeout')))
Expand Down Expand Up @@ -197,7 +199,7 @@ export async function whoisDomain(domain: string, options?: DomainWhoisOptions):
}

try {
resultRaw = await queryFn(host, query, options?.timeout)
resultRaw = await queryFn(host, query, options?.timeout, options?.encoding)
result = parseDomainWhois(domain, resultRaw, options?.ignorePrivacy ?? true)
} catch (err) {
result = { error: err.message }
Expand Down Expand Up @@ -263,7 +265,7 @@ async function findWhoisServerInIana(query: string) {
* @returns Normalized WHOIS data
* @throws Error if IP is invalid or not found
*/
export async function whoisIp(ip: string, options: { host?: string; timeout?: number } = {}): Promise<WhoisData> {
export async function whoisIp(ip: string, options: { host?: string; timeout?: number; encoding?: BufferEncoding } = {}): Promise<WhoisData> {
if (!net.isIP(ip)) {
throw new Error(`Invalid IP address "${ip}"`)
}
Expand All @@ -281,7 +283,7 @@ export async function whoisIp(ip: string, options: { host?: string; timeout?: nu
modifiedQuery = `+ n ${ip}`
}

const ipWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000)
const ipWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000, options.encoding)

return parseSimpleWhois(ipWhoisResult)
}
Expand All @@ -293,7 +295,7 @@ export async function whoisIp(ip: string, options: { host?: string; timeout?: nu
* @returns Normalized WHOIS data
* @throws Error if ASN is invalid or not found
*/
export async function whoisAsn(asn: number, options: { host?: string; timeout?: number } = {}): Promise<WhoisData> {
export async function whoisAsn(asn: number, options: { host?: string; timeout?: number; encoding?: BufferEncoding } = {}): Promise<WhoisData> {
if (asn < 0 || asn > 4294967295) {
throw new Error(`Invalid ASN number "${asn}"`)
}
Expand All @@ -312,7 +314,7 @@ export async function whoisAsn(asn: number, options: { host?: string; timeout?:
modifiedQuery = `+ a ${asn}`
}

const asnWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000)
const asnWhoisResult = await whoisQuery(host, modifiedQuery, options.timeout || 1000, options.encoding)

return parseSimpleWhois(asnWhoisResult)
}
Expand Down