Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions backend/database.js
Original file line number Diff line number Diff line change
@@ -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();
23 changes: 23 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require("express");
const crypto = require('crypto');
const fs = require("fs");
const path = require("path");

Expand All @@ -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');
});
Empty file added data/settings.json
Empty file.
103 changes: 102 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"homepage": "https://github.com/Coll147/OpenFi#readme",
"dependencies": {
"express": "^5.2.1"
"express": "^5.2.1",
"mysql2": "^3.16.0"
}
}
23 changes: 9 additions & 14 deletions public/scripts/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}