Skip to content

Full Toolkit for Next-Level Domain Analysis

Filter search suggestions to lookups that returned results πŸ”πŸŽ― (#61)

Felix Wotschofsky000c40d22a5d
diff --git a/SETUP.md b/SETUP.md
index cf4084a..393fe42 100644
--- a/SETUP.md
+++ b/SETUP.md
@@ -42,16 +42,16 @@ Use the following schema for creating the dataset:
"fields": []
},
{
- "name": "baseDomain",
+ "name": "lookupType",
"mode": "NULLABLE",
"type": "STRING",
"description": null,
"fields": []
},
{
- "name": "timestamp",
+ "name": "hasResults",
"mode": "NULLABLE",
- "type": "TIMESTAMP",
+ "type": "BOOLEAN",
"description": null,
"fields": []
},
@@ -75,6 +75,20 @@ Use the following schema for creating the dataset:
"type": "BOOLEAN",
"description": null,
"fields": []
+ },
+ {
+ "name": "baseDomain",
+ "mode": "NULLABLE",
+ "type": "STRING",
+ "description": null,
+ "fields": []
+ },
+ {
+ "name": "timestamp",
+ "mode": "NULLABLE",
+ "type": "TIMESTAMP",
+ "description": null,
+ "fields": []
}
]
```
@@ -94,10 +108,28 @@ FROM
`project.dataset.lookups`
WHERE
baseDomain IS NOT NULL
+ AND hasResults = TRUE
GROUP BY
baseDomain;
```
+#### Migrating an existing dataset
+
+If your `lookups` table predates the `hasResults` and `lookupType` columns, add them and recreate the materialized view above. Optionally backfill `hasResults` so popularity isn't reset on deploy.
+
+```sql
+ALTER TABLE `project.dataset.lookups`
+ADD COLUMN hasResults BOOL,
+ADD COLUMN lookupType STRING;
+
+-- Optional backfill: treat all historical rows as successful lookups.
+UPDATE `project.dataset.lookups`
+SET hasResults = TRUE
+WHERE hasResults IS NULL;
+```
+
+Then re-run the `CREATE OR REPLACE MATERIALIZED VIEW` statement from the previous section so the view picks up the new `hasResults` filter.
+
#### Creating a service account
When creating a service account, ensure that the following permissions are granted:
diff --git a/app/lookup/[domain]/(dns)/page.tsx b/app/lookup/[domain]/(dns)/page.tsx
index 7fc3655..1f96751 100644
--- a/app/lookup/[domain]/(dns)/page.tsx
+++ b/app/lookup/[domain]/(dns)/page.tsx
@@ -5,6 +5,7 @@ import type { FC } from 'react';
import { ALL_RECORD_TYPES } from '@/lib/data';
import { getRecordContextEntries } from '@/lib/record-context';
import { getResolverFromName } from '@/lib/resolvers/utils';
+import { recordLookupAfter } from '@/lib/search';
import { DnsTable } from './_components/dns-table';
@@ -68,6 +69,8 @@ const DnsResultsPage: FC<DnsResultsPageProps> = async ({
0,
) > 0;
+ await recordLookupAfter(domain, 'dns', hasResults);
+
if (!hasResults) {
notFound();
}
diff --git a/app/lookup/[domain]/certs/page.tsx b/app/lookup/[domain]/certs/page.tsx
index 0c5f271..ef219af 100644
--- a/app/lookup/[domain]/certs/page.tsx
+++ b/app/lookup/[domain]/certs/page.tsx
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
import type { FC } from 'react';
import { lookupRelatedCerts } from '@/lib/certs';
+import { recordLookupAfter } from '@/lib/search';
import { CertsTable } from './_components/table';
@@ -39,6 +40,8 @@ const CertsResultsPage: FC<CertsResultsPageProps> = async (props) => {
const certs = await lookupRelatedCerts(domain);
+ await recordLookupAfter(domain, 'certs', certs.length > 0);
+
if (!certs.length) {
return (
<p className="mt-8 text-center text-zinc-500 dark:text-zinc-400">
diff --git a/app/lookup/[domain]/layout.tsx b/app/lookup/[domain]/layout.tsx
index 35c60b7..04b5aa7 100644
--- a/app/lookup/[domain]/layout.tsx
+++ b/app/lookup/[domain]/layout.tsx
@@ -1,16 +1,12 @@
import { ExternalLinkIcon } from 'lucide-react';
import type { Metadata } from 'next';
import dynamic from 'next/dynamic';
-import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
-import { after } from 'next/server';
import { type FC, type ReactNode } from 'react';
import { Card } from '@/components/ui/card';
import { ClientOnly } from '@/components/client-only';
-import { getVisitorIp, isUserBot } from '@/lib/api';
-import { recordLookup } from '@/lib/search';
import { isValidDomain, isWildcardDomain } from '@/lib/utils';
import { Footer } from '../../_components/footer';
@@ -58,13 +54,6 @@ const LookupLayout: FC<LookupLayoutProps> = async (props) => {
return notFound();
}
- const headersList = await headers();
- const ip = getVisitorIp(headersList);
- const { isBot, userAgent } = isUserBot(headersList);
- after(async () => {
- await recordLookup({ domain, ip, userAgent, isBot });
- });
-
return (
<>
<Header showSearch={true} />
diff --git a/app/lookup/[domain]/subdomains/page.tsx b/app/lookup/[domain]/subdomains/page.tsx
index 09d2ea5..702c6b8 100644
--- a/app/lookup/[domain]/subdomains/page.tsx
+++ b/app/lookup/[domain]/subdomains/page.tsx
@@ -6,6 +6,7 @@ import { Button } from '@/components/ui/button';
import { generateCsv } from '@/lib/csv';
import { CloudflareDoHResolver } from '@/lib/resolvers/cloudflare';
+import { recordLookupAfter } from '@/lib/search';
import { findSubdomains } from '@/lib/subdomains';
import { SubdomainsInfoAlert } from './_components/info-alert';
@@ -46,6 +47,8 @@ const SubdomainsResultsPage: FC<SubdomainsResultsPageProps> = async ({
const { results, detailsReduced, detailedResultsLimit } =
await findSubdomains(domain, new CloudflareDoHResolver());
+ await recordLookupAfter(domain, 'subdomains', results.length > 0);
+
const exportFileName = `Domain Digger Subdomains Export ${domain.replaceAll(
'.',
'_',
diff --git a/app/lookup/[domain]/whois/page.tsx b/app/lookup/[domain]/whois/page.tsx
index afdc5c0..3752e40 100644
--- a/app/lookup/[domain]/whois/page.tsx
+++ b/app/lookup/[domain]/whois/page.tsx
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
import Link from 'next/link';
import { type FC, Fragment } from 'react';
+import { recordLookupAfter } from '@/lib/search';
import { getBaseDomain } from '@/lib/utils';
import { lookupWhois } from '@/lib/whois';
@@ -46,6 +47,8 @@ const WhoisResultsPage: FC<WhoisResultsPageProps> = async ({
const baseDomain = getBaseDomain(domain);
const results = await lookupWhois(forceOriginal ? domain : baseDomain);
+ await recordLookupAfter(domain, 'whois', results.length > 0);
+
if (results.length === 0) {
throw new Error('No results found');
}
diff --git a/lib/search.ts b/lib/search.ts
index 38ee026..ecd40f1 100644
--- a/lib/search.ts
+++ b/lib/search.ts
@@ -1,9 +1,17 @@
+import { headers } from 'next/headers';
+import { after } from 'next/server';
+
import { env } from '@/env';
+import { getVisitorIp, isUserBot } from '@/lib/api';
import { bigquery } from '@/lib/bigquery';
import { getBaseDomain } from '@/lib/utils';
+export type LookupType = 'dns' | 'whois' | 'subdomains' | 'certs';
+
type LookupLogPayload = {
domain: string;
+ lookupType: LookupType;
+ hasResults: boolean;
ip: string;
userAgent: string | null;
isBot: boolean;
@@ -23,11 +31,13 @@ export const recordLookup = async (payload: LookupLogPayload) => {
rows: [
{
domain: payload.domain,
- baseDomain,
- timestamp: Math.floor(new Date().getTime() / 1000),
+ lookupType: payload.lookupType,
+ hasResults: payload.hasResults,
ip: payload.ip,
userAgent: payload.userAgent,
isBot: payload.isBot,
+ baseDomain,
+ timestamp: Math.floor(new Date().getTime() / 1000),
},
],
})
@@ -42,6 +52,27 @@ export const recordLookup = async (payload: LookupLogPayload) => {
});
};
+export const recordLookupAfter = async (
+ domain: string,
+ lookupType: LookupType,
+ hasResults: boolean,
+) => {
+ // Resolve headers up-front; the request scope may be torn down inside after().
+ const headersList = await headers();
+ const ip = getVisitorIp(headersList);
+ const { isBot, userAgent } = isUserBot(headersList);
+ after(async () => {
+ await recordLookup({
+ domain,
+ lookupType,
+ hasResults,
+ ip,
+ userAgent,
+ isBot,
+ });
+ });
+};
+
export const getSearchSuggestions = async (query: string) => {
const primarySuggestions = await getSearchSuggestionsForPrefix(query);