This repository has been archived by the owner on Aug 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm-interface.js
151 lines (127 loc) · 3.17 KB
/
vm-interface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var config = require('./configurator.js');
var rpcEngine = require('./rpc.js');
var express = require('express');
var common = require('./common.js');
var error = common.error;
var fs = require('fs');
var log = common.log;
var vlog = common.vlog;
var vmActions = require('./vm-actions.js');
var vmid = config.vmid;
var VMStates = common.VMStates;
var vmState = VMStates.BUSY;
var rpc = config.rpcInterface;
function runFirefoxPluginListenerServer() {
var app = express();
app.use(express.bodyParser());
app.post('/browser-events', function (req, res) {
log("POST /browser-events " + JSON.stringify(req.body));
var data = req.body;
rpc.browser_event(vmid, data, function(){});
res.send('');
});
app.listen(config.vm.firefox_port);
}
//////////////////////////////////////////////
function vmCheckIn(callback) {
log("checking in, id = " + vmid);
rpc.checkin(vmid, function () {
log("Checkin successful.");
callback();
});
}
// TODO: This is just to be simple. But if we want to ultimately support multiple instances per VM, this needs to be
// changed somehow.
var sessionPayload;
function getRPCImpl() {
return {
ping: function (callback) {
callback(vmState);
},
prepare: function (data, callback) {
vmState = VMStates.BUSY;
vmActions.prepare(data, function (err, result) {
if (err) {
vmState = VMStates.ERROR;
callback({
state: vmState
});
} else {
vmState = VMStates.READY;
sessionPayload = result;
callback({
firefox_pid: result.firefox_proc.pid,
vnc_passwd: result.vnc_passwd,
state: vmState
});
}
});
},
cleanup: function (data, callback) {
vmState = VMStates.BUSY;
vmActions.cleanup({
payload: sessionPayload
}, function (err, result) {
if (err) {
vmState = VMStates.ERROR;
callback({
state: vmState
});
} else {
vmState = VMStates.FREE;
callback({
result: result,
state: vmState
});
}
});
}
// TODO: add debug commands such as enter error state
};
}
function runFlashPolicyServer() {
var net = require("net");
var flashPolicyServer = net.createServer(function (stream) {
stream.setTimeout(0);
stream.setEncoding("utf8");
stream.addListener("connect", function () {
});
stream.addListener("data", function (data) {
if ( data.indexOf('<policy-file-request/>') != -1){
stream.write('<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>');
}
stream.end();
});
stream.addListener("end", function() {
stream.end();
});
});
flashPolicyServer.listen(1234);
}
function runRpcServer() {
var rpc_server = new rpcEngine(getRPCImpl());
rpc_server.listen(config.vm.interface_port);
}
function runVMInterface() {
vmActions.initial_bootup(function (err, result) {
if (err) {
log("Initial bootup failed.");
vmState = VMStates.ERROR;
vmCheckIn(function () {
});
} else {
runRpcServer();
runFirefoxPluginListenerServer();
runFlashPolicyServer();
vmState = VMStates.FREE;
setTimeout(function() {
vmCheckIn(function () {
});
}, 2000);
}
});
return {
rpcImpl: getRPCImpl()
};
}
exports = module.exports = runVMInterface;