-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
306 lines (257 loc) · 9.48 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const middlewares = require("./helpers/middlewares")
const logger = require('morgan');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const config = require('./config.json');
const ws = require("ws");
const crypto = require("crypto")
require('express-async-errors');
// Return the 'new' updated object by default when doing findByIdAndUpdate
mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', function () {
if (!('new' in this.options)) {
this.setOptions({ new: true });
}
});
});
const Logger = require('./models/log');
const automation = require('./helpers/automation');
const { websocketManager } = require('./helpers/websocket');
const indexRouter = require('./routes/index');
const bnAppRouter = require('./routes/bnApp');
const reportsRouter = require('./routes/reports');
const appEvalRouter = require('./routes/evaluations/appEval');
const bnEvalRouter = require('./routes/evaluations/bnEval').router;
const evalArchiveRouter = require('./routes/evaluations/evalArchive');
const publicArchiveRouter = require('./routes/evaluations/publicArchive');
const yourEvalsRouter = require('./routes/evaluations/yourEvals');
const dataCollectionRouter = require('./routes/dataCollection');
const manageReportsRouter = require('./routes/manageReports');
const usersRouter = require('./routes/users/users');
const natRouter = require('./routes/users/nat');
const vetoesRouter = require('./routes/vetoes');
const discussionVoteRouter = require('./routes/discussionVote');
const qualityAssuranceRouter = require('./routes/qualityAssurance');
const logsRouter = require('./routes/logs');
const interOpRouter = require('./routes/interOp');
const modRequestsRouter = require('./routes/modRequests');
const messageRouter = require('./routes/message');
const settingsRouter = require('./routes/settings');
const spamRouter = require('./routes/spam');
const debugRouter = require('./routes/debug');
const groupHistoryRouter = require('./routes/groupHistory');
const documentationRouter = require('./routes/documentation');
const app = express();
global.ws = []; // array to access websocket clients
const wsServer = new ws.Server({ noServer: true, path: "/websocket/interOp" });
wsServer.on("connection", (socket, request) => {
console.log(`${request.headers['username'] || request.socket.remoteAddress} is connected via websocket`);
});
// discord embeds
app.use(middlewares.discordEmbeds);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// middlewares and such
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//natdb
mongoose.connect(config.connection, {
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'db connection error:'));
db.once('open', function () {
console.log('natdb connected');
});
const sessionConfig = {
name: 'bnsite_session',
secret: config.session,
store: new MongoStore({ mongooseConnection: db }),
resave: false,
saveUninitialized: false,
cookie: {
sameSite: 'lax',
httpOnly: true,
},
};
if (app.get('env') === 'production') {
app.set('trust proxy', 1);
sessionConfig.cookie.secure = true;
}
app.use(session(sessionConfig));
// setup api routes
const apiRouter = express.Router();
apiRouter.use('/', indexRouter);
apiRouter.use('/bnApps', bnAppRouter);
apiRouter.use('/reports', reportsRouter);
apiRouter.use('/appEval', appEvalRouter);
apiRouter.use('/bnEval', bnEvalRouter);
apiRouter.use('/dataCollection', dataCollectionRouter);
apiRouter.use('/evalArchive', evalArchiveRouter);
apiRouter.use('/publicArchive', publicArchiveRouter);
apiRouter.use('/yourEvals', yourEvalsRouter);
apiRouter.use('/manageReports', manageReportsRouter);
apiRouter.use('/users', usersRouter);
apiRouter.use('/users/nat', natRouter);
apiRouter.use('/vetoes', vetoesRouter);
apiRouter.use('/discussionVote', discussionVoteRouter);
apiRouter.use('/qualityassurance', qualityAssuranceRouter);
apiRouter.use('/logs', logsRouter);
apiRouter.use('/interOp', interOpRouter);
apiRouter.use('/modrequests', modRequestsRouter);
apiRouter.use('/message', messageRouter);
apiRouter.use('/settings', settingsRouter);
apiRouter.use('/spam', spamRouter);
apiRouter.use('/debug', debugRouter);
apiRouter.use('/groupHistory', groupHistoryRouter);
apiRouter.use('/docs', documentationRouter);
app.use('/api', apiRouter);
// don't break 3rd party access
app.use('/interOp/*', (req, res) => {
res.redirect(req.originalUrl.replace('/interOp', '/api/interOp'));
});
// catch 404
app.use(function (req, res) {
res.render('index', {
layout: false,
loggedIn: req.session.mongoId,
});
});
// error handler
// eslint-disable-next-line no-unused-vars
app.use(function (err, req, res, next) {
let customErrorMessage;
if (err.name == 'DocumentNotFoundError') customErrorMessage = 'Not found';
if (err instanceof mongoose.Error.ValidationError) {
// @ts-ignore
customErrorMessage = err._message + ':';
const keys = Object.keys(err.errors);
for (const key of keys) {
const error = err.errors[key];
// @ts-ignore
if (error.kind === 'required') {
// @ts-ignore
customErrorMessage += `\n"${error.path}" is missing.`;
// @ts-ignore
} else if (error.kind === 'maxlength') {
// @ts-ignore
customErrorMessage += `\nMax length for input is ${error.properties.maxlength}.`;
} else {
customErrorMessage += '\n' + error.message;
}
}
}
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = {};
if (req.app.get('env') === 'development') {
//console.log(err);
res.locals.error = err;
}
// render the error page or return json error obj
if (req.accepts(['html', 'json']) === 'json') {
res.json({ error: customErrorMessage || err.message || 'Something went wrong!' });
} else {
res.status(err.status || 500);
res.render('error');
}
Logger.generateError(
err.message,
err.stack,
JSON.stringify(err.response),
req.session.mongoId
);
});
// server start
const port = process.env.PORT || '3001';
app.set('port', port);
const server = app.listen(port, () => {
console.log('Listening on ' + port);
if (config.enableAutomation) {
automation.notifyReports.start();
automation.notifyVetoes.start();
automation.notifyApplicationEvaluations.start();
automation.notifyCurrentBnEvaluations.start();
automation.archiveInvalidEvaluations.start();
automation.lockEvaluationMessages.start();
automation.notifyContentReviews.start();
automation.checkBnEvaluationDeadlines.start();
automation.checkTenureValidity.start();
automation.badgeTracker.start();
automation.notifyBeatmapReports.start();
automation.spawnProbationEvaluations.start();
automation.spawnHighActivityEvaluations.start();
}
});
// setup websocket
server.on("upgrade", (request, socket, head) => {
wsServer.handleUpgrade(request, socket, head, (socket, request) => {
const secret = request.headers?.secret;
const username = request.headers?.username;
const tags = request.headers?.tags;
if (!config.interOpAccess[username?.toString()])
return sendError(401, "User not found!");
if (!tags || tags.toString().split("+").length == 0)
return sendError(400, "Provide valid tags to listen to!");
if (
!secret ||
!username ||
config.interOpAccess[username?.toString()].secret !== secret
)
return sendError(401, "Invalid key!");
function sendError(code, text) {
socket.send(
JSON.stringify({
status: code,
statusText: text,
})
);
socket.close();
}
const socketId = crypto.randomBytes(25).toString("hex");
const stopPingListener = () => {
const socketIndex = global.ws.findIndex((s) => s.id == socketId);
if (socketIndex == -1) return;
if (global.ws[socketIndex]) {
clearInterval(global.ws[socketIndex].pingInterval)
return console.log(`Stopped websocket ping listener for ${request.headers['username'] || request.socket.remoteAddress}`);
}
}
(global.ws || []).push({
tags: sanitizeTags(tags),
ws: socket,
pingInterval: setInterval(() => {
socket.send(JSON.stringify({
type: "PING",
data: null
}), (err) => {
if (err) return stopPingListener;
return console.log(`Sent websocket ping for ${request.headers['username'] || request.socket.remoteAddress}`);
})
}, 300000), // Ping at every 5 minutes
id: socketId,
});
socket.onclose = stopPingListener;
wsServer.emit("connection", socket, request);
});
function sanitizeTags(requestTags) {
const tags = requestTags.toString().split("+");
const availableTags = websocketManager.availableTags;
const sanitizedTags = tags
.map((t) => (t || "").toLowerCase().trim())
.filter((t) => t.trim().toLowerCase() != "")
.filter((t) => availableTags.includes(t));
return sanitizedTags;
}
});
module.exports = app;