Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (58 loc) · 1.8 KB
/
Copy pathapp.js
File metadata and controls
70 lines (58 loc) · 1.8 KB
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
/* modules */
var http = require('http'),
socketio = require('socket.io'),
gy = require('pty'),
ss = require('socket.io-stream');
/* create server */
var config = require('./config.js');
var server = http.createServer().listen(config.port, config.interface); // only on localhost
/* routes */
var router = require('./router.js');
server.on('request', router);
/* ptys manager */
var ptyMan = {};
/* socket server for terminal control */
/* on connected */
socketio(server).of('termctl').on('connection', function(socket) {
/* receives a message for new bash child process */
ss(socket).on('new', function(stream, name) {
/* start a new bash child process */
var newPty = gy.spawn('bash', [], {
// name: '',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
newPty.resize(50,24);
/* connect the bidirectional pipe from front-end */
newPty.stdout.pipe(stream).pipe(newPty.stdin);
/**/
ptyMan[name] = {
obj: newPty
};
/* if one socket disconnected, kill the spawned bash */
socket.on('disconnect', function() {
newPty.kill('SIGHUP');
delete ptyMan[name];
});
});
/* receives a message of remove one terminal, kill the spawned bash */
socket.on('remove', function(name) {
ptyMan[name].obj.kill('SIGHUP');
delete ptyMan[name];
});
/**/
socket.on('hello', function() {
console.log(ptyMan);
});
});
/* if end process, kill all spawned terminals */
process.on('exit', function() {
var keys = Object.keys(ptyMan);
for (var i in keys) {
ptyMan[i].obj.kill('SIGHUP');
delete ptyMan[i];
}
});
console.log('Running on port:' + config.port);