Files
smartwhois/ts/index.ts
T

151 lines
4.8 KiB
TypeScript

import * as plugins from './smartwhois.plugins.js';
export interface IWhoisInfo {
domain: string;
isRegistered: boolean;
domainStatus: Array<
| 'clientTransferProhibited'
| 'clientUpdateProhibited'
| 'clientDeleteProhibited'
| 'clientHold'
| 'serverTransferProhibited'
| 'serverUpdateProhibited'
| 'serverDeleteProhibited'
| 'serverHold'
| 'inactive'
| 'pendingDelete'
| 'pendingRenew'
| 'pendingTransfer'
| 'pendingUpdate'
| 'ok'>;
domainWithoutSuffix: string;
hostname: string;
isIcann: boolean;
isIp: boolean;
isPrivate: boolean;
publicSuffix: string;
subdomain: string;
dnsSec: 'signedDelegation' | 'unsigned';
originalWhoisInfo: {
[key: string]: any;
};
whoisServers: string[];
registrar: string;
registrarUrl: string;
createdDate: string;
updatedDate: string;
expiryDate: string;
}
export interface IAdditionalWhoisData {
httpStatus: number;
httpsStatus: number;
httpHeaders: unknown;
httpsHeaders: unknown;
}
export class SmartWhois {
/**
* can be used to prepare an input for the whois command
*/
public async getDomainDelegation(urlArg: string): Promise<plugins.tsclass.network.IDomainDelegation> {
if (!urlArg.includes('//')) {
urlArg = `https://${urlArg}`;
}
const smarturlInstance = plugins.smarturl.Smarturl.createFromUrl(urlArg);
const tldtsData = plugins.tldts.parse(urlArg);
return {
fullUrl: smarturlInstance.toString(),
fullDomain: smarturlInstance.hostname,
domain: tldtsData.domain ?? '',
publicSuffix: tldtsData.publicSuffix ?? '',
subdomain: tldtsData.subdomain ?? '',
domainWithoutSuffix: tldtsData.domainWithoutSuffix ?? '',
isIcann: tldtsData.isIcann ?? undefined,
};
}
public async getAdditionalWhoisDataForDomain(domainArg: string): Promise<IAdditionalWhoisData> {
if (domainArg.includes('//')) {
const parsedUrlResult = await this.getDomainDelegation(domainArg);
domainArg = parsedUrlResult.fullDomain;
}
// lets test http response
const httpResult = await plugins.smartrequest.SmartRequest.create()
.url(`http://${domainArg}/`)
.get();
// lets test https response
const httpsResult = await plugins.smartrequest.SmartRequest.create()
.url(`https://${domainArg}/`)
.get();
return {
httpStatus: httpResult.status,
httpsStatus: httpsResult.status,
httpHeaders: httpResult.headers,
httpsHeaders: httpsResult.headers,
};
}
public async getWhoisForDomain(domainArg: string): Promise<IWhoisInfo> {
if (domainArg.includes('//')) {
const parsedUrlResult = await this.getDomainDelegation(domainArg);
domainArg = parsedUrlResult.fullDomain;
}
const whoisInfo = await plugins.whoiser.domain(domainArg);
const whoisServers = Object.keys(whoisInfo);
const selectedWhoisInfo = whoisInfo[whoisServers[0]] as Record<string, unknown>;
const getStringField = (fieldName: string): string => {
const fieldValue = selectedWhoisInfo[fieldName];
return typeof fieldValue === 'string' ? fieldValue : '';
};
const registrar = getStringField('Registrar');
const registrarUrl = getStringField('Registrar URL');
const createdDate = getStringField('Created Date');
const updatedDate = getStringField('Updated Date');
const expiryDate = getStringField('Expiry Date');
const domainStatusRaw = selectedWhoisInfo['Domain Status'];
const domainStatusSource = Array.isArray(domainStatusRaw)
? domainStatusRaw
: typeof domainStatusRaw === 'string'
? [domainStatusRaw]
: [];
const domainStatus = domainStatusSource.map((statusArg) => String(statusArg).split(' ')[0]) as IWhoisInfo['domainStatus'];
const tldtsData = plugins.tldts.parse(domainArg);
return {
domain: tldtsData.domain ?? '',
domainWithoutSuffix: tldtsData.domainWithoutSuffix ?? '',
hostname: tldtsData.hostname ?? '',
isIcann: tldtsData.isIcann ?? false,
isIp: tldtsData.isIp ?? false,
isPrivate: tldtsData.isPrivate ?? false,
publicSuffix: tldtsData.publicSuffix ?? '',
subdomain: tldtsData.subdomain ?? '',
isRegistered: true,
domainStatus,
dnsSec: getStringField('DNSSEC') as IWhoisInfo['dnsSec'],
originalWhoisInfo: whoisInfo as Record<string, unknown>,
whoisServers,
registrar,
registrarUrl,
createdDate,
updatedDate,
expiryDate,
};
}
public async isValidTld(tldArg: string): Promise<boolean> {
const allTlds = await plugins.whoiser.allTlds();
const sanatizedTld = tldArg.startsWith('.')
? tldArg.replace('.', '').toUpperCase()
: tldArg.toUpperCase();
return allTlds.includes(sanatizedTld);
}
public async getWhoisForIp(ipArg: string) {}
}