Skip to content

Full Toolkit for Next-Level Domain Analysis

Add IP Details Modal to Results ℹ️

Felix Wotschofskyf83aa3d59d83
diff --git a/components/IpDetailsModal.tsx b/components/IpDetailsModal.tsx
new file mode 100644
index 0000000..ba7c4a2
--- /dev/null
+++ b/components/IpDetailsModal.tsx
@@ -0,0 +1,94 @@
+import {
+ Flex,
+ Modal,
+ ModalBody,
+ ModalCloseButton,
+ ModalContent,
+ ModalHeader,
+ ModalOverlay,
+ Spinner,
+ Table,
+ Td,
+ Tr,
+} from '@chakra-ui/react';
+import useSWR from 'swr';
+
+import type { IpLookupResponse } from '@/api/lookupIp';
+
+type IpDetailsModalProps = {
+ ip: string;
+ isOpen: boolean;
+ onClose: () => void;
+};
+
+const IpDetailsModal = (props: IpDetailsModalProps) => {
+ const { data, error } = useSWR<IpLookupResponse>(
+ props.isOpen ? `/api/lookupIp?ip=${encodeURIComponent(props.ip)}` : null
+ );
+
+ let mappedEntries: { label: string; value: string }[] = [];
+ if (data) {
+ mappedEntries = [
+ {
+ label: 'IP',
+ value: props.ip,
+ },
+ ...data.reverse.map((address) => ({
+ label: 'Reverse',
+ value: address,
+ })),
+ {
+ label: 'Organization',
+ value: data.org,
+ },
+ {
+ label: 'ISP',
+ value: data.isp,
+ },
+ {
+ label: 'Location',
+ value: `${data.country}, ${data.region}, ${data.city}`,
+ },
+ {
+ label: 'Coordinates',
+ value: `Latitude: ${data.lat}; Longitude: ${data.lon}`,
+ },
+ {
+ label: 'Timezone',
+ value: data.timezone,
+ },
+ ];
+ }
+
+ return (
+ <Modal size="xl" isOpen={props.isOpen} onClose={props.onClose}>
+ <ModalOverlay />
+ <ModalContent>
+ <ModalHeader>IP Details for {props.ip}</ModalHeader>
+ <ModalCloseButton />
+ <ModalBody>
+ {!data ? (
+ <Flex justify="center" align="center">
+ <Spinner size="xl" my={8} />
+ </Flex>
+ ) : error ? (
+ <p>An error occurred!</p>
+ ) : (
+ <>
+ <Table>
+ {mappedEntries.map((el) => (
+ <Tr key={el.label + el.value}>
+ <Td pl={0}>{el.label}</Td>
+ <Td pr={0}>{el.value}</Td>
+ </Tr>
+ ))}
+ </Table>
+ </>
+ )}
+ </ModalBody>
+ </ModalContent>
+ </Modal>
+ );
+};
+
+export default IpDetailsModal;
diff --git a/components/RecordRow.tsx b/components/RecordRow.tsx
new file mode 100644
index 0000000..b7e017e
--- /dev/null
+++ b/components/RecordRow.tsx
@@ -0,0 +1,39 @@
+import { FaInfoCircle } from 'react-icons/fa';
+import { IconButton, Td, Tr, useDisclosure } from '@chakra-ui/react';
+import isIP from 'validator/lib/isIP';
+
+import { RawRecord } from '@/utils/DnsLookup';
+import IpDetailsModal from '@/components/IpDetailsModal';
+
+const RecordRow = ({ record }: { record: RawRecord }) => {
+ const { isOpen, onOpen, onClose } = useDisclosure();
+ const recordIsIp = isIP(record.data);
+
+ return (
+ <>
+ <Tr>
+ <Td>{record.name}</Td>
+ <Td>{record.TTL}</Td>
+ <Td>
+ {record.data}
+ {recordIsIp && (
+ <IconButton
+ variant="link"
+ size="sm"
+ ml={1}
+ aria-label="Get IP Info"
+ icon={<FaInfoCircle size="16px" />}
+ onClick={onOpen}
+ />
+ )}
+ </Td>
+ </Tr>
+
+ {recordIsIp && (
+ <IpDetailsModal ip={record.data} isOpen={isOpen} onClose={onClose} />
+ )}
+ </>
+ );
+};
+
+export default RecordRow;
diff --git a/package.json b/package.json
index 695f9e0..a8b7847 100644
--- a/package.json
+++ b/package.json
@@ -22,6 +22,7 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"react-icons": "^4.2.0",
+ "swr": "^0.5.6",
"validator": "^13.6.0"
},
"devDependencies": {
diff --git a/pages/api/hello.ts b/pages/api/hello.ts
deleted file mode 100644
index f8bcc7e..0000000
--- a/pages/api/hello.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
-import type { NextApiRequest, NextApiResponse } from 'next'
-
-type Data = {
- name: string
-}
-
-export default function handler(
- req: NextApiRequest,
- res: NextApiResponse<Data>
-) {
- res.status(200).json({ name: 'John Doe' })
-}
diff --git a/pages/api/lookupIp.ts b/pages/api/lookupIp.ts
new file mode 100644
index 0000000..d8107da
--- /dev/null
+++ b/pages/api/lookupIp.ts
@@ -0,0 +1,58 @@
+import type { NextApiRequest, NextApiResponse } from 'next';
+import { promisify } from 'util';
+import axios from 'axios';
+import dns from 'dns';
+import isIP from 'validator/lib/isIP';
+
+export type IpLookupResponse = {
+ reverse: string[];
+ isp: string;
+ org: string;
+ country: string;
+ region: string;
+ city: string;
+ timezone: string;
+ lat: string;
+ lon: string;
+};
+
+export type IpLookupErrorResponse = { error: true; message: string };
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse<IpLookupResponse | IpLookupErrorResponse>
+) {
+ if (
+ !req.query.ip ||
+ typeof req.query.ip === 'object' ||
+ !isIP(req.query.ip)
+ ) {
+ res.status(400).json({
+ error: true,
+ message: 'Bad Request',
+ });
+ return;
+ }
+
+ const url = `http://ip-api.com/json/${req.query.ip}`;
+ const response = await axios(url);
+
+ let reverse: string[] = [];
+ try {
+ reverse = await promisify(dns.reverse)(req.query.ip);
+ } catch (error) {
+ console.error(error);
+ }
+
+ res.json({
+ reverse: reverse,
+ isp: response.data.isp,
+ org: response.data.org,
+ country: response.data.country,
+ region: response.data.regionName,
+ city: response.data.city,
+ timezone: response.data.timezone,
+ lat: response.data.lat,
+ lon: response.data.lon,
+ });
+}
diff --git a/pages/lookup/[domain].tsx b/pages/lookup/[domain].tsx
index 445e80b..af8ae9a 100644
--- a/pages/lookup/[domain].tsx
+++ b/pages/lookup/[domain].tsx
@@ -2,17 +2,17 @@ import type { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { useRouter } from 'next/router';
import Head from 'next/head';
import {
+ Container,
+ Heading,
Table,
- Thead,
Tbody,
- Tr,
Th,
- Td,
- Heading,
- Container,
+ Thead,
+ Tr,
} from '@chakra-ui/react';
-import DnsLookup, { RawRecord, ResolvedRecords } from '@/utils/DnsLookup';
+import DnsLookup, { ResolvedRecords } from '@/utils/DnsLookup';
+import RecordRow from '@/components/RecordRow';
type LookupDomainProps = {
records?: ResolvedRecords;
@@ -40,14 +40,6 @@ export const getServerSideProps: GetServerSideProps<LookupDomainProps> = async (
};
};
-const CustomRow = ({ record }: { record: RawRecord }) => (
- <Tr>
- <Td>{record.name}</Td>
- <Td>{record.TTL}</Td>
- <Td>{record.data}</Td>
- </Tr>
-);
-
const LookupDomain = ({
records,
}: InferGetServerSidePropsType<typeof getServerSideProps>) => {
@@ -103,7 +95,7 @@ const LookupDomain = ({
</Thead>
<Tbody>
{value.map((v) => (
- <CustomRow key={JSON.stringify(v)} record={v} />
+ <RecordRow key={JSON.stringify(v)} record={v} />
))}
</Tbody>
</Table>
diff --git a/tsconfig.json b/tsconfig.json
index 88d11a5..bef9bf2 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -15,6 +15,7 @@
"jsx": "preserve",
"baseUrl": ".",
"paths": {
+ "@/api/*": ["pages/api/*"],
"@/components/*": ["components/*"],
"@/utils/*": ["utils/*"]
}
diff --git a/yarn.lock b/yarn.lock
index abe88f6..5f0e557 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1662,6 +1662,11 @@ depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=
+ version "2.0.2"
+ resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
+ integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==
+
des.js@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/des.js/-/des.js-1.0.1.tgz#5382142e1bdc53f85d86d53e5f4aa7deb91e0843"
@@ -3894,6 +3899,13 @@ supports-color@^8.0.0:
dependencies:
has-flag "^4.0.0"
+swr@^0.5.6:
+ version "0.5.6"
+ resolved "https://registry.yarnpkg.com/swr/-/swr-0.5.6.tgz#70bfe9bc9d7ac49a064be4a0f4acf57982e55a31"
+ integrity sha512-Bmx3L4geMZjYT5S2Z6EE6/5Cx6v1Ka0LhqZKq8d6WL2eu9y6gHWz3dUzfIK/ymZVHVfwT/EweFXiYGgfifei3w==
+ dependencies:
+ dequal "2.0.2"
+
table@^6.0.9:
version "6.7.1"
resolved "https://registry.yarnpkg.com/table/-/table-6.7.1.tgz#ee05592b7143831a8c94f3cee6aae4c1ccef33e2"