From 7636262962f653e65f7dbc9243ee2ba2798ed7eb Mon Sep 17 00:00:00 2001 From: Coll147 Date: Sun, 11 Jan 2026 14:39:55 +0100 Subject: [PATCH 1/6] trying to understand express --- backend/server.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/server.js b/backend/server.js index 56c71d5..e7dd8ed 100644 --- a/backend/server.js +++ b/backend/server.js @@ -9,7 +9,22 @@ app.use(express.json()); app.use(express.static(path.join(__dirname, "../public"))); +// Ruta para obtener datos +app.get("/api/devices", (req, res) => { + const data = fs.readFileSync("./server/data.json", "utf-8"); + res.json(JSON.parse(data).usuarios); +}); + +// Ruta para guardar datos +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" }); +}); app.listen(PORT, () => { From becc473776926ee50865caadcc169ccb01807487 Mon Sep 17 00:00:00 2001 From: Coll147 Date: Sun, 11 Jan 2026 20:31:41 +0100 Subject: [PATCH 2/6] ready message --- backend/server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/server.js b/backend/server.js index e7dd8ed..d751a4b 100644 --- a/backend/server.js +++ b/backend/server.js @@ -29,4 +29,5 @@ app.post("/api/devices", (req, res) => { app.listen(PORT, () => { console.log(`Servidor corriendo en http://localhost:${PORT}`); + console.log('ready'); }); From e02ab3e6938b301bfb30c11ffcf8d6ae7c9e821e Mon Sep 17 00:00:00 2001 From: Coll147 Date: Wed, 14 Jan 2026 10:11:02 +0100 Subject: [PATCH 3/6] =?UTF-8?q?a=C3=B1adir=20mysql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/database.js | 48 +++++++++++++++++++++ data/settings.json | 0 package-lock.json | 103 +++++++++++++++++++++++++++++++++++++++++++- package.json | 3 +- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 backend/database.js create mode 100644 data/settings.json diff --git a/backend/database.js b/backend/database.js new file mode 100644 index 0000000..9b8bc54 --- /dev/null +++ b/backend/database.js @@ -0,0 +1,48 @@ +const mysql = require('mysql2'); + +const conexion = mysql.createConnection({ + host: 'localhost', + port: 3308, + user: 'root', + password: '', // mejor poner contraseña + database: 'openfi' +}); + +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) + } +}); + + +function addValue(name, model) { + 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'); + }); + }); +} + +addValue('Gateway', 'Ubiquiti Edgerouter 4'); + + +setTimeout(() => conexion.end(), 100); \ No newline at end of file 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" } } From 9d5607006df14502027df4e2cda587095270bb0a Mon Sep 17 00:00:00 2001 From: Coll147 Date: Wed, 14 Jan 2026 10:15:51 +0100 Subject: [PATCH 4/6] usar funciones porque molan --- backend/database.js | 53 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/backend/database.js b/backend/database.js index 9b8bc54..3fccacb 100644 --- a/backend/database.js +++ b/backend/database.js @@ -8,27 +8,41 @@ const conexion = mysql.createConnection({ database: 'openfi' }); -conexion.connect(err => { -if (err) { - console.error('Error conexión:', err.message); - process.exit(1); -} - console.log('Conexión OK con mysql2'); -}); +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) - } -}); + 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; @@ -40,9 +54,10 @@ function addValue(name, model) { console.log('Datos registrados'); }); }); + + // Cerrar + setTimeout(() => conexion.end(), 100); } addValue('Gateway', 'Ubiquiti Edgerouter 4'); - - -setTimeout(() => conexion.end(), 100); \ No newline at end of file +readData(); \ No newline at end of file From 29dfc63db942fbef599c777a9a8889c2d0472c40 Mon Sep 17 00:00:00 2001 From: Coll147 Date: Wed, 14 Jan 2026 10:32:15 +0100 Subject: [PATCH 5/6] use crypto library nodejs --- backend/server.js | 15 +++++++++++---- public/scripts/actions.js | 19 +++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/backend/server.js b/backend/server.js index d751a4b..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,14 +9,11 @@ const PORT = process.env.PORT; app.use(express.json()); app.use(express.static(path.join(__dirname, "../public"))); - -// Ruta para obtener datos +// Control de JSON app.get("/api/devices", (req, res) => { const data = fs.readFileSync("./server/data.json", "utf-8"); res.json(JSON.parse(data).usuarios); }); - -// Ruta para guardar datos app.post("/api/devices", (req, res) => { const nuevoUsuario = req.body; @@ -26,6 +24,15 @@ app.post("/api/devices", (req, res) => { 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}`); diff --git a/public/scripts/actions.js b/public/scripts/actions.js index b4bb780..47f6462 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) + const res = await fetch('/api/hash', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text }) + }); - // Calcular hash - const hashBuffer = await crypto.subtle.digest('SHA-256', data) + const data = await res.json(); - // 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 + document.getElementById('out').innerText = data.hash; } \ No newline at end of file From 75991636e2cfb15d22db56f0b32c129006b2532f Mon Sep 17 00:00:00 2001 From: Coll147 Date: Wed, 14 Jan 2026 10:41:31 +0100 Subject: [PATCH 6/6] use crypto library for hashing --- public/scripts/actions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/scripts/actions.js b/public/scripts/actions.js index 47f6462..e6aef6f 100644 --- a/public/scripts/actions.js +++ b/public/scripts/actions.js @@ -104,6 +104,6 @@ async function sha256(text) { }); const data = await res.json(); - - document.getElementById('out').innerText = data.hash; + console.log(data.hash) + return data.hash } \ No newline at end of file