-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
80 lines (69 loc) · 2.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const { exec } = require("child_process");
const app = express();
const PORT = 3000; // You can change this to any port you prefer
const MAC_ADDRESS = "bc:e9:2f:fb:cb:ac"; // Replace this with your PC's MAC address
// Serve the index.html file
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
// Route to handle waking up the PC
app.get("/wake", (req, res) => {
exec(`wakeonlan ${MAC_ADDRESS}`, (error, stdout, stderr) => {
if (error) {
console.error("Error:", error);
res.status(500).send("Error waking up the PC");
return;
}
console.log("Magic packet sent successfully");
res.send("Magic packet sent successfully");
});
});
// Route to shut down the target computer
app.get("/shutdown", (req, res) => {
// const macAddress = req.query.mac;
const targetIP = "172.16.128.197";
if (!targetIP) {
return res
.status(404)
.send("Target computer not found for the given MAC address.");
}
// Use SSH to send the shutdown command
const command = `ssh yonno@${targetIP} 'shutdown /s /t 0'`; // Adjust for your OS
exec(command, (error, stdout, stderr) => {
if (error) {
console.error("Error:", error);
res.status(500).send(`Error shutting down the computer: ${stderr}`);
return;
}
console.log("Shutdown command sent successfully:", stdout);
res.send("Shutdown command sent successfully.");
});
});
// Route to put the target computer to sleep
app.get("/sleep", (req, res) => {
// const macAddress = req.query.mac;
const targetIP = "172.16.128.197";
if (!targetIP) {
return res
.status(404)
.send("Target computer not found for the given MAC address.");
}
// Use SSH to send the sleep command
const command = `ssh yonno@${targetIP} 'rundll32.exe powrprof.dll,SetSuspendState 0,1,0'`; // Adjust for your OS
exec(command, (error, stdout, stderr) => {
if (error) {
console.error("Error:", error);
res.status(500).send(`Error putting the computer to sleep: ${stderr}`);
return;
}
console.log("Sleep command sent successfully:", stdout);
res.send("Sleep command sent successfully.");
});
});
// Serve static files in the 'public' directory (CSS, JS, images, etc.)
app.use(express.static("public"));
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});