import { NextRequest, NextResponse } from 'next/server'; import { esClient, INDEX_NAME, initializeIndex } from '@/lib/elasticsearch'; import { generateHashes, detectHashType, isHash } from '@/lib/hash'; export async function POST(request: NextRequest) { try { const { query } = await request.json(); if (!query || typeof query !== 'string') { return NextResponse.json( { error: 'Query parameter is required' }, { status: 400 } ); } // Ensure index exists await initializeIndex(); const cleanQuery = query.trim().split(/\s+/)[0]; if (!cleanQuery) { return NextResponse.json( { error: 'Invalid query: only whitespace provided' }, { status: 400 } ); } const cleanQueryLower = cleanQuery.toLowerCase(); const hashType = detectHashType(cleanQueryLower); if (hashType) { // Query is a hash - search for it in Elasticsearch const searchResponse = await esClient.search({ index: INDEX_NAME, query: { term: { [hashType]: hashType === 'bcrypt' ? cleanQuery : cleanQueryLower } } }); const hits = searchResponse.hits.hits; if (hits.length > 0) { // Found matching plaintext return NextResponse.json({ found: true, hashType, hash: cleanQuery, results: hits.map((hit: any) => ({ plaintext: hit._source.plaintext, hashes: { md5: hit._source.md5, sha1: hit._source.sha1, sha256: hit._source.sha256, sha512: hit._source.sha512, bcrypt: hit._source.bcrypt, } })) }); } else { // Hash not found in database return NextResponse.json({ found: false, hashType, hash: cleanQuery, message: 'Hash not found in database' }); } } else { // Query is plaintext - check if it already exists first const existsResponse = await esClient.search({ index: INDEX_NAME, query: { term: { 'plaintext.keyword': cleanQuery } } } as any); let hashes; if (existsResponse.hits.hits.length > 0) { // Plaintext found, retrieve existing hashes const existingDoc = existsResponse.hits.hits[0]._source as any; hashes = { md5: existingDoc.md5, sha1: existingDoc.sha1, sha256: existingDoc.sha256, sha512: existingDoc.sha512, bcrypt: existingDoc.bcrypt, }; } else { // Plaintext not found, generate hashes and check if any hash already exists hashes = await generateHashes(cleanQuery); const hashExistsResponse = await esClient.search({ index: INDEX_NAME, query: { bool: { should: [ { term: { md5: hashes.md5 } }, { term: { sha1: hashes.sha1 } }, { term: { sha256: hashes.sha256 } }, { term: { sha512: hashes.sha512 } }, ], minimum_should_match: 1 } } } as any); if (hashExistsResponse.hits.hits.length === 0) { // No duplicates found, insert new document await esClient.index({ index: INDEX_NAME, document: { ...hashes, created_at: new Date().toISOString() } }); // Refresh index to make the document searchable immediately await esClient.indices.refresh({ index: INDEX_NAME }); } } return NextResponse.json({ found: true, isPlaintext: true, plaintext: cleanQuery, wasGenerated: existsResponse.hits.hits.length === 0, hashes: { md5: hashes.md5, sha1: hashes.sha1, sha256: hashes.sha256, sha512: hashes.sha512, bcrypt: hashes.bcrypt, } }); } } catch (error) { console.error('Search error:', error); return NextResponse.json( { error: 'Internal server error', details: error instanceof Error ? error.message : 'Unknown error' }, { status: 500 } ); } }