-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
454 lines (431 loc) · 20.6 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
process.env.TZ = 'GMT';
require('log-timestamp')(function () { return `[${new Date().toISOString()}] [mngr] %s` });
const { ShardingManager } = require('discord.js');
const { AutoPoster } = require('topgg-autoposter');
const path = require('path');
const fetch = require('node-fetch');
const url = require('url');
const { connect, disconnect, STATES, connection } = require('mongoose');
const NodeCache = require("node-cache");
const GuildModel = require('./models/Guild');
const EventModel = require('./models/Event');
const UserModel = require('./models/User');
global.Config = require(path.resolve(process.env.CONFIGDIR || __dirname, './config.json'));
global.GuildCache = new NodeCache({ stdTTL: 86400, checkperiod: 14400 });
const timezones = require('./handlers/timezones.js');
const calendar = require('./handlers/calendar.js');
const express = require('express');
const session = require('express-session');
const morgan = require('morgan');
const Grant = require('grant').express();
const grant = new Grant(Config);
const net = require('net')
// am I in the process of shutting down?
let shutdown = false;
/**
* connect to the mongodb
*/
(async () => {
console.log('mongo user: %s ... connecting', Config.mongoUser);
await connect('mongodb://' + Config.mongoUser + ':' + Config.mongoPass + '@' + Config.mongoServer + ':' + Config.mongoPort + '/' + Config.mongoSchema + '?authSource=' + Config.mongoSchema, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
useCreateIndex: true
});
console.log('Manager connected to mongo.');
})();
/**
* invoke shardingmanager
*/
const manager = new ShardingManager('./bot.js', { token: Config.token, respawn: false, totalShards: Config.totalShards });
manager.on('shardCreate', (shard) => {
console.log(`===== Launched shard ${shard.id} =====`);
shard.on('death', (process) => {
console.log(`===== Shard ${shard.id} died with exitcode ${process.exitCode}; shutdown status is ${shutdown} =====`);
if (!shutdown) {
console.error(`Shard ${shard.id} should not have shutdown, something is awry, shutting down server completely.`);
cleanShutdown(true);
}
});
});
if (!Config.debugGuild && Config.topggToken) {
// TOP.GG stats poster
const poster = AutoPoster(Config.topggToken, manager);
poster.on('posted', () => {
console.log('TOP.GG: Posted stats');
});
}
manager.spawn();
const ROUTE_ROOT = '/';
const ROUTE_POSTOAUTH = '/postoauth';
const ROUTE_CALENDAR = '/calendar';
const ROUTE_TIMEZONES = '/timezones';
const ROUTE_TIMEZONESSET = '/timezones/set';
const ROUTE_EVENTS = '/events';
const ROUTE_EVENTSSET = '/events/set';
const ROUTE_HEALTH = '/health';
const ROUTE_LOGOUT = '/logout';
let app = express();
app.locals.pretty = true;
let server = app
.set('trust proxy', true)
.use(morgan('[:date[iso]] [mngr: HTTP] :remote-addr - :remote-user ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'))
.set('views', Config.httpPugDir)
.set('view engine', 'pug')
.use(session({ secret: 'grant', saveUninitialized: true, resave: false, maxAge: Date.now() + (7 * 86400 * 1000) }))
.use(grant)
.use(ROUTE_ROOT, express.static(Config.httpStaticDir))
.use(express.json())
.get(ROUTE_HEALTH, async (request, response) => {
if (STATES[STATES.connected] == STATES[connection.readyState]) {
response.json({ status: 'UP', dbState: STATES[connection.readyState] });
} else {
response.json({ status: 'DOWN', dbState: STATES[connection.readyState] });
}
})
.use(async function (request, response, next) {
console.log(`HTTP: in middleware checking if I need to update guildID (and channelID), guildID status: ${request.session.guildConfig ? true : false}`);
try {
// fix for IPv6 literal bug in legacy url
let hostname = request.headers.host;
if (net.isIPv6(hostname)) hostname = '[' + hostname + ']'
const requestUrl = new URL(request.url, `${request.protocol}://${hostname}`);
console.debug(`HTTP: the url formed: ${requestUrl}`);
const guildID = requestUrl.searchParams.get('guildID');
if (guildID) {
if (!request.session.guildConfig || request.session.guildConfig.guildID != guildID) {
console.log(`HTTP: Retrieving guild info for ${guildID}`);
const guildConfig = await GuildModel.findOne({ guildID: guildID });
if (guildConfig) {
request.session.guildConfig = guildConfig;
}
}
} else if (!request.session.guildConfig && request.session.discordMe) {
console.log(`HTTP: Retrieving any guild for user, ${request.session.discordMe.id}`);
const userConfig = await UserModel.findOne({ userID: request.session.discordMe.id });
//@todo this doesn't use the confirmGuildConfig and really should ... however, i need to make it distributed cluster aware first.
const guildConfig = userConfig ? await GuildModel.findOne({ guildID: userConfig.guildID }) : undefined;
if (guildConfig) {
request.session.guildConfig = guildConfig;
}
} else {
console.log(`HTTP: Don't need to (or can't) retrieve a guild ...`);
}
const channelID = requestUrl.searchParams.get('channel');
if (channelID) {
request.session.channelID = channelID;
}
next();
} catch (error) {
console.error("HTTP: guildID/channelID middleware error", error);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end("ERROR PROCESSING");
}
})
.get(ROUTE_ROOT, function (request, response) {
response.render('index', { title: 'Home', Config: Config, guildConfig: request.session.guildConfig, discordMe: request.session.discordMe });
})
.get(ROUTE_LOGOUT, async (request, response) => {
try {
console.log('HTTP: serving ' + ROUTE_LOGOUT);
request.session.discordMe = undefined;
console.debug('base url: ', request.baseUrl);
response.redirect(ROUTE_ROOT);
}
catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
.get(ROUTE_POSTOAUTH, async (request, response) => {
try {
console.info('HTTP: serving ' + ROUTE_POSTOAUTH);
// let requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
if (!request.session.grant || !request.session.grant.response || !request.session.grant.response.raw) {
// console.log('grant config', grant.config);
response.redirect(grant.config.discord.prefix + "/discord");
} else if (request.session.grant.response.error) {
throw new Error(`Discord API error: ${request.session.grant.response.error.error}`);
} else {
// console.log(`oauth2 grant response info`, request.session.grant);
if (!request.session.discordMe) {
console.info('HTTP: Making discord.com/api/users/@me call');
let discordMeResponse = await fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `${request.session.grant.response.raw.token_type} ${request.session.grant.response.access_token}`,
},
});
let discordMe = await discordMeResponse.json();
if (discordMeResponse.status != 200 || discordMe.error) {
throw new Error(`Discord response code; ${discordMeResponse.status} Discord API error: ${discordMe.error}`);
}
request.session.discordMe = discordMe;
}
var queryString = Object.keys(request.session.grant.dynamic).map(key => key + '=' + request.session.grant.dynamic[key]).join('&');
console.info(`HTTP: redirect to actual page requested ${request.session.grant.dynamic.destination}?${queryString}`);
response.redirect(`${request.session.grant.dynamic.destination}?${queryString}`);
}
} catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
.get(ROUTE_CALENDAR, async (request, response) => {
try {
console.log('HTTP: serving ' + ROUTE_CALENDAR);
const requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
let userID = requestUrl.searchParams.get('userID');
const interactive = requestUrl.searchParams.get('interactive');
const excludeGuild = requestUrl.searchParams.get('exclude') ? requestUrl.searchParams.get('exclude').split(',') : [];
if (!userID && request.session.discordMe) {
// console.log(`have discordMe, setting userID`);
userID = request.session.discordMe.id;
}
if (!userID) {
// console.log(`don't have userID, redirecting to discord to login`);
request.query.destination = ROUTE_CALENDAR;
response.redirect(url.format({
pathname: grant.config.discord.prefix + "/discord",
query: request.query,
}));
} else {
// console.log(`have userid, heading to handleCalendarRequest`);
let responseContent = await calendar.handleCalendarRequest(userID, excludeGuild);
if (interactive || !request.session.discordMe) {
response.setHeader('Content-Type', 'text/calendar');
response.end(responseContent);
}
else if (request.session.discordMe) {
response.render('calendar', { title: 'Calendar', Config: Config, guildConfig: request.session.guildConfig, discordMe: request.session.discordMe });
} else {
throw new Error('Unkown Request');
}
}
} catch (error) {
console.error('calendar:', error);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
// all routes past this point require authentication
.use(async function (request, response, next) {
const desiredDestination = request.path;
console.log(`HTTP: in middleware, ensuring user is logged in for ${desiredDestination}`);
// console.log('desiredDestination ' + desiredDestination);
if (!request.session.discordMe) {
console.log(`HTTP: user is _not_ logged in, redirecting`);
request.query.destination = desiredDestination;
response.redirect(url.format({
pathname: grant.config.discord.prefix + "/discord",
query: request.query,
}));
} else {
console.log(`HTTP: ${request.session.discordMe.username} user is logged in`);
next();
}
})
.get(ROUTE_TIMEZONESSET, async function (request, response) {
try {
console.log('HTTP: serving ' + ROUTE_TIMEZONESSET);
if (request.session.guildConfig) {
console.log('HTTP: we know the guild so we can set the timezone for user.');
let requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
// console.log(request.session.discordMe);
// console.log(requestUrl);
const timezoneToSet = requestUrl.searchParams.get('timezone');
let status = await manager.broadcastEval(async (client, { discordMeId, channelId, timezoneToSet, guildId }) => {
return await client.dnd_users.bc_setUsersTimezone(
discordMeId,
channelId,
timezoneToSet,
guildId);
}, {
context: {
discordMeId: request.session.discordMe.id,
channelId: request.session.channelID,
timezoneToSet: timezoneToSet,
guildId: request.session.guildConfig.guildID
}
});
console.log(`HTTP: users.bc_setUsersTimezone response: ${status.includes(true)}`);
response.json({ status: status.includes(true) });
} else {
console.log(`HTTP: we don't know the guild so we will error and let the user copy/paste`);
response.json({ status: 'false' });
}
} catch (error) {
console.error(`error: route: ${ROUTE_TIMEZONESSET} - ${error.message}`);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.json({ status: 'false' });
}
})
.get(ROUTE_TIMEZONES, function (request, response) {
try {
console.log('HTTP: serving ' + ROUTE_TIMEZONES);
let requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
// console.log(request.session.discordMe);
let responseData = timezones.handleTimezonesDataRequest(requestUrl);
response.render('timezones', { title: 'Timezones', timezoneData: responseData, Config: Config, guildConfig: request.session.guildConfig, discordMe: request.session.discordMe })
} catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
.get(ROUTE_EVENTS, async function (request, response) {
try {
console.log('HTTP: serving ' + ROUTE_EVENTS);
let requestUrl = new URL(request.url, `${request.protocol}://${request.headers.host}`);
// console.log(request.session.discordMe);
let eventID = requestUrl.searchParams.get('eventID');
let event = await EventModel.findById(eventID);
if (event?.userID != request.session.discordMe.id) {
console.log(`HTTP: event is not owned by current user, dereferencing`);
event = undefined;
}
let userConfig = await UserModel.findOne({ userID: request.session.discordMe.id, guildID: request.session.guildConfig.guildID });
// console.log(userConfig);
response.render('events', { title: 'Events', event: event, Config: Config, guildConfig: request.session.guildConfig, discordMe: request.session.discordMe, userConfig: userConfig })
} catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
.post(ROUTE_EVENTSSET, async (request, response) => {
try {
console.log('HTTP: serving ' + ROUTE_EVENTSSET);
// console.log(request.body);
let status = false;
if (request.session.guildConfig) {
if (request.body._id) {
console.log(`HTTP: must be an edit ... _id exists ${request.body._id}`);
let channelIDForEvent = request.session.guildConfig.channelForEvents ? request.session.guildConfig.channelForEvents : request.session.channelID;
// @todo build eventString;
let eventString = '';
//eventID, currUserId, channelIDForEvent, guildID, guildApprovalRole, eventString
status = await manager.broadcastEval(async (client, { bodyId, discordMeId, channelIDForEvent, guildId, arole, eventRequireApprover, eventString }) => {
await client.dnd_users.bc_eventEdit(
bodyId,
discordMeId,
channelIDForEvent,
guildId,
arole,
eventRequireApprover,
eventString)
}, {
context: {
bodyId: request.body._id,
discordMeId: request.session.discordMe.id,
channelIDForEvent: channelIDForEvent,
guildId: request.session.guildConfig.guildID,
arole: request.session.guildConfig.arole,
eventRequireApprover: request.session.guildConfig.eventRequireApprover,
eventString: eventString,
}
});
} else {
// @todo implement create
console.log(`HTTP: new event, no _id`);
//currUserId, channelIDForEvent, guildID, eventString
status = await manager.broadcastEval(async (client, { discordMeId, channelIDForEvent, timezoneToSet, guildId, arole, eventRequireApprover }) => {
await client.dnd_users.bc_eventCreate(
discordMeId,
channelIDForEvent,
timezoneToSet,
guildId,
arole,
eventRequireApprover
)
}, {
context: {
discordMeId: request.session.discordMe.id,
channelIDForEvent: request.session.grant.dynamic.channel,
timezoneToSet: 'EST',//timezoneToSet,
guildId: request.session.guildConfig.guildID,
arole: request.session.guildConfig.arole,
eventRequireApprover: request.session.guildConfig.eventRequireApprover,
}
});
}
}
response.json({ status: status });
} catch (error) {
console.error(error.message);
response.setHeader('Content-Type', 'text/html');
response.status(500);
response.end(error.message);
}
})
.listen(Config.httpServerPort);
console.log('HTTP: http server listening on: %s', Config.httpServerPort);
// process.on('exit', () => {
// console.info('exit signal received.');
// cleanShutdown(false);
// });
process.on('SIGTERM', async () => {
console.info('SIGTERM signal received.');
await cleanShutdown(true);
});
process.on('SIGINT', async () => {
console.info('SIGINT signal received.');
await cleanShutdown(true);
});
process.on('SIGUSR1', async () => {
console.info('SIGUSR1 signal received.');
await cleanShutdown(true);
});
process.on('SIGUSR2', async () => {
console.info('SIGUSR2 signal received.');
await cleanShutdown(true);
});
process.on('uncaughtException', async (error) => {
console.info('uncaughtException signal received.', error);
// await cleanShutdown(true);
});
/**
*
* @param {boolean} callProcessExit
*/
async function cleanShutdown(callProcessExit) {
shutdown = true;
try {
console.log('Closing out manager resources...');
await server.close();
console.log('Http server closed.');
for (let [number, shard] of manager.shards) {
if (manager.mode == 'process') {
let count = 0;
while (shard.process?.exitCode === null) {
if (++count > 5) {
shard.kill();
}
console.log(`awaiting shard ${number} to exit`);
await new Promise(resolve => setTimeout(resolve, 500));
}
// } else if (manager.mode == 'worker') {
} else {
console.error(`unknown sharding manager mode: ${manager.mode}`);
}
}
console.log('All shards shutdown.');
await disconnect();
console.log('MongoDb connection closed.');
} catch (error) {
console.error("caught error shutting down shardmanager", error);
}
if (callProcessExit) {
console.log('Exiting.');
process.exit(0);
}
}