Skip to content

Full Toolkit for Next-Level Domain Analysis

Clean up code 🧹

Felix Wotschofskyccc0ea115cd1
diff --git a/app/lookup/[domain]/certs/page.tsx b/app/lookup/[domain]/certs/page.tsx
index cd68b9d..b12e656 100644
--- a/app/lookup/[domain]/certs/page.tsx
+++ b/app/lookup/[domain]/certs/page.tsx
@@ -10,39 +10,12 @@ import {
} from '@/components/ui/table';
import DomainLink from '@/components/DomainLink';
-
-type CertsData = {
- issuer_ca_id: number;
- issuer_name: string;
- common_name: string;
- name_value: string;
- id: number;
- entry_timestamp: string;
- not_before: string;
- not_after: string;
- serial_number: string;
-}[];
+import { lookupCerts } from '@/lib/certs';
export const runtime = 'edge';
// crt.sh located in GB, always use LHR1 for lowest latency
export const preferredRegion = 'lhr1';
-const lookupCerts = async (domain: string): Promise<CertsData> => {
- const response = await fetch(
- 'https://crt.sh?' +
- new URLSearchParams({
- Identity: domain,
- output: 'json',
- })
- );
-
- if (!response.ok) {
- throw new Error('Failed to fetch certs');
- }
-
- return await response.json();
-};
-
type CertsResultsPageProps = {
params: {
domain: string;
diff --git a/app/lookup/[domain]/layout.tsx b/app/lookup/[domain]/layout.tsx
index c6e466a..7f151fa 100644
--- a/app/lookup/[domain]/layout.tsx
+++ b/app/lookup/[domain]/layout.tsx
@@ -7,7 +7,7 @@ import RelatedDomains from '@/components/RelatedDomains';
import ResultsTabs from '@/components/ResultsTabs';
import SearchForm from '@/components/SearchForm';
import WhoisQuickInfo from '@/components/WhoisQuickInfo';
-import isValidDomain from '@/utils/isValidDomain';
+import { isValidDomain } from '@/lib/utils';
type LookupLayoutProps = {
children: ReactNode;
diff --git a/app/lookup/[domain]/map/page.tsx b/app/lookup/[domain]/map/page.tsx
index ffa45b8..1e7f6ba 100644
--- a/app/lookup/[domain]/map/page.tsx
+++ b/app/lookup/[domain]/map/page.tsx
@@ -68,7 +68,6 @@ const MapResultsPage: FC<MapResultsPageProps> = async ({
previous.CNAME.toSorted().toString() !==
current.CNAME.toSorted().toString()
) {
- console.log(previous, current);
hasDifferentRecords = true;
break;
}
diff --git a/app/lookup/[domain]/subdomains/page.tsx b/app/lookup/[domain]/subdomains/page.tsx
index 2bf1457..b731b81 100644
--- a/app/lookup/[domain]/subdomains/page.tsx
+++ b/app/lookup/[domain]/subdomains/page.tsx
@@ -11,40 +11,13 @@ import {
} from '@/components/ui/table';
import DomainLink from '@/components/DomainLink';
+import { lookupCerts } from '@/lib/certs';
import CloudflareDoHResolver from '@/lib/resolvers/CloudflareDoHResolver';
-type CertsData = {
- issuer_ca_id: number;
- issuer_name: string;
- common_name: string;
- name_value: string;
- id: number;
- entry_timestamp: string;
- not_before: string;
- not_after: string;
- serial_number: string;
-}[];
-
export const runtime = 'edge';
// crt.sh located in GB, always use LHR1 for lowest latency
export const preferredRegion = 'lhr1';
-const lookupCerts = async (domain: string): Promise<CertsData> => {
- const response = await fetch(
- 'https://crt.sh?' +
- new URLSearchParams({
- Identity: domain,
- output: 'json',
- })
- );
-
- if (!response.ok) {
- throw new Error('Failed to fetch certs');
- }
-
- return await response.json();
-};
-
type SubdomainsResultsPageProps = {
params: {
domain: string;
diff --git a/app/lookup/[domain]/whois-summary/route.ts b/app/lookup/[domain]/whois-summary/route.ts
index 7399f52..7533fe0 100644
--- a/app/lookup/[domain]/whois-summary/route.ts
+++ b/app/lookup/[domain]/whois-summary/route.ts
@@ -1,6 +1,6 @@
import whoiser, { WhoisSearchResult } from 'whoiser';
-import isValidDomain from '@/utils/isValidDomain';
+import { isValidDomain } from '@/lib/utils';
export type WhoisSummaryResponse = {
registrar: string | null;
diff --git a/components/BookmarkletLink.tsx b/components/BookmarkletLink.tsx
index ddd3ebc..dcdba81 100644
--- a/components/BookmarkletLink.tsx
+++ b/components/BookmarkletLink.tsx
@@ -19,8 +19,8 @@ import {
const BookmarkletLink = () => {
const [target, setTarget] = useState('');
- const [isOpen, setOpen] = useState(false);
- const [isActivated, setActivated] = useState(false);
+ const [isOpen, setIsOpen] = useState(false);
+ const [activated, setActivated] = useState(false);
useEffect(() => {
const rawScript = `
@@ -43,9 +43,9 @@ const BookmarkletLink = () => {
const clickHandler = useCallback<MouseEventHandler<HTMLAnchorElement>>(
(event) => {
event.preventDefault();
- setOpen(true);
+ setIsOpen(true);
},
- [setOpen]
+ [setIsOpen]
);
const mouseDownHandler = useCallback<
@@ -78,14 +78,14 @@ const BookmarkletLink = () => {
onClick={clickHandler}
onMouseDown={mouseDownHandler}
>
- {isActivated ? 'Inspect Domain' : 'Bookmarklet'}
+ {activated ? 'Inspect Domain' : 'Bookmarklet'}
</a>
) : (
<span className="text-center">Loading...</span>
)}
</div>
- <AlertDialog open={isOpen} onOpenChange={setOpen}>
+ <AlertDialog open={isOpen} onOpenChange={setIsOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Installation required</AlertDialogTitle>
diff --git a/components/IpDetailsModal.tsx b/components/IpDetailsModal.tsx
index f1378d7..13c1a01 100644
--- a/components/IpDetailsModal.tsx
+++ b/components/IpDetailsModal.tsx
@@ -15,13 +15,10 @@ import {
} from '@/components/ui/dialog';
import {
Drawer,
- DrawerClose,
DrawerContent,
DrawerDescription,
- DrawerFooter,
DrawerHeader,
DrawerTitle,
- DrawerTrigger,
} from '@/components/ui/drawer';
import { Spinner } from '@/components/ui/spinner';
import { Table, TableBody, TableCell, TableRow } from '@/components/ui/table';
@@ -30,8 +27,6 @@ import type { IpLookupResponse } from '@/app/api/lookupIp/route';
import CopyButton from '@/components/CopyButton';
import DomainLink from '@/components/DomainLink';
-import { Button } from './ui/button';
-
const LocationMap = dynamic(() => import('@/components/LocationMap'), {
ssr: false,
});
diff --git a/components/LocationSelector.tsx b/components/LocationSelector.tsx
index bb7219b..91c5873 100644
--- a/components/LocationSelector.tsx
+++ b/components/LocationSelector.tsx
@@ -36,8 +36,8 @@ const LocationSelector: FC<LocationSelectorProps> = ({
current.set('location', value);
}
- const search = current.toString();
- router.push(`${pathname}${search ? `?${search}` : ''}`);
+ const search = current.size ? `?${current.toString()}` : '';
+ router.push(`${pathname}${search}`);
},
[router, pathname, searchParams]
);
diff --git a/components/ResolverSelector.tsx b/components/ResolverSelector.tsx
index b347f6b..a2fc754 100644
--- a/components/ResolverSelector.tsx
+++ b/components/ResolverSelector.tsx
@@ -30,8 +30,8 @@ const ResolverSelector: FC<ResolverSelectorProps> = ({ initialValue }) => {
current.set('resolver', value);
}
- const search = current.toString();
- router.push(`${pathname}${search ? `?${search}` : ''}`);
+ const search = current.size ? `?${current.toString()}` : '';
+ router.push(`${pathname}${search}`);
},
[router, pathname, searchParams]
);
diff --git a/components/ResultsGlobe.tsx b/components/ResultsGlobe.tsx
index e9e1d80..b8def9f 100644
--- a/components/ResultsGlobe.tsx
+++ b/components/ResultsGlobe.tsx
@@ -5,7 +5,7 @@ import naturalCompare from 'natural-compare-lite';
import { useTheme } from 'next-themes';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/navigation';
-import { type FC, useEffect, useId, useRef, useState } from 'react';
+import { type FC, useEffect, useRef, useState } from 'react';
import { cn } from '@/lib/utils';
diff --git a/components/SearchForm.tsx b/components/SearchForm.tsx
index 3247510..66e28e9 100644
--- a/components/SearchForm.tsx
+++ b/components/SearchForm.tsx
@@ -8,8 +8,7 @@ import { ChangeEvent, FormEvent, useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
-import { cn } from '@/lib/utils';
-import isValidDomain from '@/utils/isValidDomain';
+import { cn, isValidDomain } from '@/lib/utils';
enum FormStates {
Initial,
diff --git a/lib/certs.ts b/lib/certs.ts
new file mode 100644
index 0000000..f2296f0
--- /dev/null
+++ b/lib/certs.ts
@@ -0,0 +1,27 @@
+type CertsData = {
+ issuer_ca_id: number;
+ issuer_name: string;
+ common_name: string;
+ name_value: string;
+ id: number;
+ entry_timestamp: string;
+ not_before: string;
+ not_after: string;
+ serial_number: string;
+}[];
+
+export const lookupCerts = async (domain: string): Promise<CertsData> => {
+ const response = await fetch(
+ 'https://crt.sh?' +
+ new URLSearchParams({
+ Identity: domain,
+ output: 'json',
+ })
+ );
+
+ if (!response.ok) {
+ throw new Error('Failed to fetch certs');
+ }
+
+ return await response.json();
+};
diff --git a/lib/resolvers/BaseDoHResolver.ts b/lib/resolvers/BaseDoHResolver.ts
new file mode 100644
index 0000000..208d1ef
--- /dev/null
+++ b/lib/resolvers/BaseDoHResolver.ts
@@ -0,0 +1,66 @@
+import DnsResolver, {
+ type RawRecord,
+ RECORD_TYPES_BY_DECIMAL,
+ type RecordType,
+} from './DnsResolver';
+
+type DoHResponse = {
+ Status: number;
+ TC: boolean;
+ RD: boolean;
+ RA: boolean;
+ AD: boolean;
+ CD: boolean;
+ Question: {
+ name: string;
+ type: number;
+ }[];
+ Answer?: {
+ name: string;
+ type: number;
+ TTL: number;
+ data: string;
+ }[];
+ Authority?: {
+ name: string;
+ type: number;
+ TTL: number;
+ data: string;
+ }[];
+};
+
+export default class BaseDoHResolver extends DnsResolver {
+ constructor(
+ private sendRequest: (domain: string, type: RecordType) => Promise<Response>
+ ) {
+ super();
+ }
+
+ public async resolveRecordType(
+ domain: string,
+ type: RecordType
+ ): Promise<RawRecord[]> {
+ const response = await this.sendRequest(domain, type);
+ if (!response.ok)
+ throw new Error(`Bad response from Google: ${response.statusText}`);
+ const results = (await response.json()) as DoHResponse;
+
+ if (!results.Answer) {
+ return [];
+ }
+
+ const filteredAnswers = results.Answer.filter(
+ (answer) =>
+ answer.type in RECORD_TYPES_BY_DECIMAL &&
+ // @ts-expect-error
+ RECORD_TYPES_BY_DECIMAL[answer.type] === type
+ );
+
+ return filteredAnswers.map((answer) => ({
+ name: answer.name,
+ type,
+ TTL: answer.TTL,
+ data: answer.data,
+ }));
+ }
+}
diff --git a/lib/resolvers/CloudflareDoHResolver.ts b/lib/resolvers/CloudflareDoHResolver.ts
index d50399e..f67a287 100644
--- a/lib/resolvers/CloudflareDoHResolver.ts
+++ b/lib/resolvers/CloudflareDoHResolver.ts
@@ -1,66 +1,15 @@
-import DnsResolver, {
- type RawRecord,
- RECORD_TYPES_BY_DECIMAL,
- type RecordType,
-} from './DnsResolver';
-
-type DoHResponse = {
- Status: number;
- TC: boolean;
- RD: boolean;
- RA: boolean;
- AD: boolean;
- CD: boolean;
- Question: {
- name: string;
- type: number;
- }[];
- Answer?: {
- name: string;
- type: number;
- TTL: number;
- data: string;
- }[];
- Authority?: {
- name: string;
- type: number;
- TTL: number;
- data: string;
- }[];
-};
-
-export default class CloudflareDoHResolver extends DnsResolver {
- public async resolveRecordType(
- domain: string,
- type: RecordType
- ): Promise<RawRecord[]> {
- const response = await fetch(
- `https://cloudflare-dns.com/dns-query?name=${domain}&type=${type}`,
- {
- method: 'GET',
- headers: { Accept: 'application/dns-json' },
- }
- );
- if (!response.ok)
- throw new Error(`Bad response from Cloudflare: ${response.statusText}`);
- const results = (await response.json()) as DoHResponse;
-
- if (!results.Answer) {
- return [];
- }
-
- const filteredAnswers = results.Answer.filter(
- (answer) =>
- answer.type in RECORD_TYPES_BY_DECIMAL &&
- // @ts-expect-error
- RECORD_TYPES_BY_DECIMAL[answer.type] === type
+import BaseDoHResolver from './BaseDoHResolver';
+
+export default class CloudflareDoHResolver extends BaseDoHResolver {
+ constructor() {
+ super((domain, type) =>
+ fetch(
+ `https://cloudflare-dns.com/dns-query?name=${domain}&type=${type}`,
+ {
+ method: 'GET',
+ headers: { Accept: 'application/dns-json' },
+ }
+ )
);
-
- return filteredAnswers.map((answer) => ({
- name: answer.name,
- type,
- TTL: answer.TTL,
- data: answer.data,
- }));
}
}
diff --git a/lib/resolvers/GoogleDoHResolver.ts b/lib/resolvers/GoogleDoHResolver.ts
index 412a6da..f75b947 100644
--- a/lib/resolvers/GoogleDoHResolver.ts
+++ b/lib/resolvers/GoogleDoHResolver.ts
@@ -1,66 +1,12 @@
-import DnsResolver, {
- type RawRecord,
- RECORD_TYPES_BY_DECIMAL,
- type RecordType,
-} from './DnsResolver';
+import BaseDoHResolver from './BaseDoHResolver';
-type DoHResponse = {
- Status: number;
- TC: boolean;
- RD: boolean;
- RA: boolean;
- AD: boolean;
- CD: boolean;
- Question: {
- name: string;
- type: number;
- }[];
- Answer?: {
- name: string;
- type: number;
- TTL: number;
- data: string;
- }[];
- Authority?: {
- name: string;
- type: number;
- TTL: number;
- data: string;
- }[];
-};
-
-export default class GoogleDoHResolver extends DnsResolver {
- public async resolveRecordType(
- domain: string,
- type: RecordType
- ): Promise<RawRecord[]> {
- const response = await fetch(
- `https://dns.google/resolve?name=${domain}&type=${type}`,
- {
+export default class GoogleDoHResolver extends BaseDoHResolver {
+ constructor() {
+ super((domain, type) =>
+ fetch(`https://dns.google/resolve?name=${domain}&type=${type}`, {
method: 'GET',
headers: { Accept: 'application/json' },
- }
+ })
);
- if (!response.ok)
- throw new Error(`Bad response from Google: ${response.statusText}`);
- const results = (await response.json()) as DoHResponse;
-
- if (!results.Answer) {
- return [];
- }
-
- const filteredAnswers = results.Answer.filter(
- (answer) =>
- answer.type in RECORD_TYPES_BY_DECIMAL &&
- // @ts-expect-error
- RECORD_TYPES_BY_DECIMAL[answer.type] === type
- );
-
- return filteredAnswers.map((answer) => ({
- name: answer.name,
- type,
- TTL: answer.TTL,
- data: answer.data,
- }));
}
}
diff --git a/lib/utils.ts b/lib/utils.ts
index 9ad0df4..4f49246 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -4,3 +4,8 @@ import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
+
+export const isValidDomain = (domain: string) =>
+ /^(\*\.)?(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]{1,1}\.)*(xn--)?([a-z0-9][a-z0-9\-]{0,60}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
+ domain
+ );
diff --git a/utils/isValidDomain.ts b/utils/isValidDomain.ts
deleted file mode 100644
index 0c3ae4d..0000000
--- a/utils/isValidDomain.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-const isValidDomain = (domain: string) =>
- /^(\*\.)?(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]{1,1}\.)*(xn--)?([a-z0-9][a-z0-9\-]{0,60}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
- domain
- );
-
-export default isValidDomain;