forked from oftn-oswg/oftn-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (159 loc) · 5.32 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var Path = require("path");
var Spawn = require("child_process").spawn;
var Sandbox = module.exports = function(utils) {
this.utils = utils;
};
Sandbox.V8 = 1;
Sandbox.SpiderMonkey = 2;
Sandbox.Haskell = 3;
Sandbox.Node = 4;
Sandbox.Babel = 5;
var SandboxError = function(name, message) {
return {
data: {
type: null,
console: [],
obvioustype: true
},
error: name+": "+message,
result: null
};
};
var TimeoutError = function(timeout) {
return SandboxError("Timeout Error", "Execution time exceeded "+
(timeout/1000)+" second"+
((timeout == 1000) ? "" : "s"));
};
var UnknownError = function(s) {
return SandboxError("Unknown Error",
"Please contact eboyjr. INFO: "+s.replace(/\s+/g, " "));
};
Sandbox.prototype.run = function(engine, timeout, code, hollaback, object) {
var process, args, child;
switch (engine) {
case Sandbox.Haskell:
return this.runHaskell(timeout, code, hollaback, object);
default:
case Sandbox.V8:
case Sandbox.SpiderMonkey:
child = Spawn(Path.join(__dirname, engine==Sandbox.V8?"v8":"sm", "shovel.bin"),
[this.utils],
{env: {LD_LIBRARY_PATH: "/usr/local/lib"}});
break;
case Sandbox.Node:
return this.runInJsEval('node', timeout, code, hollaback, object);
case Sandbox.Babel:
return this.runInJsEval('babel', timeout, code, hollaback, object);
}
// Any vars in da house?
var timer;
var stdout = [];
var output = function(data) {
if (data) {
stdout.push(data);
}
};
// Listen
// child.stderr.on("data", output); // We don't need this anymore
child.stdout.on("data", output);
child.on("exit", function(code, signal) {
clearTimeout(timer);
if (code === null && signal !== "SIGKILL") {
hollaback.call(object, UnknownError("("+signal.toLowerCase()+")"));
return;
}
try {
var data = JSON.parse(stdout.join(""));
hollaback.call(object, data);
} catch (e) {
hollaback.call(object, UnknownError(stdout.join("")));
}
});
// Fo shizzle
child.stdin.write(code);
child.stdin.end();
// We gots ta move it, yo!
timer = setTimeout(function() {
child.stdout.removeListener("output", output);
stdout = [JSON.stringify(TimeoutError(timeout))];
child.kill("SIGKILL");
}, timeout);
};
Sandbox.prototype.runHaskell = function (timeout, code, hollaback, object) {
var stdout = ""
, env = Object.create (process.env, {LANG: {value: "en_US.UTF-8", enumerable: true, writable: true, configurable: true}})
, child = Spawn ("mueval",
["--inferred-type", "--time-limit=" + Math.round(timeout / 1000)
,"--no-imports", "--load-file=" + Path.join (__dirname, "hs", "Environment.hs")
,"--Extensions", "-e", code], {env: env});
child.stdout.on("data", function (data) { if (data) stdout += data });
child.stderr.on("data", function (data) { if (data) stdout += data }); // 2>&1
child.on("exit", function (code, signal) {
var lines = stdout.replace (/^\s+|\s+$/g, "").split ("\n");
if (code === 0) {
hollaback.call (object, { data: { type: lines.splice (1, lines.length - 2).join (" ")
, console: []
, obvioustype: false }
, error: null
, result: lines[1] });
} else {
if (lines.length > 1) {
hollaback.call (object, { data: { type: lines[1]
, console: []
, obvioustype: false }
, error: lines.splice (2, lines.length - 2).join (" ")
, result: null });
} else {
hollaback.call (object, { data: { type: null
, console: []
, obvioustype: true }
, error: lines.join (" ")
, result: null });
}
}
});
child.stdin.end();
};
Sandbox.prototype.runInJsEval = function(engine, timeout, code, hollaback, object) {
// wrap code in an iife to get a sensible completion record
// otherwise we get e.g. 'use strict' when undefined is expected
code = '(function() { ' + code + ' }.call(global))';
var stdout = ""
, childId = 'oftn_sandbox_' + Math.floor(0x10000000+Math.random()*0xefffffff).toString(32)
, child = Spawn ("docker",
["run", "-i", "--rm", "--net=none", "-m=64m", "-c=128", "--name=" + childId, "brigand/js-eval"], {env: process.env})
, timeoutId = setTimeout(function() {
console.error("Timeout on docker container running this code: ", code);
Spawn ("docker",
["kill", childId], {env: process.env})
}, timeout);
child.stdout.on("data", function (data) { if (data) stdout += data });
child.stderr.pipe(process.stderr);
child.on("exit", function (code, signal) {
clearTimeout(timeoutId);
if (code === 137) {
return hollaback.call(object, {
isJSEval: true,
reason: 'timeout, took over ' + timeout + 'ms'
});
} else if (code !== 0) {
console.error('Unknown js-eval error', code);
console.error(stdout);
return hollaback.call(object, {
isJSEval: true,
reason: 'unknown error'
});
}
var parsed = JSON.parse(stdout);
hollaback.call(object, {
isJSEval: true,
success: parsed.success,
text: parsed.text
});
});
child.stdin.write(JSON.stringify({
engine: engine,
code: code
}));
child.stdin.end();
}