forked from kevin-roark/socket.io-computer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.js
112 lines (93 loc) · 2.56 KB
/
computer.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
var Emitter = require('events').EventEmitter;
var sys = require('sys');
var fs = require('fs');
var exec = require('child_process').exec;
var VNC = require('./vnc');
var Canvas = require('canvas');
var debug = require('debug')('computer:computer');
var net = require('net');
var hostName = process.env.COMPUTER_VNC_HOST || '127.0.0.1';
var displayNum = process.env.COMPUTER_DISPLAY || '0';
var port = 5900 + parseInt(displayNum, 10);
var tcp = process.env.COMPUTER_TCP || '127.0.0.1:4444';
module.exports = Computer;
function Computer() {
if (!(this instanceof Computer)) return new Computer();
this.running = false;
this.img = process.env.COMPUTER_IMG || null;
}
Computer.prototype.__proto__ = Emitter.prototype;
Computer.prototype.closed = function() {
this.running = false;
setTimeout(this.run, 100);
return;
};
Computer.prototype.run = function() {
var self = this;
try {
this.vnc = new VNC(hostName, port);
} catch(e) {
console.log('connection error');
return self.closed();
}
try {
var split = tcp.split(':');
var tcpHost = split[0];
var tcpPort = split[1];
this.tcp = net.connect({host: tcpHost, port: tcpPort});
this.tcp.on('end', function() {
return self.closed();
});
} catch(e) {
console.log('tcp connection error');
return self.closed();
}
this.vnc.on('copy', function(rect){
self.emit('copy', rect);
});
this.vnc.on('raw', function(frame){
self.emit('raw', frame);
});
this.vnc.on('frame', function(frame){
self.emit('frame', frame);
});
this.running = true;
};
// saves a snapshot of disk image to the given filename
Computer.prototype.snapshot = function(name) {
if (!this.running || !this.img) return;
var command = 'qemu-img create -f qcow2 -b ' + this.img + ' ' + name;
exec(command, function(error, stdout, stderr) {
if (!error) {
console.log('saved emu to ' + name);
}
});
};
Computer.prototype.pointer = function(x, y, state) {
if (!this.running) return this;
try {
this.vnc.r.pointerEvent(x, y, state);
} catch(e) {
debug('vnc error -- qemu probably down');
this.closed();
}
};
Computer.prototype.key = function(key) {
if (!this.running) return this;
var command = 'sendkey ' + key + '\n';
this.tcpWrite(command);
};
Computer.prototype.destroy = function(){
if (this.destroyed) return this;
this.destroyed = true;
this.running = false;
return this;
};
Computer.prototype.tcpWrite = function(command) {
try {
this.tcp.write(command);
} catch (e) {
debug('tcp error -- qemu probably down');
this.closed();
}
};