diff --git a/start-linux.js b/start-linux.js index 569af24..5995072 100644 --- a/start-linux.js +++ b/start-linux.js @@ -4,7 +4,31 @@ let makedisk = "qemu-img create vm.img 30G" // make a 30GB disk let qemucmd = "qemu-system-x86_64 -vnc :1 -m 1G -hda vm.img -accel kvm" child.execSync(makedisk) child.execSync(qemucmd) -http.createServer(function (req, res) { - res.write('Server running at IP 127.0.0.1:5901 (VNC)'); //write a response to the client - res.end(); //end the response -}).listen(8080); //the server object listens on port 8080 \ No newline at end of file +const server = http.createServer((req, res) => { + // Check if the request is a POST to /stop + if (req.method === 'POST' && req.url === '/stop') { + // Execute the pkill command to stop Node.js processes + exec('pkill qemu*', (error, stdout, stderr) => { + if (error) { + console.error(`Error executing command: ${error.message}`); + res.writeHead(500); + res.end(JSON.stringify({ message: 'Failed to stop the server' })); + return; + } + console.log('QEMU processes terminated'); + res.writeHead(200); + res.end(JSON.stringify({ message: 'Server is stopping' })); + }); + } else { + // Handle 404 for other routes + res.writeHead(404); + res.end(JSON.stringify({ message: 'Not Found' })); + } +}); + +// Start the server on port 3000 +const PORT = 8080; +server.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); + console.log("QEMU and FreeVM are running. VNC into it using 127.0.0.1 to VNC into it and go to localhost:8080/stop to stop the VM.") +}); \ No newline at end of file