new redis migration

Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-12-15 16:35:35 +01:00
padre ad7a1cf0a7
commit 3ce64eeb8e
Se han modificado 20 ficheros con 1021 adiciones y 712 borrados

Ver fichero

@@ -1,17 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { esClient, INDEX_NAME, initializeIndex } from '@/lib/elasticsearch';
import { storeHashDocument, findByPlaintext, findByHash, initializeRedis } from '@/lib/redis';
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 {
const { query } = await request.json();
@@ -23,8 +13,8 @@ export async function POST(request: NextRequest) {
);
}
// Ensure index exists
await initializeIndex();
// Ensure Redis is connected
await initializeRedis();
const cleanQuery = query.trim().split(/\s+/)[0];
@@ -39,37 +29,24 @@ export async function POST(request: NextRequest) {
const hashType = detectHashType(cleanQueryLower);
if (hashType) {
// Query is a hash - search for it in Elasticsearch
const searchResponse = await esClient.search<HashDocument>({
index: INDEX_NAME,
query: {
term: {
[hashType]: hashType === 'bcrypt' ? cleanQuery : cleanQueryLower
}
}
});
// Query is a hash - search for it in Redis
const doc = await findByHash(hashType, cleanQueryLower);
const hits = searchResponse.hits.hits;
if (hits.length > 0) {
if (doc) {
// Found matching plaintext
return NextResponse.json({
found: true,
hashType,
hash: cleanQuery,
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,
}
};
})
results: [{
plaintext: doc.plaintext,
hashes: {
md5: doc.md5,
sha1: doc.sha1,
sha256: doc.sha256,
sha512: doc.sha512,
}
}]
});
} else {
// Hash not found in database
@@ -82,72 +59,41 @@ export async function POST(request: NextRequest) {
}
} else {
// Query is plaintext - check if it already exists first
const existsResponse = await esClient.search<HashDocument>({
index: INDEX_NAME,
query: {
term: {
'plaintext.keyword': cleanQuery
}
}
});
const existingDoc = await findByPlaintext(cleanQuery);
let hashes;
let wasGenerated = false;
if (existsResponse.hits.hits.length > 0) {
if (existingDoc) {
// Plaintext found, retrieve existing hashes
const existingDoc = existsResponse.hits.hits[0]._source!;
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
// Plaintext not found, generate and store hashes
hashes = await generateHashes(cleanQuery);
const hashExistsResponse = await esClient.search<HashDocument>({
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
}
}
await storeHashDocument({
...hashes,
created_at: new Date().toISOString()
});
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 });
}
wasGenerated = true;
}
return NextResponse.json({
found: true,
isPlaintext: true,
plaintext: cleanQuery,
wasGenerated: existsResponse.hits.hits.length === 0,
wasGenerated,
hashes: {
md5: hashes.md5,
sha1: hashes.sha1,
sha256: hashes.sha256,
sha512: hashes.sha512,
bcrypt: hashes.bcrypt,
}
});
}