-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
68 lines (57 loc) · 1.6 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
const express = require("express");
const childProcess = require("child_process");
const fs = require("fs");
const fsPromises = require("fs/promises");
const path = require("path");
const os = require("os");
const app = express();
const port = process.env.PORT ?? 3000;
app.use(express.static("dist"));
app.use(express.json());
const zigPath = "node_modules/@oven/zig/zig";
function spawn(command, args) {
return new Promise((resolve) => {
// damn this api is shit
const p = childProcess.spawn(command, args);
const stdouts = [];
const stderrs = [];
p.stdout.on("data", (data) => {
stdouts.push(data);
});
p.stderr.on("data", (data) => {
stderrs.push(data);
});
p.on("close", (code) => {
resolve({
code: code,
stdout: Buffer.concat(stdouts).toString(),
stderr: Buffer.concat(stderrs).toString(),
});
});
});
}
function maketmp() {
return new Promise((resolve, reject) => {
fs.mkdtemp(path.join(os.tmpdir(), "use-c"), (err, dir) => {
if (err !== null) reject(err);
else resolve(dir);
});
});
}
async function runC(code) {
const dir = await maketmp();
const cFile = path.join(dir, "main.c");
const outFile = path.join(dir, "main");
await fsPromises.writeFile(cFile, decodeURIComponent(code));
await spawn(zigPath, ["cc", cFile, "-o", outFile]);
const out = await spawn(outFile, []);
return out;
}
app.post("/rpc/rce", async (req, res) => {
const { code } = req.body;
const out = await runC(code);
res.json(out);
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});