Full Toolkit for Next-Level Domain Analysis
Move visitor ip detection out of request logging 🛻
Felix Wotschofsky4cb6d33cdc7d
diff --git a/app/lookup/[domain]/layout.tsx b/app/lookup/[domain]/layout.tsxindex db82639..ab272f3 100644--- a/app/lookup/[domain]/layout.tsx+++ b/app/lookup/[domain]/layout.tsx@@ -5,7 +5,7 @@ import { headers } from 'next/headers';import { notFound } from 'next/navigation';import { type FC, type ReactNode } from 'react';-import { isUserBot } from '@/lib/api';+import { getVisitorIp, isUserBot } from '@/lib/api';import { recordLookup } from '@/lib/search';import { isValidDomain } from '@/lib/utils';@@ -46,8 +46,9 @@ const LookupLayout: FC<LookupLayoutProps> = ({return notFound();}+ const ip = getVisitorIp(headers());const { isBot } = isUserBot(headers());- recordLookup({ domain, isBot });+ recordLookup({ domain, ip, isBot });return (<>diff --git a/lib/api.ts b/lib/api.tsindex 3aad467..fea4bcc 100644--- a/lib/api.ts+++ b/lib/api.ts@@ -20,6 +20,12 @@ export const applyRateLimit = async (return result.success;};+export const getVisitorIp = (headers: ReadonlyHeaders) => {+ const forwardedFor = headers.get('x-forwarded-for');+ const ip = (forwardedFor ?? '127.0.0.1').split(',')[0];+ return ip;+};+export const isUserBot = (headers: ReadonlyHeaders) => {const userAgent = headers.get('user-agent');const isBot = !userAgent || isbot(userAgent);diff --git a/lib/search.ts b/lib/search.tsindex 0c56a48..85fbd00 100644--- a/lib/search.ts+++ b/lib/search.ts@@ -1,32 +1,34 @@-import { headers } from 'next/headers';import { getDomain } from 'tldts';import { bigquery } from '@/lib/bigquery';-export const recordLookup = (data: { domain: string; isBot: boolean }) => {+type LookupLogPayload = {+ domain: string;+ ip: string;+ isBot: boolean;+};++export const recordLookup = async (payload: LookupLogPayload) => {if (!bigquery) {return;}// Remove wildcard prefix to avoid base domain not being extracted correctly// Remove trailing dot- const cleanedDomain = data.domain.replace(/^\*\./, '').replace(/\.$/, '');+ const cleanedDomain = payload.domain.replace(/^\*\./, '').replace(/\.$/, '');const baseDomain = getDomain(cleanedDomain) || cleanedDomain;- const forwardedFor = headers().get('x-forwarded-for');- const ip = (forwardedFor ?? '127.0.0.1').split(',')[0];-- bigquery+ await bigquery.insertRows({datasetName: process.env.BIGQUERY_DATASET!,tableName: 'lookups',rows: [{- domain: data.domain,+ domain: payload.domain,baseDomain,timestamp: Math.floor(new Date().getTime() / 1000),- ip,- isBot: data.isBot,+ ip: payload.ip,+ isBot: payload.isBot,},],})