Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-12-05 23:40:05 +01:00
padre ee2aaccffe
commit bb234fef1e
Se han modificado 6 ficheros con 59 adiciones y 37 borrados

Ver fichero

@@ -1,6 +1,16 @@
import { NextRequest, NextResponse } from 'next/server';
import { esClient, INDEX_NAME, initializeIndex } from '@/lib/elasticsearch';
import { generateHashes, detectHashType, isHash } from '@/lib/hash';
import { generateHashes, detectHashType } from '@/lib/hash';
interface HashDocument {
plaintext: string;
md5: string;
sha1: string;
sha256: string;
sha512: string;
bcrypt: string;
created_at?: string;
}
export async function POST(request: NextRequest) {
try {
@@ -30,7 +40,7 @@ export async function POST(request: NextRequest) {
if (hashType) {
// Query is a hash - search for it in Elasticsearch
const searchResponse = await esClient.search({
const searchResponse = await esClient.search<HashDocument>({
index: INDEX_NAME,
query: {
term: {
@@ -47,16 +57,19 @@ export async function POST(request: NextRequest) {
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,
}
}))
results: hits.map((hit) => {
const source = hit._source!;
return {
plaintext: source.plaintext,
hashes: {
md5: source.md5,
sha1: source.sha1,
sha256: source.sha256,
sha512: source.sha512,
bcrypt: source.bcrypt,
}
};
})
});
} else {
// Hash not found in database
@@ -69,20 +82,20 @@ export async function POST(request: NextRequest) {
}
} else {
// Query is plaintext - check if it already exists first
const existsResponse = await esClient.search({
const existsResponse = await esClient.search<HashDocument>({
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;
const existingDoc = existsResponse.hits.hits[0]._source!;
hashes = {
md5: existingDoc.md5,
sha1: existingDoc.sha1,
@@ -94,7 +107,7 @@ export async function POST(request: NextRequest) {
// Plaintext not found, generate hashes and check if any hash already exists
hashes = await generateHashes(cleanQuery);
const hashExistsResponse = await esClient.search({
const hashExistsResponse = await esClient.search<HashDocument>({
index: INDEX_NAME,
query: {
bool: {
@@ -107,7 +120,7 @@ export async function POST(request: NextRequest) {
minimum_should_match: 1
}
}
} as any);
});
if (hashExistsResponse.hits.hits.length === 0) {
// No duplicates found, insert new document

Ver fichero

@@ -58,7 +58,7 @@ export default function Home() {
const data = await response.json();
setResult(data);
} catch (err) {
} catch (_err) {
setError('Failed to perform search. Please check your connection.');
} finally {
setLoading(false);