'use strict'; const ChildProcess = require('childprocess'); let vBoxManageBinary,…
- User
'use strict';
const ChildProcess = require('child_process'); let vBoxManageBinary, escapeArg, isOptionSafe;
// Système d'exploitation hôte if (/^win/.test(process.platform)) { const vBoxInstallPath = process.env.VBOX_INSTALL_PATH || process.env.VBOX_MSI_INSTALL_PATH;
if (vBoxInstallPath) { vBoxManageBinary = '"' + vBoxInstallPath.replace(/\\$/, '') + '\\VBoxManage.exe' + '"'; } else { console.warn('La variable d\'environnement VBOX_INSTALL_PATH ou VBOX_MSI_INSTALL_PATH n\'est pas définie.'); vBoxManageBinary = 'VBoxManage.exe'; } escapeArg = arg => { if (!/\s|[\\"&]/.test(arg)) return arg; return '"' + arg.replace(/"/g, '"""') + '"'; }; isOptionSafe = opt => { return !/\s|[\\"&]/.test(opt); };} else { vBoxManageBinary = 'vboxmanage';
escapeArg = arg => { return arg.replace(/([ \t\\|;&"`$*])/g, '\\$1'); }; isOptionSafe = opt => { return !/([ \t\\|;&"`$*])/.test(opt); };}
const VBoxManage = {};
VBoxManage.manage = function(command, options) { command = command || []; if (!Array.isArray(command)) { command = [command]; }
options = options || {}; for (let i = 0; i < command.length; i++) { command[i] = escapeArg(command[i]); } for (const [option, value] of Object.entries(options)) { if (!isOptionSafe(option)) throw new Error('Une option non sécurisée a été transmise à VBoxManage.manage : ' + option); command.push('--' + option); if (value !== true) { command.push(escapeArg(value)); } } return new Promise((resolve, reject) => { ChildProcess.exec(vBoxManageBinary + ' ' + command.join(' '), {}, (err, stdout, stderr) => { if (err) { err.stderr = stderr; return reject(err); } resolve({ stdout, stderr }); }); });};
// Exécutez votre serveur ici pour écouter les demandes const express = require('express'); const app = express(); const PORT = 3000;
// Middleware pour gérer les requêtes app.use(express.json());
app.post('/start-vm', (req, res) => { const vmName = req.body.vmName;
VBoxManage.manage(['startvm', vmName, '--type', 'headless']) .then(result => { res.json({ message: `VM démarrée: ${result.stdout}` }); }) .catch(error => { res.status(500).json({ error: error.stderr }); });});
app.listen(PORT, () => { console.log(
Serveur démarré sur http://localhost:${PORT}); });<script> document.getElementById('startVM').addEventListener('click', () => { const vmName = document.getElementById('vmName').value; fetch('/start-vm', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ vmName }), }) .then(response => response.json()) .then(data => { alert(data.message); }) .catch(error => { alert('Erreur: ' + error); }); }); </script> - Assistant
- User
traduit
- Assistant
- User
regenere le code avec les nouvelle modification
- Assistant
- User
quand jecrit le nom de la vm sa ecrit Erreur: TypeError: Failed to fetch
- Assistant
- User
regeneremoi tous le code avec les nouvellee modification
- Assistant
- User
regenere moi avec les nouvelle modification
- Assistant
- User
Erreur: SyntaxError: Failed to execute 'json' on 'Response': Unexpected end of JSON input
- Assistant
- User
[Thu Feb 13 2025 23:26:41 GMT+0000 (temps universel coordonné)] "POST /start-vm" Error (404): "Not found"
- Assistant