-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (99 loc) · 4.37 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
const express = require('express');
const docker_run = require('./docker')
const uuid = require('uuid');
const util = require('util');
const { spawn, spawnSync } = require('child_process');
// promisifed method
const writeFile_promise = util.promisify(require('fs').writeFile);
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', (req, res) => {
res.json({ "team": "kenzz17", "members": ["Shivam Raj", "Aman Singh", "Pawan Kumar", "Aniket Agrawal"] })
})
app.post('/v1', async (req, res) => {
if (req.body.passwd != '314159kenzz17') return res.json({ "error": "Access Denied", "stdout": "", "stdout": "" });
filename = uuid.v4();
toRun = '___main___.' + req.body.lang;
try {
spawnSync('sudo', ['mkdir', '-p', './temp/' + filename]);
const code = writeFile_promise('./temp/' + filename + '/' + toRun, req.body.code);
const stdin = writeFile_promise('./temp/' + filename + '/in', req.body.stdin);
await Promise.all([code, stdin]).catch((err) => { throw 'err' });
} catch (err) {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
return res.json({ "error": "Unable to write", "stderr": "", "stdout": "" });
}
docker_run(filename, toRun, req.body.lang)
.then((data) => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json(data);
})
.catch(() => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json({ "error": "Unable to run/compile", "stderr": "", "stdout": "" });
});
})
app.post('/v2', async (req, res) => {
if (req.body.passwd != '314159kenzz17') return res.json({ "error": "Access Denied", "stdout": "", "stdout": "" });
filename = uuid.v4();
toRun = '___main___.' + req.body.lang;
try {
spawnSync('sudo', ['mkdir', '-p', './temp/' + filename]);
const helper = [];
req.body.helper.forEach((ele, idx) => {
helper.push(writeFile_promise('./temp/' + filename + '/' + ele.name, ele.body));
});
const code = writeFile_promise('./temp/' + filename + '/' + toRun, req.body.code);
const stdin = writeFile_promise('./temp/' + filename + '/in', req.body.stdin);
await Promise.all([...helper, code, stdin]).catch((err) => { throw 'err' });
} catch (err) {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
return res.json({ "error": "Unable to write", "stderr": "", "stdout": "" });
}
docker_run(filename, toRun, req.body.lang)
.then((data) => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json(data);
})
.catch(() => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json({ "error": "Unable to run/compile", "stderr": "", "stdout": "" });
});
})
app.post('/v3', async (req, res) => {
if (req.body.passwd != '314159kenzz17') return res.json({ "error": "Access Denied", "stdout": "", "stdout": "" });
filename = uuid.v4();
toRun = req.body.path + '/' + req.body.name;
try {
const helper = [];
req.body.helper.forEach((ele, idx) => {
file = './temp/' + filename + '/' + ele.path + '/' + ele.name;
dir = './temp/' + filename + '/' + ele.path + '/';
spawnSync('sudo', ['mkdir', '-p', dir]);
helper.push(writeFile_promise(file, ele.body));
});
const stdin = writeFile_promise('./temp/' + filename + '/in', req.body.stdin);
await Promise.all([...helper, stdin]).catch((err) => { throw 'err' });
} catch (err) {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
return res.json({ "error": "Unable to write", "stderr": "", "stdout": "" });
}
docker_run(filename, toRun, req.body.lang)
.then((data) => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json(data);
})
.catch(() => {
spawn('sudo', ['rm', '-r', './temp/' + filename]);
res.json({ "error": "Unable to run/compile", "stderr": "", "stdout": "" });
});
})
const PORT = 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));