Files
smartwhois/ts/index.ts
T

151 lines
4.8 KiB
TypeScript
Raw Permalink Normal View History

2023-04-03 16:48:14 +02:00
import * as plugins from './smartwhois.plugins.js';
2023-04-10 14:34:28 +02:00
export interface IWhoisInfo {
domain: string;
2023-04-10 14:42:24 +02:00
isRegistered: boolean;
domainStatus: Array<
| 'clientTransferProhibited'
| 'clientUpdateProhibited'
| 'clientDeleteProhibited'
| 'clientHold'
| 'serverTransferProhibited'
| 'serverUpdateProhibited'
| 'serverDeleteProhibited'
| 'serverHold'
| 'inactive'
| 'pendingDelete'
| 'pendingRenew'
| 'pendingTransfer'
| 'pendingUpdate'
| 'ok'>;
2023-04-10 14:34:28 +02: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;
}
export interface IAdditionalWhoisData {
httpStatus: number;
httpsStatus: number;
httpHeaders: unknown;
httpsHeaders: unknown;
}
2023-04-03 16:48:14 +02:00
export class SmartWhois {
2023-04-18 20:40:39 +02:00
/**
* can be used to prepare an input for the whois command
*/
2023-04-19 21:36:44 +02:00
public async getDomainDelegation(urlArg: string): Promise<plugins.tsclass.network.IDomainDelegation> {
2023-04-19 18:57:12 +02:00
if (!urlArg.includes('//')) {
urlArg = `https://${urlArg}`;
}
2023-04-18 20:40:39 +02: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 ?? '',
domainWithoutSuffix: tldtsData.domainWithoutSuffix ?? '',
isIcann: tldtsData.isIcann ?? undefined,
};
2023-04-18 20:40:39 +02:00
}
public async getAdditionalWhoisDataForDomain(domainArg: string): Promise<IAdditionalWhoisData> {
2023-04-19 14:52:37 +02:00
if (domainArg.includes('//')) {
2023-04-19 21:36:44 +02:00
const parsedUrlResult = await this.getDomainDelegation(domainArg);
2023-04-19 14:52:37 +02:00
domainArg = parsedUrlResult.fullDomain;
}
// lets test http response
2025-07-29 00:27:48 +00:00
const httpResult = await plugins.smartrequest.SmartRequest.create()
.url(`http://${domainArg}/`)
.get();
2023-04-19 14:52:37 +02:00
// lets test https response
2025-07-29 00:27:48 +00:00
const httpsResult = await plugins.smartrequest.SmartRequest.create()
.url(`https://${domainArg}/`)
.get();
2023-04-19 14:52:37 +02:00
return {
2025-07-29 00:27:48 +00:00
httpStatus: httpResult.status,
httpsStatus: httpsResult.status,
2023-04-19 18:41:54 +02:00
httpHeaders: httpResult.headers,
httpsHeaders: httpsResult.headers,
};
2023-04-19 14:52:37 +02:00
}
2023-04-10 14:42:24 +02:00
public async getWhoisForDomain(domainArg: string): Promise<IWhoisInfo> {
2023-04-18 20:40:39 +02:00
if (domainArg.includes('//')) {
2023-04-19 21:36:44 +02:00
const parsedUrlResult = await this.getDomainDelegation(domainArg);
2023-04-18 20:40:39 +02:00
domainArg = parsedUrlResult.fullDomain;
}
2023-04-03 16:48:14 +02:00
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'];
2023-04-10 14:34:28 +02:00
const tldtsData = plugins.tldts.parse(domainArg);
2023-04-03 16:48:14 +02:00
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 ?? '',
2023-04-10 14:34:28 +02:00
isRegistered: true,
domainStatus,
dnsSec: getStringField('DNSSEC') as IWhoisInfo['dnsSec'],
originalWhoisInfo: whoisInfo as Record<string, unknown>,
2023-04-03 16:48:14 +02:00
whoisServers,
registrar,
registrarUrl,
createdDate,
updatedDate,
expiryDate,
};
}
2023-04-03 17:50:55 +02: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 16:48:14 +02:00
public async getWhoisForIp(ipArg: string) {}
2023-04-03 17:50:55 +02:00
}