-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (125 loc) · 4.58 KB
/
index.js
File metadata and controls
142 lines (125 loc) · 4.58 KB
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
import express from 'express';
import config from './config.js';
import fs from 'fs';
import { encrypt, decrypt } from './utils/cypher.js';
import path from 'path';
import bodyParser from 'body-parser';
const app = express();
const port = config.port || "3000";
const __dirname = path.resolve(path.dirname('..'));
const keys = config.keys;
app.use(bodyParser.json())
app.use('/*', (req, res, next) => {
if (keys.includes(req.headers['unfortunatism']) && decrypt(req.headers['iv'], req.headers['content']) == config.secret) {
next();
} else {
res.send({ success: false });
}
});
// Files
app.get('/authtokens', (req, res) => {
return res.sendFile(__dirname + '/storage/authtokens.json');
})
app.post('/authtokens', (req, res) => {
/*
const body = req.body;
if (!body) return res.send({ success: false })
fs.readFile(__dirname + '/storage/authtokens.json', (err, data) => {
if (err) {
console.error(err);
return res.send({ success: false, message: 'An error occurred in fs.' })
};
var data = JSON.parse(data);
data.push(body);
fs.writeFile(__dirname + '/storage/authtokens.json', JSON.stringify(data, null, 2), (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: JSON.stringify(data, null, 2) })
}
});
});
*/
const body = req.body;
if (!body) return res.send({ success: false })
fs.writeFile(__dirname + '/storage/authtokens.json', JSON.stringify(body), (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: JSON.stringify(body) })
}
})
})
app.get('/giveaways', (req,res) => {
return res.sendFile(__dirname + '/giveaways.json');
})
app.post('/giveaways', (req, res) => {
const body = req.body;
if (!body) return res.send({ success: false })
fs.writeFile(__dirname + '/storage/giveaways.json', JSON.stringify(body), (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: JSON.stringify(body) })
}
})
})
app.get('/donatebot', (req,res) => {
return res.sendFile(__dirname + '/donatebot.json');
})
app.post('/donatebot', (req, res) => {
const body = req.body;
if (!body) return res.send({ success: false })
fs.writeFile(__dirname + '/storage/donatebot.json', JSON.stringify(body), (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: JSON.stringify(body) })
}
})
})
app.get('/backups', (req,res) => {
const id = req.query.id;
if (!id) return res.send({ success: false })
return res.sendFile(__dirname + `/storage/backups/${id}.json`);
})
app.post('/backups', (req, res) => {
const id = req.query.id;
const body = req.body;
if (!body) return res.send({ success: false })
if (!id) return res.send({ success: false })
fs.writeFile(__dirname + `/storage/backups/${id}.json`, JSON.stringify(body), (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: JSON.stringify(body) })
}
})
})
app.use(bodyParser.text())
app.get('/transcripts', (req,res) => {
const id = req.query.id;
if (!id) return res.send({ success: false })
return res.sendFile(__dirname + `/storage/transcripts/${id}.html`);
})
app.post('/transcripts', (req, res) => {
const id = req.query.id;
const body = req.body;
console.log(body)
if (!body) return res.send({ success: false })
if (!id) return res.send({ success: false })
fs.writeFile(__dirname + `/storage/transcripts/${id}.html`, body, (err, result) => {
if (err) {
return res.send({ success: false, message: 'An error occurred in fs.' })
} else {
return res.send({ success: true, body: body })
}
})
})
app.listen(port, () => console.log(`Server listening on port ${port}.`));
process.on('unhandledRejection', (e) => console.log(e));
process.on('rejectionHandled', (e) => console.log(e));
process.on('uncaughtException', (e) => console.log(e));
function alert (error) {
// add later
}