├── .gitignore ├── .env.sample ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | node_modules -------------------------------------------------------------------------------- /.env.sample: -------------------------------------------------------------------------------- 1 | PORT=8000 2 | SECURITY_KEY=your_security_key_here -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "control_host_api", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "start": "node src/index.js" 7 | }, 8 | "keywords": [], 9 | "author": "", 10 | "license": "ISC", 11 | "description": "", 12 | "dependencies": { 13 | "cors": "^2.8.5", 14 | "dotenv": "^17.2.2", 15 | "express": "^5.1.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const express = require('express'); 3 | const { exec } = require('child_process'); 4 | const cors = require('cors'); 5 | 6 | const app = express(); 7 | // const PORT = 3000; 8 | const PORT = process.env.PORT || 8000; 9 | 10 | // Security key - should be changed to a strong key in production and obtained via environment variables 11 | const SECURITY_KEY = process.env.SECURITY_KEY || 'your_secure_key'; 12 | 13 | // Middleware 14 | app.use(cors()); 15 | app.use(express.json()); 16 | 17 | // Validation middleware 18 | const validateRequest = (req, res, next) => { 19 | const { key } = req.body; 20 | 21 | if (!key || key !== SECURITY_KEY) { 22 | return res.status(403).json({ 23 | success: false, 24 | message: 'Unauthorized access: Invalid security key' 25 | }); 26 | } 27 | 28 | next(); 29 | }; 30 | 31 | // Shutdown API endpoint 32 | app.post('/shutdown', validateRequest, (req, res) => { 33 | // Log the request 34 | console.log(`Received shutdown request from ${req.ip} at ${new Date().toISOString()}`); 35 | 36 | // Execute Ubuntu shutdown command 37 | // -h now means shutdown immediately 38 | exec('sudo shutdown -h now', (error, stdout, stderr) => { 39 | if (error) { 40 | console.error(`Execution error: ${error.message}`); 41 | return res.status(500).json({ 42 | success: false, 43 | message: 'Failed to execute shutdown command', 44 | error: error.message 45 | }); 46 | } 47 | 48 | if (stderr) { 49 | console.error(`Command error output: ${stderr}`); 50 | return res.status(500).json({ 51 | success: false, 52 | message: 'Shutdown command returned an error', 53 | error: stderr 54 | }); 55 | } 56 | 57 | console.log(`Shutdown command executed: ${stdout}`); 58 | res.status(200).json({ 59 | success: true, 60 | message: 'Shutdown command received, system will shut down immediately' 61 | }); 62 | }); 63 | }); 64 | 65 | // Reboot API endpoint (additional feature) 66 | app.post('/reboot', validateRequest, (req, res) => { 67 | console.log(`Received reboot request from ${req.ip} at ${new Date().toISOString()}`); 68 | 69 | exec('sudo reboot', (error, stdout, stderr) => { 70 | if (error) { 71 | console.error(`Execution error: ${error.message}`); 72 | return res.status(500).json({ 73 | success: false, 74 | message: 'Failed to execute reboot command', 75 | error: error.message 76 | }); 77 | } 78 | 79 | if (stderr) { 80 | console.error(`Command error output: ${stderr}`); 81 | return res.status(500).json({ 82 | success: false, 83 | message: 'Reboot command returned an error', 84 | error: stderr 85 | }); 86 | } 87 | 88 | console.log(`Reboot command executed: ${stdout}`); 89 | res.status(200).json({ 90 | success: true, 91 | message: 'Reboot command received, system will reboot immediately' 92 | }); 93 | }); 94 | }); 95 | 96 | // Health check endpoint 97 | app.get('/health', (req, res) => { 98 | res.status(200).json({ 99 | success: true, 100 | message: 'Server is running normally', 101 | timestamp: new Date().toISOString() 102 | }); 103 | }); 104 | 105 | // Start the server 106 | app.listen(PORT, () => { 107 | console.log(`Shutdown control API server started, listening on port ${PORT}`); 108 | console.log(`Please ensure sudo permissions are properly configured`); 109 | }); 110 | --------------------------------------------------------------------------------