-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
87 lines (75 loc) · 2.35 KB
/
server.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
var express = require("express");
var app = express();
const { spawn } = require('child_process');
var runningProcesses = {};
function makeId() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 20; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.get("/terminalserver", function(req, res) {
res.send("running terminalserver");
});
app.get("/kill/*", function(req, res) {
let id = req.params[0];
console.log(runningProcesses);
runningProcesses[id]["process"].kill();
res.send("killed");
});
app.get("/stdout/*", function(req, res) {
let id = req.params[0];
let result = runningProcesses[id]["stdout"].splice(0, runningProcesses[id]["stdout"].length)
res.send(result);
});
app.get("/stderr/*", function(req, res) {
let id = req.params[0];
let result = runningProcesses[id]["stderr"].splice(0, runningProcesses[id]["stderr"].length)
res.send(result);
});
app.get("/end/*", function(req, res) {
let id = req.params[0];
if (runningProcesses[id]["end"] && runningProcesses[id]["stdout"].length === 0 && runningProcesses[id]["stderr"].length === 0) {
delete runningProcesses[id];
res.send(true);
} else {
res.send(false);
}
});
app.get("/new/*", function(req, res){
let cmd = req.params[0];
console.log('spawn: ' + cmd);
let proc = spawn(cmd.split(" ")[0], cmd.split(" ").slice(1));
let id = makeId();
runningProcesses[id] = {"stdout": [], "stderr": [], "end": false};
runningProcesses[id]["process"] = proc;
proc.stdout.on('data', function (data) {
runningProcesses[id]["stdout"].push(data.toString());
});
proc.stderr.on('data', function (data) {
runningProcesses[id]["stderr"].push(data.toString());
});
proc.on('close', function () {
runningProcesses[id]["end"] = true;
console.log("end");
console.log(runningProcesses);
})
res.send(id);
});
// var port = process.env.PORT || 5000;
// app.listen(port, function() {
// console.log("Listening on " + port);
// });
module.exports = {
terminalServer: function (port) {
app.listen(port, function() {
console.log("Listening on " + port);
});
}
};