-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (79 loc) · 2.33 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
101
102
103
104
var express = require('express');
var http = require('http');
var exec = require('ssh-exec');
var execSync = require('child_process').execSync;
var atob = require('atob');
var app = express();
var HOST = process.env.UBUNTU_HOST || "ubuntu-qemu";
var GUACAMOLE_HOST = process.env.GUACAMOLE_HOST || "guacamole-client";
var USERNAME = "vagrant";
var PASSWORD = "vagrant";
app.use(express.static('frontend/dist'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/token', function (req, res) {
var cmd = execSync("curl 'http://" + GUACAMOLE_HOST + ":8080/guacamole/api/tokens' --data ''");
res.send(cmd.toString());
});
app.get('/apps', function (req, res) {
exec('for app in /usr/share/applications/*.desktop ; do app="${app##/*/}"; echo -n "${app::-8}=" && cat /usr/share/applications/${app::-8}.desktop | grep \'^Exec\' | head -n1 | cut -d \'=\' -f 2 ; done', {
user: USERNAME,
host: HOST,
port: 2222,
password: PASSWORD
}, function(err, stdout, stderr) {
if (err) {
return res.status(500).send({
error: err,
stderr: stderr
});
}
var apps = stdout.split("\n");
var result = {};
apps.forEach(function(app) {
var appName = app.split('=')[0];
var appPath = app.split('=')[1];
result[appName] = appPath;
});
return res.send(result);
});
});
app.post('/app/*', function(req, res) {
// We want to sleep to give time to the app to launch before closing SSH connection
exec('(env DISPLAY=:0 nohup ' + atob(req.params[0]) + ' &) ; sleep 3', {
user: USERNAME,
host: HOST,
port: 2222,
password: PASSWORD
}, function(err, stdout, stderr) {
if (err) {
return res.status(500).send({
error: err,
stderr: stderr
});
}
return res.send(stdout);
});
});
app.get('/', function (req, res) {
exec('uname -a', {
user: USERNAME,
host: HOST,
port: 2222,
password: PASSWORD
}, function(err, stdout, stderr) {
if (err) {
return res.status(500).send({
error: err,
stderr: stderr
});
}
return res.send(stdout);
});
});
app.listen(3000, function () {
console.log('Human-apps listening on port 3000');
});