Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const WebSocket = require('ws');
|
| 3 |
+
const http = require('http');
|
| 4 |
+
const { spawn } = require('child_process');
|
| 5 |
+
const path = require('path');
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const server = http.createServer(app);
|
| 9 |
+
const wss = new WebSocket.Server({ server });
|
| 10 |
+
|
| 11 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 12 |
+
|
| 13 |
+
wss.on('connection', ws => {
|
| 14 |
+
const shell = spawn('bash');
|
| 15 |
+
|
| 16 |
+
shell.stdout.on('data', data => ws.send(data.toString()));
|
| 17 |
+
shell.stderr.on('data', data => ws.send(data.toString()));
|
| 18 |
+
shell.on('close', code => ws.send(`\n\nShell closed with code ${code}`));
|
| 19 |
+
|
| 20 |
+
ws.on('message', msg => {
|
| 21 |
+
shell.stdin.write(msg);
|
| 22 |
+
});
|
| 23 |
+
|
| 24 |
+
ws.on('close', () => {
|
| 25 |
+
shell.kill();
|
| 26 |
+
});
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
const port = process.env.PORT || 7860;
|
| 30 |
+
server.listen(port, () => {
|
| 31 |
+
console.log("Server running on http://localhost:" + port);
|
| 32 |
+
});
|