-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (54 loc) · 1.64 KB
/
index.ts
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
const BODY_LIMIT = 8 * 1024;
const MONITOR_ACCESS_TOKEN =
"MEGALITH_MONITORING_SYSTEM_EZRVJJCQ4ET3W25BONANMPWPQSRV7JXE";
Bun.serve({
development: false,
maxRequestBodySize: BODY_LIMIT,
port: 1984,
fetch: async function (request) {
if (request.headers.get("Monitor-Access-Token") !== MONITOR_ACCESS_TOKEN) {
return new Response(null, { status: 451 });
}
let data;
try {
data = await request.json();
} catch {
return new Response(null, { status: 400 });
}
const entry = data["entry"] || ".";
const cmd = data["cmd"] || "";
if (typeof entry !== "string" || typeof cmd !== "string") {
return new Response(null, { status: 422 });
} else if (entry.includes("..")) {
return new Response(null, { status: 418 });
}
const cmdPath = path.join("/tmp", crypto.randomBytes(8).toString("hex"));
Bun.write(cmdPath, cmd, { mode: 400 });
const command = Bun.spawn(
["/usr/bin/deno", "run", `--allow-read=./${entry}`, cmdPath],
{
stderr: "ignore",
cwd: "entries",
env: {},
}
);
Bun.sleep(1000).then(() => command.kill(9));
let totalLength = 0;
const result = new Uint8Array(BODY_LIMIT);
for await (const chunk of command.stdout) {
let part = chunk.slice(
0,
Math.min(result.length - totalLength, chunk.length)
);
result.set(part, totalLength);
totalLength += part.length;
if (totalLength >= result.length) {
command.kill(9);
break;
}
}
await command.exited;
await unlink(cmdPath);
return new Response(result.slice(0, totalLength));
},
});