From f1df0b94f078d7960a0170e7fdf5d9eca081a726 Mon Sep 17 00:00:00 2001 From: Raphix Date: Tue, 28 Nov 2023 20:04:33 +0100 Subject: [PATCH] Version 1.0.1 - Ajout de metrics --- main.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/main.js b/main.js index 7734384..a75ada1 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,7 @@ const fs = require('fs'); const os = require('os'); const osutils = require('os-utils'); const {statfs} = require('fs'); +const { exec } = require('child_process'); const server = http.createServer(async (req, res) => { const parsedUrl = url.parse(req.url, true); @@ -24,6 +25,36 @@ const server = http.createServer(async (req, res) => { res.statusCode = 404; res.end('Not Found'); } + + // Restart the machine + + if (parsedUrl.pathname === '/restart' && query.key) { + const config = JSON.parse(fs.readFileSync('config.json')); + if (query.key === config.AUTH_KEY) { + //Restart the machine + exec("sudo reboot", (error, stdout, stderr) => { + if (error) { + console.log(`error: ${error.message}`); + return; + } + if (stderr) { + console.log(`stderr: ${stderr}`); + return; + } + + console.log(`stdout: ${stdout}`); + }); + + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(metrics)); + } else { + res.statusCode = 401; + res.end('Unauthorized'); + } + } else { + res.statusCode = 404; + res.end('Not Found'); + } }); server.listen(4000, () => {