@@ -1,6 +1,16 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { esClient, INDEX_NAME, initializeIndex } from '@/lib/elasticsearch';
|
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) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
@@ -30,7 +40,7 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
if (hashType) {
|
if (hashType) {
|
||||||
// Query is a hash - search for it in Elasticsearch
|
// Query is a hash - search for it in Elasticsearch
|
||||||
const searchResponse = await esClient.search({
|
const searchResponse = await esClient.search<HashDocument>({
|
||||||
index: INDEX_NAME,
|
index: INDEX_NAME,
|
||||||
query: {
|
query: {
|
||||||
term: {
|
term: {
|
||||||
@@ -47,16 +57,19 @@ export async function POST(request: NextRequest) {
|
|||||||
found: true,
|
found: true,
|
||||||
hashType,
|
hashType,
|
||||||
hash: cleanQuery,
|
hash: cleanQuery,
|
||||||
results: hits.map((hit: any) => ({
|
results: hits.map((hit) => {
|
||||||
plaintext: hit._source.plaintext,
|
const source = hit._source!;
|
||||||
|
return {
|
||||||
|
plaintext: source.plaintext,
|
||||||
hashes: {
|
hashes: {
|
||||||
md5: hit._source.md5,
|
md5: source.md5,
|
||||||
sha1: hit._source.sha1,
|
sha1: source.sha1,
|
||||||
sha256: hit._source.sha256,
|
sha256: source.sha256,
|
||||||
sha512: hit._source.sha512,
|
sha512: source.sha512,
|
||||||
bcrypt: hit._source.bcrypt,
|
bcrypt: source.bcrypt,
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
|
})
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Hash not found in database
|
// Hash not found in database
|
||||||
@@ -69,20 +82,20 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Query is plaintext - check if it already exists first
|
// Query is plaintext - check if it already exists first
|
||||||
const existsResponse = await esClient.search({
|
const existsResponse = await esClient.search<HashDocument>({
|
||||||
index: INDEX_NAME,
|
index: INDEX_NAME,
|
||||||
query: {
|
query: {
|
||||||
term: {
|
term: {
|
||||||
'plaintext.keyword': cleanQuery
|
'plaintext.keyword': cleanQuery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} as any);
|
});
|
||||||
|
|
||||||
let hashes;
|
let hashes;
|
||||||
|
|
||||||
if (existsResponse.hits.hits.length > 0) {
|
if (existsResponse.hits.hits.length > 0) {
|
||||||
// Plaintext found, retrieve existing hashes
|
// Plaintext found, retrieve existing hashes
|
||||||
const existingDoc = existsResponse.hits.hits[0]._source as any;
|
const existingDoc = existsResponse.hits.hits[0]._source!;
|
||||||
hashes = {
|
hashes = {
|
||||||
md5: existingDoc.md5,
|
md5: existingDoc.md5,
|
||||||
sha1: existingDoc.sha1,
|
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
|
// Plaintext not found, generate hashes and check if any hash already exists
|
||||||
hashes = await generateHashes(cleanQuery);
|
hashes = await generateHashes(cleanQuery);
|
||||||
|
|
||||||
const hashExistsResponse = await esClient.search({
|
const hashExistsResponse = await esClient.search<HashDocument>({
|
||||||
index: INDEX_NAME,
|
index: INDEX_NAME,
|
||||||
query: {
|
query: {
|
||||||
bool: {
|
bool: {
|
||||||
@@ -107,7 +120,7 @@ export async function POST(request: NextRequest) {
|
|||||||
minimum_should_match: 1
|
minimum_should_match: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} as any);
|
});
|
||||||
|
|
||||||
if (hashExistsResponse.hits.hits.length === 0) {
|
if (hashExistsResponse.hits.hits.length === 0) {
|
||||||
// No duplicates found, insert new document
|
// No duplicates found, insert new document
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ export default function Home() {
|
|||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setResult(data);
|
setResult(data);
|
||||||
} catch (err) {
|
} catch (_err) {
|
||||||
setError('Failed to perform search. Please check your connection.');
|
setError('Failed to perform search. Please check your connection.');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export const INDEX_MAPPING = {
|
|||||||
analysis: {
|
analysis: {
|
||||||
analyzer: {
|
analyzer: {
|
||||||
lowercase_analyzer: {
|
lowercase_analyzer: {
|
||||||
type: 'custom',
|
type: 'custom' as const,
|
||||||
tokenizer: 'keyword',
|
tokenizer: 'keyword',
|
||||||
filter: ['lowercase']
|
filter: ['lowercase']
|
||||||
}
|
}
|
||||||
@@ -26,31 +26,31 @@ export const INDEX_MAPPING = {
|
|||||||
mappings: {
|
mappings: {
|
||||||
properties: {
|
properties: {
|
||||||
plaintext: {
|
plaintext: {
|
||||||
type: 'text',
|
type: 'text' as const,
|
||||||
analyzer: 'lowercase_analyzer',
|
analyzer: 'lowercase_analyzer',
|
||||||
fields: {
|
fields: {
|
||||||
keyword: {
|
keyword: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
md5: {
|
md5: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
},
|
},
|
||||||
sha1: {
|
sha1: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
},
|
},
|
||||||
sha256: {
|
sha256: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
},
|
},
|
||||||
sha512: {
|
sha512: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
},
|
},
|
||||||
bcrypt: {
|
bcrypt: {
|
||||||
type: 'keyword'
|
type: 'keyword' as const
|
||||||
},
|
},
|
||||||
created_at: {
|
created_at: {
|
||||||
type: 'date'
|
type: 'date' as const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,8 +63,9 @@ export async function initializeIndex(): Promise<void> {
|
|||||||
if (!indexExists) {
|
if (!indexExists) {
|
||||||
await esClient.indices.create({
|
await esClient.indices.create({
|
||||||
index: INDEX_NAME,
|
index: INDEX_NAME,
|
||||||
...INDEX_MAPPING
|
settings: INDEX_MAPPING.settings,
|
||||||
} as any);
|
mappings: INDEX_MAPPING.mappings
|
||||||
|
});
|
||||||
console.log(`Index '${INDEX_NAME}' created successfully with 10 shards`);
|
console.log(`Index '${INDEX_NAME}' created successfully with 10 shards`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`Index '${INDEX_NAME}' already exists`);
|
console.log(`Index '${INDEX_NAME}' already exists`);
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export function isHash(input: string): boolean {
|
|||||||
export async function verifyBcrypt(plaintext: string, hash: string): Promise<boolean> {
|
export async function verifyBcrypt(plaintext: string, hash: string): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
return await bcrypt.compare(plaintext, hash);
|
return await bcrypt.compare(plaintext, hash);
|
||||||
} catch (error) {
|
} catch (_error) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,9 +41,9 @@
|
|||||||
"@types/bcrypt": "^6.0.0",
|
"@types/bcrypt": "^6.0.0",
|
||||||
"bcrypt": "^6.0.0",
|
"bcrypt": "^6.0.0",
|
||||||
"lucide-react": "^0.555.0",
|
"lucide-react": "^0.555.0",
|
||||||
"next": "16.0.7",
|
"next": "15.4.8",
|
||||||
"react": "19.2.0",
|
"react": "19.1.2",
|
||||||
"react-dom": "19.2.0",
|
"react-dom": "19.1.2",
|
||||||
"tsx": "^4.21.0"
|
"tsx": "^4.21.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2017",
|
"target": "ES2017",
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"esnext"
|
||||||
|
],
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
@@ -11,7 +15,7 @@
|
|||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"jsx": "react-jsx",
|
"jsx": "preserve",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"plugins": [
|
"plugins": [
|
||||||
{
|
{
|
||||||
@@ -19,7 +23,9 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./*"]
|
"@/*": [
|
||||||
|
"./*"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
@@ -30,5 +36,7 @@
|
|||||||
".next/dev/types/**/*.ts",
|
".next/dev/types/**/*.ts",
|
||||||
"**/*.mts"
|
"**/*.mts"
|
||||||
],
|
],
|
||||||
"exclude": ["node_modules"]
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Referencia en una nueva incidencia
Block a user