-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (145 loc) · 4.25 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
#!/usr/bin/env node
const express = require("express");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const rimraf = require("rimraf");
const childProcess = require("child_process");
const Canvas = require("canvas");
const unzip = require("./unzip");
const cors = require("cors");
var files = require(path.resolve( __dirname, "files.json"));
const randomColourPart = () => Math.floor(Math.random() * 255);
const randomColour = () =>
`rgb(${randomColourPart()},${randomColourPart()},${randomColourPart()})`;
const WIDTH = 1280;
const HEIGHT = 720;
const BLOCK = 50;
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const outPath = path.join(__dirname, "out");
let imageTime = Date.now();
let logs = "Random logs\n";
function updateImageTime() {
setTimeout(() => {
imageTime = Date.now();
if(logs.length < 500) {
logs += Math.random() + "\n";
} else {
logs = "Random logs\n";
}
updateImageTime();
}, (Math.floor(Math.random() * 3) + 1) * 1000);
}
updateImageTime();
app.use(cors())
app.get("/", (_req, res) => res.sendFile(path.join(__dirname, "index.txt")));
let child = {
process: null,
log: ""
};
const logData = data => {
child.log += data;
console.log(`[LOG] ${data.toString().trim()}`);
};
const killProcess = () => {
if (child.process) {
child.process.kill("SIGINT");
console.log("[INFO] Killed process!");
}
};
const port = parseInt(process.argv[process.argv.length - 1]) || 80;
app.post("/upload/upload", upload.single("uploaded_file"), (req, res) => {
killProcess();
child.log = "";
const respond = err => {
console.log(err ? `[ERROR] ${err}` : "[INFO] Uploaded!");
res.status(err ? 500 : 204);
res.send(err);
};
console.log(`[INFO] Uploading ${req.file.originalname}...`);
rimraf(outPath, err => {
err
? respond(err)
: setTimeout(() => {
fs.mkdir(outPath, err => {
err
? respond(err)
: req.file.mimetype === "application/zip"
? unzip.unzipBuffer(req.file.buffer, outPath, respond)
: fs.writeFile(
path.join(outPath, "main.py"),
req.file.buffer,
respond
);
});
}, 100);
});
});
app.post("/run/start", (_req, res) => {
console.log("[INFO] Received start request!");
killProcess();
child.log = "";
console.log("[INFO] Spawning process...");
child.process = childProcess.spawn("python", [
"-u",
path.join(outPath, "main.py")
]);
child.process.stdout.on("data", logData);
child.process.stderr.on("data", logData);
child.process.on("close", () => {
child.process = null;
console.log("[INFO] Process terminated!");
});
res.sendStatus(204);
});
app.post("/run/stop", (_req, res) => {
console.log("[INFO] Received stop request!");
killProcess();
child.process = null;
res.sendStatus(204);
});
app.get("/run/output", (_req, res) => {
res.send(child.log || logs);
});
app.get("/static/image.jpg", (_req, res) => {
const canvas = Canvas.createCanvas(WIDTH, HEIGHT);
const ctx = canvas.getContext("2d");
for (let y = 0; y < HEIGHT / BLOCK; y++) {
for (let x = 0; x < WIDTH / BLOCK; x++) {
ctx.fillStyle = randomColour();
ctx.fillRect(x * BLOCK, y * BLOCK, BLOCK, BLOCK);
}
}
const text = `${new Date().toLocaleTimeString()} [${port}]`;
ctx.font = "100px sans-serif";
let bounds = ctx.measureText(text);
const backWidth = bounds.width + 50;
ctx.fillStyle = "rgb(0, 0, 0)";
ctx.fillRect(
(WIDTH - backWidth) / 2,
(HEIGHT - (bounds.emHeightAscent + bounds.emHeightDescent)) / 2,
backWidth,
bounds.emHeightAscent + bounds.emHeightDescent
);
ctx.fillStyle = "white";
ctx.fillText(
text,
(WIDTH - bounds.width) / 2,
(HEIGHT + bounds.emHeightAscent - bounds.emHeightDescent) / 2
);
// Simulates network delay
setTimeout(() => {
res.contentType("image/jpeg");
canvas.createJPEGStream().pipe(res);
}, 500);
});
app.get("/static/imgtime.txt", (_req, res) => {
res.send(imageTime.toString());
});
app.get("/files", (_req, res) => {
res.json(files);
});
app.listen(port, () => {
console.log(`[INFO] Server running on port: ${port}`);
});