forked from butlerx/wetty
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
75 lines (65 loc) · 1.91 KB
/
app.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
var express = require('express');
var http = require('http');
var path = require('path');
var server = require('socket.io');
var pty = require('pty.js');
var opts = require('optimist')
.options({
port: {
demand: true,
alias: 'p',
description: 'wetty listen port'
},
command: {
demand: false,
alias: 'c',
description: 'command to run in shell, defaults to /bin/login'
},
}).boolean('allow_discovery').argv;
var command = '/bin/login';
if (opts.command) {
command = opts.command;
}
process.on('uncaughtException', function(e) {
console.error('Error: ' + e);
});
var app = express();
app.get('/course/:course', function(req, res) {
var course = req.params.course;
res.sendfile(__dirname + '/public/index.html');
});
app.use('/', express.static(path.join(__dirname, 'public')));
var httpserv = http.createServer(app).listen(opts.port, function() {
console.log('http on port ' + opts.port);
});
var io = server(httpserv);
io.on('connection', function(socket){
var request = socket.request;
var course = 'default';
if (match = request.headers.referer.match('/course/.+$')) {
course = match[0].replace('/course/', '');
}
console.log((new Date()) + ' Connection accepted.');
var term;
term = pty.spawn(command, [course], {
name: 'xterm-256color',
cols: 80,
rows: 30
});
console.log((new Date()) + " PID=" + term.pid + " STARTED")
term.on('data', function(data) {
socket.emit('output', data);
});
term.on('exit', function(code) {
console.log((new Date()) + " PID=" + term.pid + " ENDED")
});
socket.on('resize', function(data) {
term.resize(data.col, data.row);
});
socket.on('input', function(data) {
term.write(data);
});
socket.on('disconnect', function() {
term.end();
});
})