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);

Ver fichero

@@ -16,7 +16,7 @@ export const INDEX_MAPPING = {
analysis: {
analyzer: {
lowercase_analyzer: {
type: 'custom',
type: 'custom' as const,
tokenizer: 'keyword',
filter: ['lowercase']
}
@@ -26,31 +26,31 @@ export const INDEX_MAPPING = {
mappings: {
properties: {
plaintext: {
type: 'text',
type: 'text' as const,
analyzer: 'lowercase_analyzer',
fields: {
keyword: {
type: 'keyword'
type: 'keyword' as const
}
}
},
md5: {
type: 'keyword'
type: 'keyword' as const
},
sha1: {
type: 'keyword'
type: 'keyword' as const
},
sha256: {
type: 'keyword'
type: 'keyword' as const
},
sha512: {
type: 'keyword'
type: 'keyword' as const
},
bcrypt: {
type: 'keyword'
type: 'keyword' as const
},
created_at: {
type: 'date'
type: 'date' as const
}
}
}
@@ -63,8 +63,9 @@ export async function initializeIndex(): Promise<void> {
if (!indexExists) {
await esClient.indices.create({
index: INDEX_NAME,
...INDEX_MAPPING
} as any);
settings: INDEX_MAPPING.settings,
mappings: INDEX_MAPPING.mappings
});
console.log(`Index '${INDEX_NAME}' created successfully with 10 shards`);
} else {
console.log(`Index '${INDEX_NAME}' already exists`);

Ver fichero

@@ -73,7 +73,7 @@ export function isHash(input: string): boolean {
export async function verifyBcrypt(plaintext: string, hash: string): Promise<boolean> {
try {
return await bcrypt.compare(plaintext, hash);
} catch (error) {
} catch (_error) {
return false;
}
}

Ver fichero

@@ -41,9 +41,9 @@
"@types/bcrypt": "^6.0.0",
"bcrypt": "^6.0.0",
"lucide-react": "^0.555.0",
"next": "16.0.7",
"react": "19.2.0",
"react-dom": "19.2.0",
"next": "15.4.8",
"react": "19.1.2",
"react-dom": "19.1.2",
"tsx": "^4.21.0"
},
"devDependencies": {

Ver fichero

@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
@@ -11,7 +15,7 @@
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"jsx": "preserve",
"incremental": true,
"plugins": [
{
@@ -19,7 +23,9 @@
}
],
"paths": {
"@/*": ["./*"]
"@/*": [
"./*"
]
}
},
"include": [
@@ -30,5 +36,7 @@
".next/dev/types/**/*.ts",
"**/*.mts"
],
"exclude": ["node_modules"]
"exclude": [
"node_modules"
]
}