-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (77 loc) · 2.54 KB
/
app.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
const fs = require('fs');
const https = require('https');
const express = require('express');
const multer = require('multer');
const cors = require('cors');
const { Readable } = require('stream');
const creds = {
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.crt')
};
const app = express();
let config = {
dir: ''
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const upload = multer({ dest: './uploads' })
app.use(cors());
app.use(express.json());
app.post('/config', (req, res) => {
config = req.body;
console.dir(config);
res.end();
});
app.post('/challenge', (req, res) => {
const cat = req.body.category.replace(/[^a-z0-9_\(\)\[\]\-]/gi, '');
const name = req.body.name.replace(/[^a-z0-9_\(\)\[\]\-]/gi, '');
const theDir = `${config.dir}/${cat}/${name}`;
fs.mkdir(theDir, { recursive: true }, (e) => {
if (e) {
throw new Error(`Can't make directory ${theDir}`);
}
const content = `# ${req.body.name}\n\n` +
`${req.body.attribution ? '> Author: ' + req.body.attribution : ''}\n\n` +
req.body.description.replaceAll('\r\n', '\n') +
`\n\n${req.body.connection_info ?? ''}`;
fs.writeFile(`${theDir}/README.md`, content, (e) => {
if (e) {
throw new Error(`Can't make README ${theDir}/README.md`);
}
console.log(`Written challenge ${name} description`);
res.json({
...req.body,
category: cat,
name: name
});
});
});
});
app.post('/challenge/:chalCategory/:chalName/attachment', upload.single('file'), (req, res) => {
res.end();
const cat = req.params.chalCategory.replace(/[^a-z0-9_\(\)\[\]\-]/gi, '');
const name = req.params.chalName.replace(/[^a-z0-9_\(\)\[\]\-]/gi, '');
const theDir = `${config.dir}/${cat}/${name}`;
fs.rename(req.file.path, `${theDir}/${req.file.originalname}`, () => {});
console.log(`Got file for ${cat}/${name}: ${req.file.originalname}. Saved to ${theDir}/${req.file.originalname}`);
});
app.get('/proxy', async (req, res) => {
const url = req.query.url;
try {
const fetchResponse = await fetch(url);
if (!fetchResponse.ok) {
return res.status(fetchResponse.status).send('Error fetching data');
}
fetchResponse.headers.forEach((value, name) => {
res.setHeader(name, value);
});
const readableStream = Readable.fromWeb(fetchResponse.body);
readableStream.pipe(res);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Internal Server Error');
}
});
const server = https.createServer(creds, app);
server.listen(8080, () => {
console.log("[i] Listened to localhost 8080");
});