-
Notifications
You must be signed in to change notification settings - Fork 20
/
terminal.js
158 lines (137 loc) · 4.61 KB
/
terminal.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const express = require('express');
const app = express();
const expressWs = require('express-ws')(app);
const os = require('os');
const cors = require('cors');
const pty = require('node-pty');
const { spawn } = require('child_process');
const bodyParser = require('body-parser');
const guard = require('./core/guard');
const fs = require('fs');
app.use(cors());
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
const terminals = {};
const logs = {};
const websocket = {};
app.post('/terminals', (req, res) => {
const cols = parseInt(req.query.cols);
const rows = parseInt(req.query.rows);
const term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], {
name: 'xterm-color',
cols: cols || 80,
rows: rows || 24,
cwd: os.homedir(),
env: process.env
});
console.log(`Created terminal with PID: ${term.pid}`);
terminals[term.pid] = term;
logs[term.pid] = '';
term.on('data', (data) => {
logs[term.pid] += data;
});
res.send({pid: term.pid.toString(), cwd: os.homedir(),platform: process.platform});
res.end();
});
app.post('/terminals/:pid/size', (req, res) => {
const pid = parseInt(req.params.pid);
const cols = parseInt(req.query.cols);
const rows = parseInt(req.query.rows);
const term = terminals[pid];
term.resize(cols, rows);
console.log(`Resized terminal ${pid} to ${cols} cols and ${rows} rows.`);
res.end();
});
app.ws('/cli', (ws, res) => {
ws.on('message', (msg) => {
msg = JSON.parse(msg);
const flags = msg.stdin.trim().split(' ');
console.log('<<<', flags);
if(msg.dir && !fs.existsSync(msg.dir)) {
fs.mkdirSync(msg.dir,{recursive: true});
}
const child = spawn(os.platform() === 'win32' ? 'ng.cmd' : 'ng', flags, {
cwd: msg.dir || os.homedir(),
env: process.env
});
child.stdin.setEncoding('utf-8');
child.stdout.setEncoding('utf-8');
child.stderr.setEncoding('utf-8');
child.stdout.on('data', (stdout) => {
console.log('We received a reply: ' + stdout);
ws.send(JSON.stringify({
stdout
}));
});
child.stderr.on('data', (stderr) => {
console.log('There was an error: ' + stderr);
ws.send(JSON.stringify({
stderr
}));
});
child.on('exit', (exit) => {
console.log("Child exited with code: " + exit);
ws.send(JSON.stringify({
exit: exit,
platform: os.platform()
}));
});
if (msg.KILL) {
child.kill('SIGINT');
ws.send(JSON.stringify({
killed: true
}));
}
});
ws.on('close', () => {
console.log(`Clean things up WS`);
});
});
app.ws('/terminals/:pid', (ws, req) => {
const term = terminals[parseInt(req.params.pid)];
console.log(`Connected to terminal: ${term.pid}`);
ws.send(logs[term.pid]);
let command = '';
term.on('data', (data) => {
// This even will get emitted just after message event
try {
console.log('>>>', data);
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
ws.on('message', (msg) => {
console.log('<<<', msg);
// Check if input is "Enter" key
if (msg.charCodeAt(0) !== 13) {
/**
* When we type command from shell, 'message' event fires for every character. So we append each character until "Enter" (13 ascii code) is
* pressed. Once enter is pressed, it consider that the command is complete and validates
* it and reset the command value accordingly or kill it if
*/
command += msg;
} else if (!guard.isValidShellCommand(command)) {
// Pass ^C command if command is invalid.
term.kill('SIGINT');
command = '';
return;
} else {
command = '';
}
term.write(msg);
});
ws.on('close', () => {
term.kill();
console.log(`Closed terminal: ${term.pid}`);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
const port = process.env.PORT || 3000;
const host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0';
console.log(`App listening to http://${host}:${port}`);
app.listen(port, host);