-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
65 lines (59 loc) · 1.49 KB
/
server.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
this.Pty = require('node-pty-prebuilt-multiarch');
this.Websocket = require("ws").Server;
this.onclosed = () => {
process.exit();
};
this.onopened = () => {};
this.onresize = () => {};
this.ondisconnected = () => {};
var port = process.argv.slice(2)[0];
var dir = process.argv.slice(3)[0];
var commands = process.argv.slice(4)[0].split(" ");
var command = commands[0];
var args = commands.slice(1);
this.tty = this.Pty.spawn(command, args, {
name: 'xterm-color',
cols: 80,
rows: 24,
cwd: dir,
env: process.env
});
this.tty.on('exit', (code, signal) => {
this.onclosed(code, signal);
});
this.wss = new this.Websocket({
port: port,
clientTracking: true,
verifyClient: (info) => {
if (this.wss.clients.length >= 1) {
return false;
} else {
return true;
}
}
});
this.wss.on('connection', (ws) => {
this.onopened();
ws.on('message', (msg) => {
if (msg.startsWith("ESCAPED|-- ")) {
if (msg.startsWith("ESCAPED|-- RESIZE:")) {
msg = msg.substr(18);
let cols = msg.slice(0, -4);
let rows = msg.substr(4);
this.tty.resize(Number(cols), Number(rows));
}
} else {
this.tty.write(msg);
}
});
this.tty.on('data', (data) => {
try {
ws.send(data);
} catch (e) {
// Websocket closed
}
});
});
this.wss.on('close', () => {
this.ondisconnected();
});