-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
39 lines (31 loc) · 965 Bytes
/
backend.js
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
29
30
31
32
33
34
35
36
37
38
39
const WebSocket = require('ws')
var os = require('os');
var pty = require('node-pty');
const wss = new WebSocket.Server({ port: 6060 })
console.log("Socket is up and running...")
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
var ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
// cwd: process.env.HOME,
env: process.env,
});
wss.on('connection', ws => {
console.log("new session")
// Catch incoming request
ws.on('message', command => {
var processedCommand = commandProcessor(command)
ptyProcess.write(processedCommand);
})
// Output: Sent to the frontend
ptyProcess.on('data', function (rawOutput) {
var processedOutput = outputProcessor(rawOutput);
ws.send(processedOutput);
console.log(processedOutput);
});
})
const commandProcessor = function(command) {
return command;
}
const outputProcessor = function(output) {
return output;
}