Skip to content

Full Toolkit for Next-Level Domain Analysis

Make DNS resolver logic more generic 👌

Felix Wotschofsky1fd6b402dd96
diff --git a/app/lookup/[domain]/page.tsx b/app/lookup/[domain]/page.tsx
index 297139e..4ddc018 100644
--- a/app/lookup/[domain]/page.tsx
+++ b/app/lookup/[domain]/page.tsx
@@ -1,7 +1,7 @@
import type { FC } from 'react';
import DnsTable from '@/components/DnsTable';
-import DnsLookup from '@/utils/DnsLookup';
+import AuthoritativeResolver from '@/lib/resolvers/AuthoritativeResolver';
type LookupDomainProps = {
params: {
@@ -12,7 +12,7 @@ type LookupDomainProps = {
export const fetchCache = 'default-no-store';
const LookupDomain: FC<LookupDomainProps> = async ({ params: { domain } }) => {
- const lookup = new DnsLookup();
+ const lookup = new AuthoritativeResolver();
const records = await lookup.resolveAllRecords(domain);
return (
diff --git a/components/DnsTable.tsx b/components/DnsTable.tsx
index 964585e..38c9101 100644
--- a/components/DnsTable.tsx
+++ b/components/DnsTable.tsx
@@ -10,7 +10,7 @@ import {
} from '@/components/ui/table';
import RecordRow from '@/components/RecordRow';
-import { ResolvedRecords } from '@/utils/DnsLookup';
+import type { ResolvedRecords } from '@/lib/resolvers/DnsResolver';
type DnsTableProps = {
records: ResolvedRecords;
diff --git a/components/RecordRow.tsx b/components/RecordRow.tsx
index 06d0201..5af8192 100644
--- a/components/RecordRow.tsx
+++ b/components/RecordRow.tsx
@@ -5,7 +5,7 @@ import { TableCell, TableRow } from '@/components/ui/table';
import DomainLink from '@/components/DomainLink';
import IpLink from '@/components/IpLink';
-import { RawRecord } from '@/utils/DnsLookup';
+import type { RawRecord } from '@/lib/resolvers/DnsResolver';
const DOMAIN_REGEX = /([a-zA-Z0-9-_]+\.)+[a-z]+\.?/gi;
const IPV4_REGEX = /(\d{1,3}\.){3}\d{1,3}/g;
diff --git a/utils/DnsLookup.ts b/lib/resolvers/AuthoritativeResolver.ts
similarity index 87%
rename from utils/DnsLookup.ts
rename to lib/resolvers/AuthoritativeResolver.ts
index 4187c46..2944888 100644
--- a/utils/DnsLookup.ts
+++ b/lib/resolvers/AuthoritativeResolver.ts
@@ -7,34 +7,9 @@ import dnsPacket, {
} from 'dns-packet';
import dgram from 'node:dgram';
-const RECORD_TYPES = [
- 'A',
- 'AAAA',
- 'CAA',
- 'CNAME',
- 'DNSKEY',
- 'DS',
- 'MX',
- 'NAPTR',
- 'NS',
- 'PTR',
- 'SOA',
- 'SRV',
- 'TXT',
-] as const;
-
-export type RecordType = (typeof RECORD_TYPES)[number];
-
-export type RawRecord = {
- name: string;
- type: RecordType;
- TTL: number;
- data: string;
-};
-
-export type ResolvedRecords = Record<string, RawRecord[]>;
-
-class DnsLookup {
+import DnsResolver, { type RawRecord, type RecordType } from './DnsResolver';
+
+class AuthoritativeResolver extends DnsResolver {
private async getRootServers() {
const response = await fetch('https://www.internic.net/domain/named.root', {
next: {
@@ -223,19 +198,12 @@ class DnsLookup {
return [];
}
- public async resolveAllRecords(domain: string): Promise<ResolvedRecords> {
- const results = await Promise.all(
- RECORD_TYPES.map((type) => this.fetchRecords(domain, type))
- );
-
- return RECORD_TYPES.reduce(
- (res, type, index) => ({
- ...res,
- [type]: results[index],
- }),
- {} as ResolvedRecords
- );
+ public async resolveRecordType(
+ domain: string,
+ type: RecordType
+ ): Promise<RawRecord[]> {
+ return this.fetchRecords(domain, type);
}
}
-export default DnsLookup;
+export default AuthoritativeResolver;
diff --git a/lib/resolvers/DnsResolver.ts b/lib/resolvers/DnsResolver.ts
new file mode 100644
index 0000000..a741440
--- /dev/null
+++ b/lib/resolvers/DnsResolver.ts
@@ -0,0 +1,47 @@
+const RECORD_TYPES = [
+ 'A',
+ 'AAAA',
+ 'CAA',
+ 'CNAME',
+ 'DNSKEY',
+ 'DS',
+ 'MX',
+ 'NAPTR',
+ 'NS',
+ 'PTR',
+ 'SOA',
+ 'SRV',
+ 'TXT',
+] as const;
+
+export type RecordType = (typeof RECORD_TYPES)[number];
+
+export type RawRecord = {
+ name: string;
+ type: RecordType;
+ TTL: number;
+ data: string;
+};
+
+export type ResolvedRecords = Record<string, RawRecord[]>;
+
+export default abstract class DnsResolver {
+ public abstract resolveRecordType(
+ domain: string,
+ type: RecordType
+ ): Promise<RawRecord[]>;
+
+ public async resolveAllRecords(domain: string): Promise<ResolvedRecords> {
+ const results = await Promise.all(
+ RECORD_TYPES.map((type) => this.resolveRecordType(domain, type))
+ );
+
+ return RECORD_TYPES.reduce(
+ (res, type, index) => ({
+ ...res,
+ [type]: results[index],
+ }),
+ {} as ResolvedRecords
+ );
+ }
+}