Reaperxxxx commited on
Commit
451349c
·
verified ·
1 Parent(s): d5287e7

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +15 -11
server.js CHANGED
@@ -74,33 +74,37 @@ app.post("/signup", async (req, res) => {
74
  });
75
 
76
  // Endpoint to handle terminal commands
77
- app.post("/execute", async (req, res) => {
78
  const { username, command } = req.body;
79
 
80
- if (!username || !command) {
81
- return res.status(400).send("Username and command are required.");
82
- }
83
-
84
  const userDir = path.join(usersDir, username);
85
- if (!await fs.access(userDir).catch(() => false)) {
86
- return res.status(400).send("User directory not found.");
87
  }
88
 
89
- const child = exec(command, { cwd: userDir, shell: "/bin/bash" });
 
 
 
 
 
 
90
 
91
  child.stdout.on("data", (data) => {
92
- appendLog(username, `STDOUT: ${data.trim()}`);
 
93
  });
94
 
95
  child.stderr.on("data", (data) => {
96
- appendLog(username, `STDERR: ${data.trim()}`);
 
97
  });
98
 
99
  child.on("close", (code) => {
100
  appendLog(username, `Process exited with code ${code}`);
101
  });
102
 
103
- res.send("Command execution started.");
104
  });
105
 
106
  // Endpoint to get user logs
 
74
  });
75
 
76
  // Endpoint to handle terminal commands
77
+ app.post("/execute", (req, res) => {
78
  const { username, command } = req.body;
79
 
 
 
 
 
80
  const userDir = path.join(usersDir, username);
81
+ if (!fs.existsSync(userDir)) {
82
+ return res.status(400).send("User not found.");
83
  }
84
 
85
+ const logFile = path.join(userDir, "logs.txt");
86
+ const commandParts = command.split(" ");
87
+ const mainCommand = commandParts[0];
88
+ const args = commandParts.slice(1);
89
+
90
+ // Spawn a child process to execute the command
91
+ const child = spawn(mainCommand, args, { cwd: userDir, shell: true });
92
 
93
  child.stdout.on("data", (data) => {
94
+ const output = data.toString().trim();
95
+ appendLog(username, output);
96
  });
97
 
98
  child.stderr.on("data", (data) => {
99
+ const errorOutput = data.toString().trim();
100
+ appendLog(username, errorOutput);
101
  });
102
 
103
  child.on("close", (code) => {
104
  appendLog(username, `Process exited with code ${code}`);
105
  });
106
 
107
+ res.send("Command executed.");
108
  });
109
 
110
  // Endpoint to get user logs