Skip to content

Full Toolkit for Next-Level Domain Analysis

Add IP details subtitle 👀

Felix Wotschofskyb4232830c4f0
diff --git a/app/api/lookup-ip/route.ts b/app/api/lookup-ip/route.ts
index 5d6a9d0..37a4b2c 100644
--- a/app/api/lookup-ip/route.ts
+++ b/app/api/lookup-ip/route.ts
@@ -1,5 +1,7 @@
import isIP from 'validator/lib/isIP';
+import { getIpDetails } from '@/lib/ips';
+
export const runtime = 'edge';
export type IpLookupResponse = {
@@ -14,20 +16,6 @@ export type IpLookupResponse = {
timezone: string;
};
-export type IpLookupErrorResponse = { error: true; message: string };
-
-const getIpDetails = async (ip: string) => {
- const response = await fetch(`http://ip-api.com/json/${ip}`);
-
- if (!response.ok)
- throw new Error(`Error fetching IP details: ${response.statusText}`);
-
- const data = (await response.json()) as Record<string, any>;
- delete data.status;
-
- return data;
-};
-
const ipv4ToDnsName = (ipv4: string) =>
ipv4.split('.').reverse().join('.') + '.in-addr.arpa';
diff --git a/app/lookup/[domain]/page.tsx b/app/lookup/[domain]/page.tsx
index a8ea465..2b577de 100644
--- a/app/lookup/[domain]/page.tsx
+++ b/app/lookup/[domain]/page.tsx
@@ -5,6 +5,7 @@ import type { FC } from 'react';
import DnsTable from '@/components/results/dns/DnsTable';
import LocationSelector from '@/components/results/dns/LocationSelector';
import ResolverSelector from '@/components/results/dns/ResolverSelector';
+import { hostLookupLoader } from '@/lib/ips';
import AuthoritativeResolver from '@/lib/resolvers/AuthoritativeResolver';
import CloudflareDoHResolver from '@/lib/resolvers/CloudflareDoHResolver';
import GoogleDoHResolver from '@/lib/resolvers/GoogleDoHResolver';
@@ -35,6 +36,15 @@ const getResolver = (
}
};
+const getIpsInfo = async (ips: string[]): Promise<Record<string, string>> => {
+ const hosts = await hostLookupLoader.loadMany(ips);
+ return Object.fromEntries(
+ ips
+ .map((ip, index) => [ip, hosts[index]])
+ .filter(([, host]) => typeof host === 'string')
+ );
+};
+
type LookupDomainProps = {
params: {
domain: string;
@@ -79,6 +89,9 @@ const LookupDomain: FC<LookupDomainProps> = async ({
const resolver = getResolver(resolverName, locationName);
const records = await resolver.resolveAllRecords(domain);
+ const ipsInfo = await getIpsInfo(
+ records.A.map((r) => r.data).concat(records.AAAA.map((r) => r.data))
+ );
const hasResults =
Object.values(records)
@@ -96,7 +109,7 @@ const LookupDomain: FC<LookupDomainProps> = async ({
</div>
{hasResults ? (
- <DnsTable records={records} />
+ <DnsTable records={records} ipsInfo={ipsInfo} />
) : (
<p className="mt-24 text-center text-muted-foreground">
No DNS records found!
diff --git a/components/results/dns/DnsTable.tsx b/components/results/dns/DnsTable.tsx
index d9f717e..ff8bb55 100644
--- a/components/results/dns/DnsTable.tsx
+++ b/components/results/dns/DnsTable.tsx
@@ -14,9 +14,10 @@ import type { ResolvedRecords } from '@/lib/resolvers/DnsResolver';
type DnsTableProps = {
records: ResolvedRecords;
+ ipsInfo: Record<string, string>;
};
-const DnsTable: FC<DnsTableProps> = ({ records }) => (
+const DnsTable: FC<DnsTableProps> = ({ records, ipsInfo }) => (
<>
{Object.keys(records).map((recordType) => {
const value = records[recordType];
@@ -45,7 +46,13 @@ const DnsTable: FC<DnsTableProps> = ({ records }) => (
.slice()
.sort((a, b) => naturalCompare(a.data, b.data))
.map((v) => (
- <RecordRow key={v.type + v.data} record={v} />
+ <RecordRow
+ key={v.type + v.data}
+ name={v.name}
+ TTL={v.TTL}
+ value={v.data}
+ subvalue={ipsInfo[v.data]}
+ />
))}
</TableBody>
</Table>
diff --git a/components/results/dns/RecordRow.tsx b/components/results/dns/RecordRow.tsx
index 15c7805..d1cead0 100644
--- a/components/results/dns/RecordRow.tsx
+++ b/components/results/dns/RecordRow.tsx
@@ -5,7 +5,7 @@ import { TableCell, TableRow } from '@/components/ui/table';
import DomainLink from '@/components/results/DomainLink';
import IpLink from '@/components/results/IpLink';
-import type { RawRecord } from '@/lib/resolvers/DnsResolver';
+import { cn } from '@/lib/utils';
const DOMAIN_REGEX = /([a-zA-Z0-9-_]+\.)+[a-z]+\.?/gi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.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?/gi;
type RecordRowProps = {
- record: RawRecord;
+ name: string;
+ TTL: number;
+ value: string;
+ subvalue?: string;
};
-const RecordRow: FC<RecordRowProps> = ({ record }) => {
- let interpolatedValue: ReactNode[] | string | null = record.data;
+const RecordRow: FC<RecordRowProps> = async ({
+ name,
+ TTL,
+ value,
+ subvalue,
+}) => {
+ let interpolatedValue: ReactNode[] | string | null = value;
- const domainMatches = record.data.match(DOMAIN_REGEX);
+ const domainMatches = value.match(DOMAIN_REGEX);
for (const domain of domainMatches ?? []) {
interpolatedValue = reactStringReplace(
interpolatedValue,
@@ -33,7 +41,7 @@ const RecordRow: FC<RecordRowProps> = ({ record }) => {
);
}
- const ipv4Matches = record.data.match(IPV4_REGEX);
+ const ipv4Matches = value.match(IPV4_REGEX);
for (const domain of ipv4Matches ?? []) {
interpolatedValue = reactStringReplace(
interpolatedValue,
@@ -42,7 +50,7 @@ const RecordRow: FC<RecordRowProps> = ({ record }) => {
);
}
- const ipv6Matches = record.data.match(IPV6_REGEX);
+ const ipv6Matches = value.match(IPV6_REGEX);
for (const domain of ipv6Matches ?? []) {
interpolatedValue = reactStringReplace(
interpolatedValue,
@@ -53,9 +61,16 @@ const RecordRow: FC<RecordRowProps> = ({ record }) => {
return (
<TableRow className="hover:bg-transparent">
- <TableCell className="pl-0">{record.name}</TableCell>
- <TableCell>{record.TTL}</TableCell>
- <TableCell className="pr-0">{interpolatedValue}</TableCell>
+ <TableCell className="pl-0">{name}</TableCell>
+ <TableCell>{TTL}</TableCell>
+ <TableCell className={cn('pr-0', { ['py-1']: subvalue })}>
+ {interpolatedValue}
+ {subvalue && (
+ <span className="mt-1 block text-xs text-muted-foreground">
+ {subvalue}
+ </span>
+ )}
+ </TableCell>
</TableRow>
);
};
diff --git a/lib/ips.ts b/lib/ips.ts
new file mode 100644
index 0000000..0ec605f
--- /dev/null
+++ b/lib/ips.ts
@@ -0,0 +1,52 @@
+import DataLoader from 'dataloader';
+
+type IpDetails = {
+ country: string;
+ countryCode: string;
+ region: string;
+ regionName: string;
+ city: string;
+ zip: string;
+ lat: number;
+ lon: number;
+ timezone: string;
+ isp: string;
+ org: string;
+ as: string;
+ query: string;
+};
+
+export const getIpDetails = async (ip: string) => {
+ const response = await fetch(`http://ip-api.com/json/${ip}`);
+
+ if (!response.ok)
+ throw new Error(`Error fetching IP details: ${response.statusText}`);
+
+ const data = (await response.json()) as Record<string, any>;
+ delete data.status;
+
+ return data as IpDetails;
+};
+
+const normalizeIp = (ip: string) =>
+ ip.replace(/\.[0-9]+$/, '.0').replace(/:([0-9a-fA-F]+)$/, ':');
+
+// Normalize IPs to their CIDR ranges to reduce the number of requests and avoid rate limiting
+export const hostLookupLoader = new DataLoader(
+ async (keys: readonly string[]) =>
+ Promise.all(
+ keys.map(async (ip) => {
+ const normalIp = normalizeIp(ip);
+ const data = await getIpDetails(normalIp);
+
+ if (data.org === data.isp) {
+ return data.org;
+ }
+
+ return `${data.org} / ${data.isp}`;
+ })
+ ),
+ {
+ cacheKeyFn: normalizeIp,
+ }
+);