mirror of
https://github.com/andrey271192/Domain_web.git
synced 2026-09-20 14:41:58 +00:00
feat: rebrand to Domain Scanner platform
Replace GeoExport static site with Next.js domain intelligence app: real DNS/WHOIS/SSL/HTTP/geo scans, SSE progress, Docker stack, updated install/uninstall scripts, and full documentation. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
14
src/app/api/scan/[id]/route.ts
Normal file
14
src/app/api/scan/[id]/route.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id } = await params;
|
||||
const scan = await prisma.scan.findUnique({ where: { id } });
|
||||
if (!scan) {
|
||||
return NextResponse.json({ error: "Scan not found" }, { status: 404 });
|
||||
}
|
||||
return NextResponse.json(scan);
|
||||
}
|
||||
65
src/app/api/scan/[id]/stream/route.ts
Normal file
65
src/app/api/scan/[id]/stream/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(
|
||||
_req: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
const send = (data: object) => {
|
||||
controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
|
||||
};
|
||||
|
||||
let attempts = 0;
|
||||
const maxAttempts = 120;
|
||||
|
||||
const poll = async () => {
|
||||
const scan = await prisma.scan.findUnique({ where: { id } });
|
||||
if (!scan) {
|
||||
send({ error: "not_found" });
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
send({
|
||||
id: scan.id,
|
||||
status: scan.status,
|
||||
progress: scan.progress,
|
||||
domain: scan.domain,
|
||||
result: scan.status === "COMPLETED" ? scan.result : undefined,
|
||||
error: scan.error,
|
||||
});
|
||||
|
||||
if (scan.status === "COMPLETED" || scan.status === "FAILED") {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
send({ error: "timeout" });
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(poll, 1000);
|
||||
};
|
||||
|
||||
await poll();
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"Content-Type": "text/event-stream",
|
||||
"Cache-Control": "no-cache",
|
||||
Connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
}
|
||||
98
src/app/api/scan/route.ts
Normal file
98
src/app/api/scan/route.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { checkRateLimit } from "@/lib/rate-limit";
|
||||
import { normalizeDomain, isValidDomain } from "@/lib/utils";
|
||||
import { runDomainScan } from "@/lib/scanner";
|
||||
import { cacheGet, cacheSet } from "@/lib/redis";
|
||||
|
||||
const bodySchema = z.object({
|
||||
domain: z.string().min(1).max(253),
|
||||
});
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? "anonymous";
|
||||
const rate = await checkRateLimit(`scan:${ip}`);
|
||||
if (!rate.ok) {
|
||||
return NextResponse.json({ error: "Rate limit exceeded" }, { status: 429 });
|
||||
}
|
||||
|
||||
let body: z.infer<typeof bodySchema>;
|
||||
try {
|
||||
body = bodySchema.parse(await req.json());
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const domain = normalizeDomain(body.domain);
|
||||
if (!isValidDomain(domain)) {
|
||||
return NextResponse.json({ error: "Invalid domain name" }, { status: 400 });
|
||||
}
|
||||
|
||||
const cacheKey = `scan:result:${domain}`;
|
||||
const cached = await cacheGet<object>(cacheKey);
|
||||
if (cached) {
|
||||
const scan = await prisma.scan.create({
|
||||
data: {
|
||||
domain,
|
||||
status: "COMPLETED",
|
||||
progress: 100,
|
||||
result: cached,
|
||||
ip,
|
||||
userAgent: req.headers.get("user-agent") ?? undefined,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ id: scan.id, domain, cached: true });
|
||||
}
|
||||
|
||||
const scan = await prisma.scan.create({
|
||||
data: {
|
||||
domain,
|
||||
status: "RUNNING",
|
||||
progress: 0,
|
||||
ip,
|
||||
userAgent: req.headers.get("user-agent") ?? undefined,
|
||||
},
|
||||
});
|
||||
|
||||
runScanAsync(scan.id, domain, cacheKey);
|
||||
|
||||
return NextResponse.json({ id: scan.id, domain });
|
||||
}
|
||||
|
||||
async function runScanAsync(scanId: string, domain: string, cacheKey: string) {
|
||||
try {
|
||||
const result = await runDomainScan(domain, async (progress) => {
|
||||
await prisma.scan.update({ where: { id: scanId }, data: { progress } });
|
||||
});
|
||||
await prisma.scan.update({
|
||||
where: { id: scanId },
|
||||
data: { status: "COMPLETED", progress: 100, result: result as object },
|
||||
});
|
||||
await cacheSet(cacheKey, result, 3600);
|
||||
} catch (e) {
|
||||
await prisma.scan.update({
|
||||
where: { id: scanId },
|
||||
data: {
|
||||
status: "FAILED",
|
||||
error: e instanceof Error ? e.message : "Scan failed",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const limit = Math.min(Number(req.nextUrl.searchParams.get("limit") ?? 20), 50);
|
||||
const scans = await prisma.scan.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: limit,
|
||||
select: {
|
||||
id: true,
|
||||
domain: true,
|
||||
status: true,
|
||||
progress: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ scans });
|
||||
}
|
||||
Reference in New Issue
Block a user