smartwhois/ts/index.ts

124 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2023-04-03 14:48:14 +00:00
import * as plugins from './smartwhois.plugins.js';
2023-04-10 12:34:28 +00:00
export interface IWhoisInfo {
domain: string;
2023-04-10 12:42:24 +00:00
isRegistered: boolean;
domainStatus: Array<
| 'clientTransferProhibited'
| 'clientUpdateProhibited'
| 'clientDeleteProhibited'
| 'clientHold'
| 'serverTransferProhibited'
| 'serverUpdateProhibited'
| 'serverDeleteProhibited'
| 'serverHold'
| 'inactive'
| 'pendingDelete'
| 'pendingRenew'
| 'pendingTransfer'
| 'pendingUpdate'
| 'ok'>;
2023-04-10 12:34:28 +00:00
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;
}
2023-04-03 14:48:14 +00:00
export class SmartWhois {
2023-04-18 18:40:39 +00:00
/**
* can be used to prepare an input for the whois command
*/
2023-04-19 19:36:44 +00:00
public async getDomainDelegation(urlArg: string): Promise<plugins.tsclass.network.IDomainDelegation> {
2023-04-19 16:57:12 +00:00
if (!urlArg.includes('//')) {
urlArg = `https://${urlArg}`;
}
2023-04-18 18:40:39 +00:00
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,
2023-04-19 19:36:44 +00:00
domainWithoutSuffix: tldtsData.domainWithoutSuffix,
2023-04-19 19:51:14 +00:00
isIcann: tldtsData.isIcann,
2023-04-18 18:40:39 +00:00
}
}
2023-04-19 12:52:37 +00:00
public async getAdditionalWhoisDataForDomain(domainArg: string) {
if (domainArg.includes('//')) {
2023-04-19 19:36:44 +00:00
const parsedUrlResult = await this.getDomainDelegation(domainArg);
2023-04-19 12:52:37 +00:00
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,
2023-04-19 16:41:54 +00:00
httpHeaders: httpResult.headers,
httpsHeaders: httpsResult.headers,
2023-04-19 12:52:37 +00:00
}
}
2023-04-18 18:40:39 +00:00
2023-04-10 12:42:24 +00:00
public async getWhoisForDomain(domainArg: string): Promise<IWhoisInfo> {
2023-04-18 18:40:39 +00:00
if (domainArg.includes('//')) {
2023-04-19 19:36:44 +00:00
const parsedUrlResult = await this.getDomainDelegation(domainArg);
2023-04-18 18:40:39 +00:00
domainArg = parsedUrlResult.fullDomain;
}
2023-04-03 14:48:14 +00:00
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'];
2023-04-10 12:34:28 +00:00
const tldtsData = plugins.tldts.parse(domainArg);
2023-04-03 14:48:14 +00:00
return {
2023-04-10 12:34:28 +00:00
...tldtsData,
isRegistered: true,
2023-04-10 12:42:24 +00:00
domainStatus: selectedWhoisInfo['Domain Status'].map((statusArg: string) => statusArg.split(' ')[0]),
2023-04-10 12:34:28 +00:00
dnsSec: selectedWhoisInfo['DNSSEC'],
2023-04-03 14:48:14 +00:00
originalWhoisInfo: whoisInfo,
whoisServers,
registrar,
registrarUrl,
createdDate,
updatedDate,
expiryDate,
};
}
2023-04-03 15:50:55 +00:00
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);
}
2023-04-03 14:48:14 +00:00
public async getWhoisForIp(ipArg: string) {}
2023-04-03 15:50:55 +00:00
}