Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const WebSocket = require('ws');
|
2 |
+
const os = require('os');
|
3 |
+
const pty = require('node-pty');
|
4 |
+
|
5 |
+
// Create WebSocket server
|
6 |
+
const wss = new WebSocket.Server({ port: 6060 });
|
7 |
+
|
8 |
+
wss.on('connection', function connection(ws) {
|
9 |
+
// Determine the shell to use based on the OS
|
10 |
+
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
|
11 |
+
|
12 |
+
// Spawn a terminal instance
|
13 |
+
const ptyProcess = pty.spawn(shell, [], {
|
14 |
+
name: 'xterm-color',
|
15 |
+
env: process.env,
|
16 |
+
});
|
17 |
+
|
18 |
+
// Send command to the terminal
|
19 |
+
ws.on('message', (command) => {
|
20 |
+
ptyProcess.write(command + '\n');
|
21 |
+
});
|
22 |
+
|
23 |
+
// Send terminal output to the WebSocket client
|
24 |
+
ptyProcess.on('data', function (data) {
|
25 |
+
ws.send(data);
|
26 |
+
});
|
27 |
+
});
|