-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (43 loc) · 1.28 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
var express = require('express');
var fs = require('fs');
var hogan = require('hogan-express');
var bodyParser = require('body-parser');
var childProcess = require('child_process');
const exec = childProcess.exec;
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('views', __dirname + '/views');
app.engine('html', hogan);
app.set('view engine', 'html');
// the code will be stored inside a temporary file, tmp.bsh
// then the beanshell will intepret the tmp.bsh file
app.post('/code', function(req, res) {
var code = req.body.code;
var params = req.body.params;
if (code == undefined || code == "") {
res.json({out: "", err: "Empty command"});
return;
}
fs.writeFile("tmp.bsh", code, function(err) {
if(err) {
res.json({out: "", err: err});
return;
}
var cmd = "java -cp lib/bsh-2.1.8.jar " + params + "bsh.Interpreter tmp.bsh";
exec(cmd, {shell: '/bin/bash'}, function (err, stdout, stderr) {
res.json({out: stdout, err: stderr});
return;
});
});
});
app.all(['/','/index'], function (req, res, next) {
res.render('index.html');
});
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log("Server is running on port " + port);
});