diff --git a/backend/database.js b/backend/database.js new file mode 100644 index 0000000..3fccacb --- /dev/null +++ b/backend/database.js @@ -0,0 +1,63 @@ +const mysql = require('mysql2'); + +const conexion = mysql.createConnection({ + host: 'localhost', + port: 3308, + user: 'root', + password: '', // mejor poner contraseña + database: 'openfi' +}); + +function readData() { + conexion.connect(err => { + if (err) { + console.error('Error conexión:', err.message); + process.exit(1); + } + console.log('Conexión OK con mysql2'); + }); + + + const usuario = "SELECT * FROM devices"; + conexion.query(usuario,function(error,rows){ + if(error){ + throw error; + } + else{ + console.log(rows) + } + }); + + setTimeout(() => conexion.end(), 100); +} + + +function addValue(name, model) { + // Conectar + conexion.connect(err => { + if (err) { + console.error('Error conexión:', err.message); + process.exit(1); + } + console.log('Conexión OK con mysql2'); + }); + + // Añadir dato + conexion.query("SELECT MAX(id) AS maxId FROM devices", function(error, rows) { + if (error) throw error; + + const nextId = (rows[0].maxId || 0) + 1; + + const nuevoreg = "INSERT INTO devices (id, name, model) VALUES (?, ?, ?)"; + conexion.query(nuevoreg, [nextId, name, model], function(error) { + if (error) throw error; + console.log('Datos registrados'); + }); + }); + + // Cerrar + setTimeout(() => conexion.end(), 100); +} + +addValue('Gateway', 'Ubiquiti Edgerouter 4'); +readData(); \ No newline at end of file diff --git a/backend/server.js b/backend/server.js index 56c71d5..e28517e 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,4 +1,5 @@ const express = require("express"); +const crypto = require('crypto'); const fs = require("fs"); const path = require("path"); @@ -8,10 +9,32 @@ const PORT = process.env.PORT; app.use(express.json()); app.use(express.static(path.join(__dirname, "../public"))); +// Control de JSON +app.get("/api/devices", (req, res) => { + const data = fs.readFileSync("./server/data.json", "utf-8"); + res.json(JSON.parse(data).usuarios); +}); +app.post("/api/devices", (req, res) => { + const nuevoUsuario = req.body; + + const data = JSON.parse(fs.readFileSync("./server/data.json", "utf-8")); + data.usuarios.push(nuevoUsuario); + fs.writeFileSync("./server/data.json", JSON.stringify(data, null, 2)); + res.json({ mensaje: "Usuario guardado" }); +}); +// Generar hash +app.post('/api/hash', (req, res) => { + const { text } = req.body; + if (!text) return res.status(400).json({ error: "no hay valor" }); + + const hash = crypto.createHash('sha256').update(text).digest('hex'); + res.json({ hash }); +}); app.listen(PORT, () => { console.log(`Servidor corriendo en http://localhost:${PORT}`); + console.log('ready'); }); diff --git a/data/settings.json b/data/settings.json new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index b89f0a7..63cbad3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "express": "^5.2.1" + "express": "^5.2.1", + "mysql2": "^3.16.0" } }, "node_modules/accepts": { @@ -25,6 +26,15 @@ "node": ">= 0.6" } }, + "node_modules/aws-ssl-profiles": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz", + "integrity": "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==", + "license": "MIT", + "engines": { + "node": ">= 6.0.0" + } + }, "node_modules/body-parser": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.2.tgz", @@ -144,6 +154,15 @@ } } }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -318,6 +337,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "license": "MIT", + "dependencies": { + "is-property": "^1.0.2" + } + }, "node_modules/get-intrinsic": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", @@ -448,6 +476,33 @@ "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", "license": "MIT" }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==", + "license": "MIT" + }, + "node_modules/long": { + "version": "5.3.2", + "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz", + "integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==", + "license": "Apache-2.0" + }, + "node_modules/lru.min": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/lru.min/-/lru.min-1.1.3.tgz", + "integrity": "sha512-Lkk/vx6ak3rYkRR0Nhu4lFUT2VDnQSxBe8Hbl7f36358p6ow8Bnvr8lrLt98H8J1aGxfhbX4Fs5tYg2+FTwr5Q==", + "license": "MIT", + "engines": { + "bun": ">=1.0.0", + "deno": ">=1.30.0", + "node": ">=8.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wellwelwel" + } + }, "node_modules/math-intrinsics": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", @@ -509,6 +564,38 @@ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "license": "MIT" }, + "node_modules/mysql2": { + "version": "3.16.0", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.16.0.tgz", + "integrity": "sha512-AEGW7QLLSuSnjCS4pk3EIqOmogegmze9h8EyrndavUQnIUcfkVal/sK7QznE+a3bc6rzPbAiui9Jcb+96tPwYA==", + "license": "MIT", + "dependencies": { + "aws-ssl-profiles": "^1.1.1", + "denque": "^2.1.0", + "generate-function": "^2.3.1", + "iconv-lite": "^0.7.0", + "long": "^5.2.1", + "lru.min": "^1.0.0", + "named-placeholders": "^1.1.3", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/named-placeholders": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.6.tgz", + "integrity": "sha512-Tz09sEL2EEuv5fFowm419c1+a/jSMiBjI9gHxVLrVdbUkkNUUfjsVYs9pVZu5oCon/kmRh9TfLEObFtkVxmY0w==", + "license": "MIT", + "dependencies": { + "lru.min": "^1.1.0" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/negotiator": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz", @@ -670,6 +757,11 @@ "url": "https://opencollective.com/express" } }, + "node_modules/seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==" + }, "node_modules/serve-static": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", @@ -767,6 +859,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sqlstring": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz", + "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/statuses": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", diff --git a/package.json b/package.json index eafd48d..b4bdcd9 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "homepage": "https://github.com/Coll147/OpenFi#readme", "dependencies": { - "express": "^5.2.1" + "express": "^5.2.1", + "mysql2": "^3.16.0" } } diff --git a/public/scripts/actions.js b/public/scripts/actions.js index b4bb780..e6aef6f 100644 --- a/public/scripts/actions.js +++ b/public/scripts/actions.js @@ -97,18 +97,13 @@ function parseDate(str) { // SHA-256 async function sha256(text) { - // Convertir el texto a bytes - const encoder = new TextEncoder() - const data = encoder.encode(text) - - // Calcular hash - const hashBuffer = await crypto.subtle.digest('SHA-256', data) - - // Convertir bytes a hex - const hashArray = Array.from(new Uint8Array(hashBuffer)) - const hashHex = hashArray - .map(b => b.toString(16).padStart(2, '0')) - .join('') - - return hashHex + const res = await fetch('/api/hash', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text }) + }); + + const data = await res.json(); + console.log(data.hash) + return data.hash } \ No newline at end of file