-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,150 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const express = require('express'); | ||
const { spawn } = require('child_process'); | ||
|
||
const app = express(); | ||
|
||
app.use(express.text()); // Parse text/plain requests | ||
|
||
app.post('/', (req, res) => { | ||
const command = req.body.trim(); | ||
console.log(`Received command: ${command}`); | ||
|
||
const shellCommand = process.platform === 'win32' ? 'cmd' : 'bash'; | ||
const child = spawn(shellCommand); | ||
|
||
child.stdin.write(command + '\n'); | ||
child.stdin.end(); | ||
|
||
let stderrData = ''; // Store stderr data | ||
|
||
// Capture stdout | ||
child.stdout.on('data', (stdoutData) => { | ||
res.write(stdoutData); | ||
}); | ||
|
||
// Capture stderr | ||
child.stderr.on('data', (stderrChunk) => { | ||
stderrData += stderrChunk.toString(); // Append stderr data | ||
}); | ||
|
||
child.on('close', (code) => { | ||
console.log(`Child process exited with code ${code}`); | ||
res.end(stderrData); // Send stderr data as part of response | ||
}); | ||
|
||
child.on('error', (error) => { | ||
console.error(`Error spawning child process: ${error.message}`); | ||
res.status(500).send(`Error spawning child process: ${error.message}`); | ||
}); | ||
}); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Server running'); | ||
}); | ||
|
||
const PORT = 8080; | ||
app.listen(PORT, () => { | ||
console.log(`HTTP server running on port ${PORT}`); | ||
}); |
Oops, something went wrong.