-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawswaf_classification.js
More file actions
103 lines (86 loc) · 3.81 KB
/
awswaf_classification.js
File metadata and controls
103 lines (86 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dotenv/config';
import axios from 'axios';
import { readFileSync } from 'fs';
// Configuracion de la API de CapSolver
const CAPSOLVER_API_KEY = process.env.CAPSOLVER_API_KEY;
const CREATE_TASK_URL = 'https://api.capsolver.com/createTask';
// Tipos de preguntas para clasificacion AWS WAF
// Preguntas tipo cuadricula (soporta hasta 9 imagenes)
const GRID_QUESTIONS = [
'aws:grid:bed', // cama
'aws:grid:bag', // bolsa
'aws:grid:hat', // sombrero
'aws:grid:chair', // silla
'aws:grid:bucket', // cubo
'aws:grid:curtain', // cortina
'aws:grid:mop', // fregona
'aws:grid:clock', // reloj
'aws:grid:suitcase', // maleta
'aws:grid:binocular', // binoculares
'aws:grid:cooking pot' // olla
];
// Tipo ciudad de coche de juguete (soporta 1 imagen, marca el punto final del camino del coche)
const TOYCARCITY_QUESTIONS = [
'aws:toycarcity:carcity'
];
/**
* Clasificar desafios de imagenes de AWS WAF.
* @param {Object} options - Objeto de opciones
* @param {string[]} options.images - Lista de imagenes codificadas en base64 (hasta 9 para cuadricula, 1 para toycarcity)
* @param {string} options.question - Tipo de pregunta (ver GRID_QUESTIONS o TOYCARCITY_QUESTIONS)
* @returns {Promise<Object>} Solucion conteniendo objetos (cuadricula) o coordenadas de caja (toycarcity)
*/
async function solveAwsWafClassification({ images, question }) {
if (!images || images.length === 0) {
throw new Error('Se debe proporcionar al menos una imagen');
}
const payload = {
clientKey: CAPSOLVER_API_KEY,
task: {
type: 'AwsWafClassification',
images: Array.isArray(images) ? images : [images],
question: question
}
};
// AwsWafClassification devuelve resultado directamente (no necesita polling)
const response = await axios.post(CREATE_TASK_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Error al resolver: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Funcion auxiliar para cargar multiples imagenes como base64.
* @param {string[]} imagePaths - Array de rutas de archivos
* @returns {string[]} Array de imagenes codificadas en base64
*/
function loadImagesAsBase64(imagePaths) {
return imagePaths.map(path => {
const imageBuffer = readFileSync(path);
return imageBuffer.toString('base64');
});
}
async function main() {
if (!CAPSOLVER_API_KEY) {
console.log('Error: CAPSOLVER_API_KEY no encontrada en el archivo .env');
console.log('Por favor, crea un archivo .env con tu clave API:');
console.log('CAPSOLVER_API_KEY=tu_clave_api_aqui');
return;
}
console.log('Clasificacion de Imagenes AWS WAF');
console.log('\nPreguntas de Cuadricula (soporta hasta 9 imagenes):');
GRID_QUESTIONS.forEach(q => console.log(` ${q}`));
console.log('\nPreguntas de Ciudad de Coche de Juguete (1 imagen):');
TOYCARCITY_QUESTIONS.forEach(q => console.log(` ${q}`));
console.log('\nEjemplo de uso:');
console.log(' // Para desafios de cuadricula:');
console.log(' const images = loadImagesAsBase64(["img1.png", "img2.png", ...]);');
console.log(' const solution = await solveAwsWafClassification({ images, question: "aws:grid:chair" });');
console.log(' // Devuelve: { objects: [0, 2, 5] } // indices de imagenes coincidentes');
console.log('\n // Para ciudad de coche de juguete:');
console.log(' const solution = await solveAwsWafClassification({ images: [imagenBase64], question: "aws:toycarcity:carcity" });');
console.log(' // Devuelve: { box: [x, y] } // coordenadas del punto final');
}
main();
export { solveAwsWafClassification, loadImagesAsBase64, GRID_QUESTIONS, TOYCARCITY_QUESTIONS };