smartwhois/ts/index.ts
2023-04-19 21:51:14 +02:00

124 lines
3.6 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 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,
}
}
public async getAdditionalWhoisDataForDomain(domainArg: string) {
if (domainArg.includes('//')) {
const parsedUrlResult = await this.getDomainDelegation(domainArg);
domainArg = parsedUrlResult.fullDomain;
}
// lets test http response
const httpResult = await plugins.smartrequest.safeGet(`http://${domainArg}/`);
// lets test https response
const httpsResult = await plugins.smartrequest.safeGet(`https://${domainArg}/`);
return {
httpStatus: httpResult.statusCode,
httpsStatus: httpsResult.statusCode,
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: any = whoisInfo[whoisServers[0]];
const registrar = selectedWhoisInfo.Registrar;
const registrarUrl = selectedWhoisInfo['Registrar URL'];
const createdDate = selectedWhoisInfo['Created Date'];
const updatedDate = selectedWhoisInfo['Updated Date'];
const expiryDate = selectedWhoisInfo['Expiry Date'];
const tldtsData = plugins.tldts.parse(domainArg);
return {
...tldtsData,
isRegistered: true,
domainStatus: selectedWhoisInfo['Domain Status'].map((statusArg: string) => statusArg.split(' ')[0]),
dnsSec: selectedWhoisInfo['DNSSEC'],
originalWhoisInfo: whoisInfo,
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) {}
}