fix: better whois and smaller tld list
This commit is contained in:
+9
-53
@@ -1,55 +1,11 @@
|
||||
export class WhoIs {
|
||||
rdapEndpoints: Record<string, string> | null = null;
|
||||
error: string | null = null;
|
||||
|
||||
async init() {
|
||||
await this.loadRdapEndpoints();
|
||||
}
|
||||
|
||||
async loadRdapEndpoints() {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://cors.klemek.fr/https://data.iana.org/rdap/dns.json`,
|
||||
);
|
||||
const content: { services: { 0: string[]; 1: string[] }[] } =
|
||||
await response.json();
|
||||
const rdapEndpoints: Record<string, string> = {};
|
||||
content.services.forEach((item) => {
|
||||
if (item[1][0]) {
|
||||
const supplier: string = item[1][0];
|
||||
item[0].forEach((domain: string) => {
|
||||
rdapEndpoints[domain] = supplier;
|
||||
});
|
||||
}
|
||||
});
|
||||
this.rdapEndpoints = rdapEndpoints;
|
||||
} catch (e) {
|
||||
this.error = "Could not load RDAP endpoints";
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
getRdapQuery(domain: string): string {
|
||||
if (this.rdapEndpoints === null) {
|
||||
throw Error("RDAP endpoints not loaded");
|
||||
}
|
||||
for (const tld of Object.keys(this.rdapEndpoints)) {
|
||||
if (domain.endsWith(`.${tld}`) && this.rdapEndpoints[tld]) {
|
||||
return `${this.rdapEndpoints[tld]}/domain/${domain}`;
|
||||
}
|
||||
}
|
||||
throw Error(`RDAP supplier not found for ${domain}`);
|
||||
}
|
||||
|
||||
async isDomainAvailable(domain: string): Promise<boolean> {
|
||||
const url = this.getRdapQuery(domain);
|
||||
try {
|
||||
const response = await fetch(`https://cors.klemek.fr/${url}`, {
|
||||
method: "HEAD",
|
||||
});
|
||||
return response.status === 404;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
export async function isDomainAvailable(domain: string): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://whois.klemek.fr/api/lookup/${domain}`,
|
||||
);
|
||||
const content = await response.json();
|
||||
return content?.data?.nameservers?.length === 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user