-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
126 lines (110 loc) · 3.85 KB
/
Copy pathserver.js
File metadata and controls
126 lines (110 loc) · 3.85 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
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const helmet = require('helmet');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const app = express();
const server = require('http').createServer(app);
const WebSocket = require('ws');
const wss = new WebSocket.Server({ server });
// Modules
const { initRedis } = require('./src/redis');
const { initDiscord } = require('./src/discord');
const { initFirewall } = require('./src/firewall');
const { initMonitoring } = require('./src/monitoring');
const { initEnhancedProtection } = require('./src/enhanced-protection');
const { setupAutoRenewal } = require('./src/ssl-manager-simple');
const { setupDefaultServer } = require('./src/nginx-manager-simple');
const routes = require('./src/routes');
const captchaHandler = require('./src/captcha');
const logger = require('./src/logger');
// Middleware
app.use(helmet({
contentSecurityPolicy: false
}));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session({
secret: process.env.JWT_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: false, maxAge: 24 * 60 * 60 * 1000 }
}));
// Static files
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.use('/api', routes);
app.use('/captcha', captchaHandler);
// Page principale
app.get('/', (req, res) => {
if (!req.session.authenticated) {
return res.sendFile(path.join(__dirname, 'public', 'login-dark.html'));
}
res.sendFile(path.join(__dirname, 'public', 'dashboard.html'));
});
// WebSocket pour les mises à jour en temps réel
wss.on('connection', (ws) => {
logger.info('WebSocket client connected');
ws.on('message', (message) => {
try {
const data = JSON.parse(message);
logger.debug('WebSocket message received', data);
} catch (err) {
logger.error('WebSocket message error', err);
}
});
ws.on('close', () => {
logger.info('WebSocket client disconnected');
});
});
// Export pour broadcast
global.wsBroadcast = (data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
};
// Initialisation
async function init() {
try {
// Créer les dossiers nécessaires
['logs', 'data', 'ssl'].forEach(dir => {
const dirPath = path.join(__dirname, dir);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
});
await initRedis();
await initDiscord();
await initFirewall();
await initMonitoring();
await initEnhancedProtection(); // Protection DDoS avancée
setupAutoRenewal(); // Renouvellement SSL automatique
await setupDefaultServer(); // Page 502 par défaut
const PORT = process.env.PANEL_PORT || 46789;
server.listen(PORT, () => {
logger.info(`Anti-DDoS Protection System started on port ${PORT}`);
console.log(`\n=== Anti-DDoS System Ready ===`);
console.log(`Panel: http://localhost:${PORT}`);
console.log(`===============================\n`);
});
} catch (error) {
logger.error('Failed to start server', error);
process.exit(1);
}
}
// Gestion des erreurs
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception', error);
});
process.on('unhandledRejection', (error) => {
logger.error('Unhandled Rejection', error);
});
init();