File size: 701 Bytes
49b9e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const WebSocket = require('ws');
const os = require('os');
const pty = require('node-pty');

// Create WebSocket server
const wss = new WebSocket.Server({ port: 6060 });

wss.on('connection', function connection(ws) {
  // Determine the shell to use based on the OS
  const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

  // Spawn a terminal instance
  const ptyProcess = pty.spawn(shell, [], {
    name: 'xterm-color',
    env: process.env,
  });

  // Send command to the terminal
  ws.on('message', (command) => {
    ptyProcess.write(command + '\n');
  });

  // Send terminal output to the WebSocket client
  ptyProcess.on('data', function (data) {
    ws.send(data);
  });
});