-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
247 lines (203 loc) · 7.97 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const express = require('express'),
fs = require('fs'),
app = express(),
path = require('path'),
WebSocket = require('ws'),
PORT = process.env.PORT || 3000;
const saveTopicIncrementing = require('./scripts/saveTopicIncrementing.js');
//const readTopics = require('./scripts/readTopics.js');
// Function for incrementing filename when saving it, if it already exists
const writeFileIncrementing = (filename, data, increment = 0) => {
const name = `${path.basename(filename, path.extname(filename))}${increment || ""}${path.extname(filename)}`;
return fs.promises.writeFile(path.join(path.dirname(filename), name), data, { encoding: 'utf8', flag: 'wx' }).catch(ex => {
if (ex.code === "EEXIST") return writeFileIncrementing(filename, data, ++increment);
throw ex;
});
}
// Specify limit for file sizes being saved
app.use(express.json({limit: '25mb'}));
// Set homepage
app.get('/', (req, res) => {
let allTopics = [],
dirname = path.join(__dirname, '/model/topicscache/');
// Check if it's a private information
if (req.body.private) dirname = path.join(__dirname, '/model/private/topicscache');
fs.promises.readdir(dirname, 'utf8')
.then(
topicIds => {
const topicsContents = [];
for (const name of topicIds) {
if (name === '.gitignore') continue;
topicsContents.push(fs.promises.readFile(path.join(dirname, name), 'utf8'));
}
return Promise.all(topicsContents);
}
)
.then(
topicsContents => {
allTopics = topicsContents;
return fs.promises.readFile(path.join(__dirname, '/public/index.html'), 'utf8');
}
)
.then(
page => {
// SECURITY ALERT!
// VERY UNSAFE!!! Attacker can manipulate entry here
// Check needs to happen in the server (but rather on input), because HTML parser runs first than browser JS
console.log(allTopics)
const allTopicsBadlySanitized = ('[' + allTopics.join() + ']').replace(/[<>]/g, (char) => char === '<' ? '<' : '>');
res.send(page.replace(/<body>/i, '<body><x-server-data id="x-server-data" class="hidden">' + allTopicsBadlySanitized + '</x-server-data>'));
}
)
.catch(
(err) => res.status(err.statusCode || 500).send(err)
);
});
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
// hack (while there's no webpack): add path to node module
app.get('/purify.min.js', function(req, res) {
res.sendFile(__dirname + '/node_modules/dompurify/dist/purify.min.js');
});
// API
app.get('/topic/:topicId', (req, res, next) => {
const topicId = req.params.topicId;
let location = path.join(__dirname, '/model/topicscache/', topicId);
// Check if it's a private information
if (req.body.private) location = path.join(__dirname, '/model/private/topicscache/', topicId);
return fs.promises.readFile(location, 'utf8')
.then(contents => res.send(contents))
.catch(next);
});
// This here is weird, for REST this should be GET instead of POST. But GET has char limit
// This is all temporary anyway since I'll use a database afterwards
app.post('/api/infos/', (req, res, next) => {
const infos = req.body;
if (!Array.isArray(infos)) throw new Error('type is not Array');
let dirname = path.join(__dirname, '/model/infos/');
// Check if it's a private information
if (req.body.private) dirname = path.join(__dirname, '/model/private/infos');
const allPromises = [];
for (let id of infos) {
const location = path.join(dirname, id);
allPromises.push(fs.promises.readFile(location, 'utf8'));
}
Promise.all(allPromises).then(contents => res.send(contents)).catch(next);
});
app.post('/api/save/', [superBackup, saveInfo, saveTopic, updateTopicsCache], (req, res) => {
res.sendStatus(200);
req.body.info ? console.log(`Success! Saved topic ${req.body.topic.topicId} and info ${req.body.info.id}`) :
console.log(`Success! Saved topic ${req.body.topic.topicId} and no info`);
});
function superBackup(req, res, next) {
let filename = new Date().toString().replace(/[: ]/g, (char)=> char === ':' ? '-' : '_');
console.log(req, req.body, req.body.info);
writeFileIncrementing(path.join(__dirname, '/superbackup/', filename), JSON.stringify(req.body))
.then((fulfilled) => next(), (err) => next(err));
}
function saveInfo(req, res, next) {
const info = req.body.info;
if (!info) return next();
const filename = info?.id,
infoString = JSON.stringify(info);
let dirname = path.join(__dirname, '/model/infos/', filename);
// Check if it's a private information
if (req.body.private) dirname = path.join(__dirname, '/model/private/infos', filename);
fs.promises.writeFile(dirname, infoString, {flag: 'wx'})
.then((fulfilled) => next(), (err) => next(err));
}
function saveTopic(req, res, next) {
let topic = req.body.topic,
filename = topic.topicId,
dirname = path.join(__dirname, '/model/topics', filename);
// Check if it's a private information
if (req.body.private) dirname = path.join(__dirname, '/model/private/topics', filename);
topic = JSON.stringify(topic);
// If folder doesn't exist, create it
// No need to set folder to 0755, because umask already converts 0777 to 0755
fs.mkdir(dirname, (err) => {
topic.timestamp = new Date().getTime();
if (err) {
if (err.code === 'EEXIST') {
// fs.promises.writeFile(path.join(dirname, filename), order, {flag: 'wx'}).catch(next);
return saveTopicIncrementing(dirname, filename, JSON.stringify(req.body))
.then((fulfilled) => next(), (err) => next(err));
}
else {
next(err);
}
}
// If no error:
else {
return saveTopicIncrementing(dirname, filename, JSON.stringify(req.body))
.then((fulfilled) => next(), (err) => next(err));
}
});
}
function updateTopicsCache(req, res, next) {
let topic = req.body.topic,
filename = topic.topicId,
dirname = path.join(__dirname, '/model/topicscache/', filename);
topic = JSON.stringify(topic);
// Check if it's a private information
if (req.body.private) dirname = path.join(__dirname, '/model/private/topicscache', filename);
return fs.promises.writeFile(dirname, topic, {flag: 'w'})
.then((fulfilled) => next(), (err) => next(err));
}
// Error handler
app.use((err, req, res, next) => {
console.log(req);
console.log(req.body);
console.log(err.statusCode);
console.error(err.stack);
//res.status(err.statusCode || 500).send({error: err, message: err.message});
next(err); // uses Express default error handler
});
// Because of nginx, I think it's ok to omit host here
const server = app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
// WebSocket
//
// EventEmitter for ws
const EventEmitter = require('events'),
wsemitter = new EventEmitter();
// Create WebSocket
const wsserver = new WebSocket.Server({
server: server, // server:server connects to the express server 'app.listen', named 'server'
backlog: 100,
clientTracking: true
});
wsserver.on('connection', function connection(ws) {
// nginx proxy_read_timeout defaults to 1 min, it closes the connection if nothing is by the server
// because it's possible that the connection is closed and nodejs doesn't know it, it makes sense
// to implement ping/pong to also keep the connection alive
ws.isAlive = true;
ws.on('pong', function() {this.isAlive = true}); // must not be arrow function, otherwise 'this' won't work
// ping will be sent below, to all connections
ws.send('apagar connected '+ new Date());
const wsemitterFunction = (change) => {
ws.send(change);
};
// EventEmitter adds the same function over and over, even if it's a named function, so we must check if it has been added already
const added = {};
ws.on('message', function incoming(topicId) {
if (!added[topicId]) {
wsemitter.on(topicId, wsemitterFunction);
added[topicId] = true;
}
ws.send('received, time ' + new Date());
});
ws.on('close', () => {
for (let topicId in added) {
wsemitter.off(topicId, wsemitterFunction);
}
});
});
const interval = setInterval(function ping() {
wsserver.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.close(4000, 'did not receive a ping on time');
ws.isAlive = false;
ws.ping();
});
}, 45000);