-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
861 lines (726 loc) · 28 KB
/
index.js
File metadata and controls
861 lines (726 loc) · 28 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/**
* C³ CELERITY - Management panel for Hysteria 2 nodes
* by Click Connect
*/
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const cron = require('node-cron');
const session = require('express-session');
const RedisStore = require('connect-redis').default;
const path = require('path');
const fs = require('fs');
const { WebSocketServer } = require('ws');
const config = require('./config');
const logger = require('./src/utils/logger');
const requireAuth = require('./src/middleware/auth');
const { requireScope } = requireAuth;
const { i18nMiddleware } = require('./src/middleware/i18n');
const { countRequest } = require('./src/middleware/rpsCounter');
const syncService = require('./src/services/syncService');
const cacheService = require('./src/services/cacheService');
const statsService = require('./src/services/statsService');
const HyUser = require('./src/models/hyUserModel');
const HyNode = require('./src/models/hyNodeModel');
const backupService = require('./src/services/backupService');
const usersRoutes = require('./src/routes/users');
const nodesRoutes = require('./src/routes/nodes');
const cascadeRoutes = require('./src/routes/cascade');
const subscriptionRoutes = require('./src/routes/subscription');
const authRoutes = require('./src/routes/auth');
const panelRoutes = require('./src/routes/panel');
const mcpRoutes = require('./src/routes/mcp');
const helmet = require('helmet');
const app = express();
app.set('trust proxy', 1);
// ==================== MIDDLEWARE ====================
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
}));
app.use(compression({
filter: (req, res) => {
if (req.headers['x-no-compression']) {
return false;
}
return compression.filter(req, res);
},
level: 6,
}));
app.use(cors({
origin: config.BASE_URL,
credentials: true,
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
let sessionMiddleware = null;
function initSessionMiddleware() {
sessionMiddleware = session({
store: new RedisStore({
client: cacheService.redis,
prefix: 'sess:',
}),
secret: config.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: true,
sameSite: 'strict',
maxAge: 24 * 60 * 60 * 1000
}
});
}
app.use((req, res, next) => {
if (sessionMiddleware) {
return sessionMiddleware(req, res, next);
}
next();
});
app.use(cookieParser());
app.use(i18nMiddleware);
app.use(countRequest);
app.use(express.static(path.join(__dirname, 'public'), {
maxAge: process.env.NODE_ENV === 'production' ? '1d' : 0,
}));
// Sanitize error details from 500 responses in production
app.use((req, res, next) => {
const originalJson = res.json.bind(res);
res.json = function(body) {
if (res.statusCode >= 500 && process.env.NODE_ENV !== 'development') {
body = { error: 'Internal Server Error' };
}
return originalJson(body);
};
next();
});
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use((req, res, next) => {
const skipPaths = ['/css', '/js', '/api/auth', '/api/files', '/health'];
const shouldSkip = skipPaths.some(p => req.path.startsWith(p));
if (!shouldSkip) {
logger.debug(`${req.method} ${req.path}`);
}
next();
});
// ==================== HEALTH CHECK ====================
app.get('/health', async (req, res) => {
const cacheStats = await cacheService.getStats();
res.json({
status: 'ok',
uptime: process.uptime(),
lastSync: syncService.lastSyncTime,
isSyncing: syncService.isSyncing,
cache: cacheStats,
});
});
// ==================== API ROUTES ====================
app.use('/api/auth', authRoutes);
const Admin = require('./src/models/adminModel');
const totpService = require('./src/services/totpService');
const rateLimit = require('express-rate-limit');
const API_LOGIN_2FA_PENDING_TTL_MS = 10 * 60 * 1000;
function clearApiLogin2faPending(req) {
if (req.session) {
delete req.session.apiLogin2faPending;
}
}
function isApiLogin2faPendingValid(req) {
const pending = req.session?.apiLogin2faPending;
if (!pending) return false;
if (!pending.createdAt) return false;
return (Date.now() - pending.createdAt) < API_LOGIN_2FA_PENDING_TTL_MS;
}
const apiLoginLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 10,
message: { error: 'Too many attempts. Try again in 15 minutes.' },
});
const apiTotpVerifyLimiter = rateLimit({
windowMs: 10 * 60 * 1000,
max: 8,
message: { error: 'Too many verification attempts. Try again later.' },
});
app.post('/api/login', apiLoginLimiter, async (req, res) => {
try {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password required' });
}
const admin = await Admin.verifyPassword(username, password);
if (!admin) {
logger.warn(`[API] Failed login: ${username} (IP: ${req.ip})`);
return res.status(401).json({ error: 'Invalid username or password' });
}
if (admin.twoFactor?.enabled) {
req.session.apiLogin2faPending = {
username: admin.username,
secretEncrypted: admin.twoFactor.secretEncrypted,
createdAt: Date.now(),
};
delete req.session.authenticated;
delete req.session.adminUsername;
logger.info(`[API] 2FA required for ${admin.username} (IP: ${req.ip})`);
return res.status(202).json({
success: false,
requiresTwoFactor: true,
message: 'Two-factor verification required',
});
}
clearApiLogin2faPending(req);
req.session.authenticated = true;
req.session.adminUsername = admin.username;
await Admin.recordSuccessfulLogin(admin.username);
logger.info(`[API] Login: ${admin.username} (IP: ${req.ip})`);
return res.json({
success: true,
username: admin.username,
message: 'Authentication successful. Use cookies for subsequent requests.',
});
} catch (error) {
return res.status(500).json({ error: error.message });
}
});
app.post('/api/login/totp', apiTotpVerifyLimiter, async (req, res) => {
try {
if (!isApiLogin2faPendingValid(req)) {
clearApiLogin2faPending(req);
return res.status(401).json({ error: 'Invalid username or password' });
}
const token = String(req.body?.token || '').trim();
if (!token) {
return res.status(400).json({ error: 'Verification code required' });
}
const pending = req.session.apiLogin2faPending;
const secret = totpService.decryptSecret(pending.secretEncrypted);
const isValid = await totpService.verifyToken({ secret, token });
if (!isValid) {
logger.warn(`[API] Failed login 2FA confirmation: ${pending.username} (IP: ${req.ip})`);
return res.status(401).json({ error: 'Invalid username or password' });
}
clearApiLogin2faPending(req);
req.session.authenticated = true;
req.session.adminUsername = pending.username;
await Admin.recordSuccessfulLogin(pending.username);
logger.info(`[API] Login with 2FA: ${pending.username} (IP: ${req.ip})`);
return res.json({
success: true,
username: pending.username,
message: 'Authentication successful. Use cookies for subsequent requests.',
});
} catch (error) {
return res.status(500).json({ error: error.message });
}
});
app.post('/api/logout', (req, res) => {
const username = req.session?.adminUsername;
req.session.destroy((err) => {
if (err) logger.error('[API] Session destroy error on logout:', err.message);
if (username) {
logger.info(`[API] Logout: ${username}`);
}
res.json({ success: true });
});
});
const rateLimitSettings = {
subscriptionPerMinute: 100,
authPerSecond: 200,
};
const subscriptionLimiter = rateLimit({
windowMs: 60 * 1000,
max: () => rateLimitSettings.subscriptionPerMinute,
handler: (req, res) => {
logger.warn(`[Sub] Rate limit: ${req.ip}`);
res.status(429).type('text/plain').send('# Too many requests');
},
});
async function reloadSettings() {
const Settings = require('./src/models/settingsModel');
const settings = await Settings.get();
cacheService.updateTTL(settings);
if (settings.rateLimit) {
rateLimitSettings.subscriptionPerMinute = settings.rateLimit.subscriptionPerMinute || 100;
rateLimitSettings.authPerSecond = settings.rateLimit.authPerSecond || 200;
logger.info(`[Settings] Rate limits: sub=${rateLimitSettings.subscriptionPerMinute}/min`);
}
}
module.exports = { reloadSettings };
app.use('/api/files', subscriptionLimiter);
app.use('/api/info', subscriptionLimiter);
app.use('/api', subscriptionRoutes);
app.use('/api/users', requireAuth, usersRoutes);
app.use('/api/nodes', requireAuth, nodesRoutes);
app.use('/api/cascade', requireAuth, cascadeRoutes);
app.use('/api/mcp', requireAuth, mcpRoutes);
app.get('/api/groups', requireAuth, requireScope('stats:read'), async (req, res) => {
try {
const { getActiveGroups } = require('./src/utils/helpers');
const groups = await getActiveGroups();
res.json(groups.map(g => ({ _id: g._id, name: g.name })));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/stats', requireAuth, requireScope('stats:read'), async (req, res) => {
try {
const [usersTotal, usersEnabled, nodesTotal, nodesOnline] = await Promise.all([
HyUser.countDocuments(),
HyUser.countDocuments({ enabled: true }),
HyNode.countDocuments(),
HyNode.countDocuments({ status: 'online' }),
]);
const nodes = await HyNode.find({ active: true }).select('name onlineUsers');
const totalOnline = nodes.reduce((sum, n) => sum + (n.onlineUsers || 0), 0);
res.json({
users: { total: usersTotal, enabled: usersEnabled },
nodes: { total: nodesTotal, online: nodesOnline },
onlineUsers: totalOnline,
nodesList: nodes.map(n => ({ name: n.name, online: n.onlineUsers })),
lastSync: syncService.lastSyncTime,
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/api/sync', requireAuth, requireScope('sync:write'), async (req, res) => {
if (syncService.isSyncing) {
return res.status(409).json({ error: 'Sync already in progress' });
}
syncService.syncAllNodes().catch(err => {
logger.error(`[API] Sync error: ${err.message}`);
});
res.json({ message: 'Sync started' });
});
app.post('/api/kick/:userId', requireAuth, requireScope('sync:write'), async (req, res) => {
try {
await syncService.kickUser(req.params.userId);
await cacheService.clearDeviceIPs(req.params.userId);
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// ==================== API DOCS ====================
if (config.API_DOCS_ENABLED) {
const { buildSpec } = require('./src/docs/openapi');
// Serve spec in requested language (?lang=ru|en)
app.get('/api/docs/openapi.json', (req, res) => {
const lang = req.query.lang === 'ru' ? 'ru' : 'en';
res.json(buildSpec(lang));
});
app.get('/api/docs', (req, res) => {
const lang = req.query.lang === 'ru' ? 'ru' : 'en';
const otherLang = lang === 'ru' ? 'en' : 'ru';
const otherLabel = lang === 'ru' ? 'English' : 'Русский';
const specUrl = `/api/docs/openapi.json?lang=${lang}`;
res.send(`<!doctype html>
<html>
<head>
<title>C³ CELERITY — API Reference</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style>
body { margin: 0; }
#lang-toggle {
position: fixed; top: 14px; right: 16px; z-index: 9999;
padding: 5px 12px; border-radius: 6px; border: 1px solid #a78bfa;
background: #1e1e2e; color: #a78bfa; font-size: 13px;
cursor: pointer; text-decoration: none; font-family: sans-serif;
}
#lang-toggle:hover { background: #2e2e3e; }
</style>
</head>
<body>
<a id="lang-toggle" href="/api/docs?lang=${otherLang}">${otherLabel}</a>
<script
id="api-reference"
data-url="${specUrl}"
data-configuration='{"theme":"purple","layout":"modern"}'
></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>`);
});
logger.info('[Docs] API docs available at /api/docs');
}
// ==================== WEB PANEL ====================
app.use('/panel', panelRoutes);
app.get('/', (req, res) => {
res.redirect('/panel');
});
// ==================== ERROR HANDLING ====================
// 404
app.use((req, res) => {
if (req.path.startsWith('/api')) {
res.status(404).json({ error: 'Not Found' });
} else {
res.status(404).send('404 - Not Found');
}
});
// Error handler
app.use((err, req, res, next) => {
logger.error(`[Error] ${err.message}`);
const msg = process.env.NODE_ENV !== 'development' ? 'Internal Server Error' : err.message;
if (req.path.startsWith('/api')) {
res.status(500).json({ error: msg });
} else {
res.status(500).send('Internal Server Error');
}
});
// ==================== START SERVER ====================
async function connectMongo(retries = 5, delayMs = 3000) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
await mongoose.connect(config.MONGO_URI, {
maxPoolSize: 10,
minPoolSize: 2,
serverSelectionTimeoutMS: 10000,
socketTimeoutMS: 45000,
});
return;
} catch (err) {
if (attempt === retries) throw err;
logger.warn(`[MongoDB] Connection attempt ${attempt}/${retries} failed: ${err.message}. Retrying in ${delayMs / 1000}s...`);
await new Promise(r => setTimeout(r, delayMs));
delayMs = Math.min(delayMs * 2, 30000);
}
}
}
async function startServer() {
try {
await connectMongo();
logger.info('[MongoDB] Connected');
await cacheService.connect();
initSessionMiddleware();
logger.info('[Redis] Session store initialized');
// Migration: ensure all users have xrayUuid (for Xray VLESS support)
const usersWithoutUuid = await HyUser.find({
$or: [{ xrayUuid: { $exists: false } }, { xrayUuid: null }, { xrayUuid: '' }]
}).select('_id');
if (usersWithoutUuid.length > 0) {
const crypto = require('crypto');
const bulkOps = usersWithoutUuid.map(u => ({
updateOne: {
filter: { _id: u._id },
update: { $set: { xrayUuid: crypto.randomUUID() } }
}
}));
await HyUser.bulkWrite(bulkOps, { ordered: false });
logger.info(`[Migration] Generated xrayUuid for ${usersWithoutUuid.length} existing users`);
}
await reloadSettings();
const PORT = process.env.PORT || 3000;
const useCaddy = process.env.USE_CADDY === 'true';
if (useCaddy) {
const http = require('http');
const server = http.createServer(app);
setupWebSocketServer(server);
activeServers.push(server);
server.listen(PORT, () => {
logger.info(`[Server] HTTP listening on port ${PORT} (behind Caddy)`);
logger.info(`[Server] Panel: https://${config.PANEL_DOMAIN}/panel`);
});
} else {
// Standalone with Greenlock (for local development)
logger.info(`[Server] Starting HTTPS for ${config.PANEL_DOMAIN}`);
const Greenlock = require('@root/greenlock-express');
const greenlockDir = path.join(__dirname, 'greenlock.d');
const livePath = path.join(greenlockDir, 'live', config.PANEL_DOMAIN);
if (!fs.existsSync(livePath)) {
fs.mkdirSync(livePath, { recursive: true });
}
const configPath = path.join(greenlockDir, 'config.json');
try {
const glConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const siteExists = glConfig.sites.some(s => s.subject === config.PANEL_DOMAIN);
if (!siteExists) {
glConfig.sites.push({
subject: config.PANEL_DOMAIN,
altnames: [config.PANEL_DOMAIN],
});
}
glConfig.defaults.subscriberEmail = config.ACME_EMAIL;
glConfig.defaults.store = {
module: 'greenlock-store-fs',
basePath: greenlockDir,
};
fs.writeFileSync(configPath, JSON.stringify(glConfig, null, 2));
} catch (err) {
logger.warn(`[Greenlock] Config error: ${err.message}`);
}
const glInstance = Greenlock.init({
packageRoot: __dirname,
configDir: greenlockDir,
maintainerEmail: config.ACME_EMAIL,
cluster: false,
staging: false,
});
glInstance.ready((glx) => {
const httpServer = glx.httpServer();
activeServers.push(httpServer);
httpServer.listen(80, () => {
logger.info('[Server] HTTP listening on port 80');
});
const httpsServer = glx.httpsServer(null, app);
setupWebSocketServer(httpsServer);
activeServers.push(httpsServer);
httpsServer.listen(443, () => {
logger.info('[Server] HTTPS listening on port 443');
logger.info(`[Server] Panel: https://${config.PANEL_DOMAIN}/panel`);
});
});
}
// Cron jobs
setupCronJobs();
} catch (err) {
logger.error(`[Server] Startup failed: ${err.message}`);
process.exit(1);
}
}
function setupWebSocketServer(server) {
const wssTerminal = new WebSocketServer({ noServer: true });
const wssLogs = new WebSocketServer({ noServer: true });
const sshTerminal = require('./src/services/sshTerminal');
const crypto = require('crypto');
server.on('upgrade', (request, socket, head) => {
const pathname = request.url;
const fakeRes = {
writeHead: () => {},
end: () => {},
write: () => {},
getHeader: () => {},
setHeader: () => {},
};
sessionMiddleware(request, fakeRes, () => {
if (!request.session?.authenticated) {
logger.warn(`[WS] Unauthorized upgrade attempt: ${request.socket.remoteAddress}`);
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
if (pathname && pathname.startsWith('/ws/terminal/')) {
wssTerminal.handleUpgrade(request, socket, head, (ws) => {
wssTerminal.emit('connection', ws, request);
});
} else if (pathname === '/ws/logs') {
wssLogs.handleUpgrade(request, socket, head, (ws) => {
wssLogs.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
});
// SSH Terminal WebSocket
wssTerminal.on('connection', async (ws, req) => {
const urlParts = req.url.split('/');
const nodeId = urlParts[urlParts.length - 1];
const sessionId = crypto.randomUUID();
logger.info(`[WS] SSH terminal for node ${nodeId}`);
try {
const node = await HyNode.findById(nodeId);
if (!node) {
ws.send(JSON.stringify({ type: 'error', message: 'Node not found' }));
ws.close();
return;
}
if (!node.ssh?.password && !node.ssh?.privateKey) {
ws.send(JSON.stringify({ type: 'error', message: 'SSH credentials not configured' }));
ws.close();
return;
}
await sshTerminal.createSession(sessionId, node, ws);
ws.send(JSON.stringify({ type: 'connected', sessionId }));
ws.on('message', (message) => {
try {
const msg = JSON.parse(message.toString());
switch (msg.type) {
case 'input':
sshTerminal.write(sessionId, msg.data);
break;
case 'resize':
sshTerminal.resize(sessionId, msg.cols, msg.rows);
break;
}
} catch (err) {
logger.error(`[WS] Error: ${err.message}`);
}
});
ws.on('close', () => {
logger.info(`[WS] Connection closed for node ${nodeId}`);
sshTerminal.closeSession(sessionId);
});
} catch (error) {
logger.error(`[WS] Terminal error: ${error.message}`);
ws.send(JSON.stringify({ type: 'error', message: error.message }));
ws.close();
}
});
// Real-time Logs WebSocket
wssLogs.on('connection', (ws) => {
logger.info(`[WS] Logs stream connected`);
// Send recent logs buffer on connect
const recentLogs = logger.getRecentLogs();
ws.send(JSON.stringify({ type: 'history', logs: recentLogs }));
// Subscribe to new logs
const onLog = (logEntry) => {
if (ws.readyState === 1) { // WebSocket.OPEN
ws.send(JSON.stringify({ type: 'log', ...logEntry }));
}
};
logger.logEmitter.on('log', onLog);
ws.on('close', () => {
logger.logEmitter.off('log', onLog);
logger.info(`[WS] Logs stream disconnected`);
});
ws.on('error', () => {
logger.logEmitter.off('log', onLog);
});
});
logger.info('[WS] WebSocket server initialized (terminal + logs)');
}
function setupCronJobs() {
// Collect stats every 5 minutes
cron.schedule('*/5 * * * *', async () => {
try {
logger.debug('[Cron] Collecting stats');
await syncService.collectAllStats();
// Save stats snapshot for charts
await statsService.saveHourlySnapshot();
} catch (error) {
logger.error(`[Cron] Stats collection failed: ${error.message}`);
}
});
// Health check every minute
cron.schedule('* * * * *', async () => {
try {
await syncService.healthCheck();
} catch (error) {
logger.error(`[Cron] Health check failed: ${error.message}`);
}
try {
const cascadeService = require('./src/services/cascadeService');
await cascadeService.healthCheckAll();
} catch (error) {
logger.error(`[Cron] Cascade health check failed: ${error.message}`);
}
});
// Save daily snapshot every hour
cron.schedule('0 * * * *', async () => {
try {
logger.debug('[Cron] Saving daily stats snapshot');
await statsService.saveDailySnapshot();
} catch (error) {
logger.error(`[Cron] Daily snapshot failed: ${error.message}`);
}
});
// Save monthly snapshot and cleanup at 00:05
cron.schedule('5 0 * * *', async () => {
try {
logger.info('[Cron] Saving monthly stats snapshot');
await statsService.saveMonthlySnapshot();
await statsService.cleanup();
} catch (error) {
logger.error(`[Cron] Monthly maintenance failed: ${error.message}`);
}
});
// Clean old logs daily at 3:00
cron.schedule('0 3 * * *', () => {
try {
logger.info('[Cron] Cleaning old logs');
cleanOldLogs(30);
} catch (error) {
logger.error(`[Cron] Log cleanup failed: ${error.message}`);
}
});
// Check for scheduled backup every hour
cron.schedule('0 * * * *', async () => {
try {
await backupService.scheduledBackup();
} catch (error) {
logger.error(`[Cron] Scheduled backup failed: ${error.message}`);
}
});
// Initial health check and stats snapshot after 5 seconds
setTimeout(async () => {
try {
logger.info('[Startup] Checking nodes status');
await syncService.healthCheck();
// Initial stats snapshot
await statsService.saveHourlySnapshot();
logger.info('[Startup] Initial stats snapshot saved');
} catch (error) {
logger.error(`[Startup] Initial checks failed: ${error.message}`);
}
}, 5000);
}
/**
* Clean logs older than N days
*/
function cleanOldLogs(days) {
try {
const logsDir = path.join(__dirname, 'logs');
if (!fs.existsSync(logsDir)) {
return;
}
const files = fs.readdirSync(logsDir);
const now = Date.now();
const maxAge = days * 24 * 60 * 60 * 1000;
// Active Winston files (skip)
const activeFiles = ['error.log', 'combined.log'];
for (let i = 1; i <= 5; i++) {
activeFiles.push(`combined${i}.log`);
}
let deleted = 0;
files.forEach(file => {
if (activeFiles.includes(file)) {
return;
}
const filePath = path.join(logsDir, file);
const stats = fs.statSync(filePath);
if (now - stats.mtime.getTime() > maxAge) {
fs.unlinkSync(filePath);
deleted++;
logger.info(`[Cleanup] Deleted old log: ${file}`);
}
});
if (deleted > 0) {
logger.info(`[Cleanup] Removed ${deleted} old log files`);
}
} catch (err) {
logger.error(`[Cleanup] Failed to clean logs: ${err.message}`);
}
}
// Graceful shutdown
const activeServers = [];
const FORCE_SHUTDOWN_MS = 10000;
let isShuttingDown = false;
async function shutdown(signal) {
if (isShuttingDown) return;
isShuttingDown = true;
logger.info(`[Server] ${signal} received, shutting down...`);
await Promise.all(
activeServers.map(s => new Promise(resolve => s.close(resolve)))
);
if (cacheService.isConnected()) {
await cacheService.redis.quit().catch(() => {});
}
await mongoose.disconnect().catch(() => {});
process.exit(0);
}
for (const sig of ['SIGTERM', 'SIGINT']) {
process.once(sig, () => {
setTimeout(() => process.exit(1), FORCE_SHUTDOWN_MS).unref();
shutdown(sig).catch(() => process.exit(1));
});
}
process.on('unhandledRejection', (reason) => {
logger.error('[Process] Unhandled rejection:', reason);
});
process.on('uncaughtException', (err) => {
logger.error('[Process] Uncaught exception:', err);
shutdown('uncaughtException').catch(() => process.exit(1));
});
startServer();