-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (89 loc) · 2.35 KB
/
index.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
var fs = require('fs-extra');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var pty = require('pty.js');
app.get('/', function(req, res) { res.sendFile(__dirname + "/index.html"); });
io.on('connection', function(socket) {
var connected = true;
console.log('user connected');
socket.on('disconnect', function() {
connected = false;
if(qemu) qemu.kill();
console.log('user disconnected');
});
var qemu = (function setupQEMU() {
var q = pty.spawn('qemu-system-i386',
['-curses',
'-kernel', 'vmlinuz',
'-initrd', 'core.gz',
'--append', 'superuser quiet',
'-m', '48',
'-net', 'none'],
{
name: 'vt100',
cols: 80,
rows: 25,
cwd: process.env.PWD,
env: process.env
});
q.on('data', function(data) {
socket.emit('stdout', data);
});
q.on('exit', function() {
if(!connected) return; // Don't respawn qemu if client has disconnected.
qemu = setupQEMU();
});
return q;
})();
socket.on('stdin', function(data) {
if(typeof(data) != 'object') return;
if(typeof(data.keyCode) != 'number') return;
switch(data.keyCode) {
case 37:
qemu.write(String.fromCharCode(27) + "[D");
break;
case 38:
qemu.write(String.fromCharCode(27) + "[A");
break;
case 39:
qemu.write(String.fromCharCode(27) + "[C");
break;
case 40:
qemu.write(String.fromCharCode(27) + "[B");
break;
case 220: // Backslash
qemu.write("\\");
break;
case 191: // Forward slash
qemu.write("/");
break;
case 190: // Period / right angle bracket
if(data.shiftKey) {
qemu.write('>');
} else {
qemu.write('.');
}
break;
case 189: // Dash
qemu.write('-');
break;
case 186: // (semi) colon
if(data.shiftKey) {
qemu.write(':');
} else {
qemu.write(';');
}
break;
default:
if(data.shiftKey) {
qemu.write(String.fromCharCode(data.keyCode));
} else {
qemu.write(String.fromCharCode(data.keyCode).toLowerCase());
}
}
});
});
http.listen(3000, function() {
console.log('listening on port 3000...');
});