-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
289 lines (252 loc) · 9.48 KB
/
Copy pathindex.js
File metadata and controls
289 lines (252 loc) · 9.48 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
require('dotenv').config();
const express = require('express');
const app = express();
const passport = require('passport');
const { Strategy } = require('passport-discord');
const server = require('http').createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
const session = require('express-session');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const csrf = require('csrf');
const tokens = new csrf();
global.someKeys = new Map();
const path = require('path');
const { Client, GatewayIntentBits, WebhookClient } = require('discord.js');
// Initialize Discord client with required intents
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
],
allowedMentions: { parse: [] }
});
const { processFrontEndMessage, validInvs, parseDate, isValidEmoji } = require('./renderMessage.js');
// Updated webhook initialization
const webhook = new WebhookClient({
id: process.env.ID_WH,
token: process.env.TOKEN_WH
});
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
const scopes = ['identify'];
passport.use(
new Strategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: `${process.env.URL}/login`,
scope: scopes,
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
)
);
// Rate limiting middleware
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
app.use(express.json())
.use(express.urlencoded({ extended: true }))
.engine('html', require('ejs').renderFile)
.use(express.static(path.join(__dirname, '/public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
// Security enhancements
.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "code.jquery.com", "cdnjs.cloudflare.com"],
styleSrc: ["'self'", "'unsafe-inline'", "fonts.googleapis.com", "cdnjs.cloudflare.com"],
fontSrc: ["'self'", "fonts.gstatic.com"],
imgSrc: ["'self'", "data:", "cdn.discordapp.com", "i.imgur.com", "*.discordapp.net"],
connectSrc: ["'self'", "ws://localhost:*", "wss://*"]
}
}
}))
.use(
session({
secret: process.env.SESSION_SECRET || 'mychat_secret',
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
})
)
.use((req, res, next) => {
if (!req.session.csrfSecret) {
req.session.csrfSecret = tokens.secretSync();
}
req.csrfToken = function() {
return tokens.create(req.session.csrfSecret);
};
next();
})
.use(passport.initialize())
.use(passport.session())
.use(apiLimiter) // Apply rate limiting to all routes
.use(function (req, res, next) {
req.client = client;
next();
})
.use('/', require('./rutas/index'))
.get('*', function (req, res) {
res.status(404).sendFile(__dirname + '/views/404.html');
})
.use('*', function (req, res) {
res.status(405).sendFile(__dirname + '/views/405.html');
});
client.on('ready', async () => {
console.log('Bot ready!');
});
io.on('connection', (socket) => {
socket.on('add message', async function (key, pre_content) {
if (!someKeys.has(key)) return;
try {
const channel = await client.channels.fetch(process.env.ID_CHANNEL);
const user = await client.users.fetch(someKeys.get(key).userID);
const member = await channel.guild.members
.fetch(someKeys.get(key).userID)
.catch(() => {});
if (someKeys.get(key).send_on && channel.rateLimitPerUser) {
const time = someKeys.get(key).send_on;
const dif = Date.now() - time.getTime();
const seconds = Math.floor(Math.abs(dif / 1000));
if (channel.rateLimitPerUser > seconds)
return socket.emit(
'cooldown',
seconds,
channel.rateLimitPerUser
);
}
someKeys.get(key).send_on = new Date();
// Permitir símbolos :o :) etc
let content = pre_content;
const emojiPattern = /:([\w+-]+):/g;
content = content.replace(emojiPattern, (match) => {
return isValidEmoji(match) ? match : match.replace(':', ':');
});
if (validInvs(content)) {
content = `**${user.username}** enlace inválido.`;
}
const webhookMessage = await webhook.send({
content: content,
username: (member ? member.displayName : user.username).replace(
/clyde/gi,
'Clide'
),
avatarURL: user.displayAvatarURL({ format: 'png' }),
wait: true
});
const messageData = {
content: content,
author: member ? member.displayName : user.username,
avatarURL: user.displayAvatarURL({ format: 'png', dynamic: true, size: 1024 }),
id: user.id,
messageID: webhookMessage.id,
date: parseDate(new Date()),
colorName: member ? member.displayHexColor : '#FFFFFF',
attachmentURL: null
};
// Emitir nuevo mensaje al socket
socket.emit('new message', messageData);
// Emitirlo para todos
socket.broadcast.emit('new message', messageData);
socket.emit('cooldown', null, channel.rateLimitPerUser);
} catch (error) {
console.error('Error sending message:', error);
socket.emit('error', 'Failed to send message');
}
});
socket.on('join', async function (key) {
if (!someKeys.has(key)) return;
try {
const channel = await client.channels.fetch(process.env.ID_CHANNEL_LOG);
const user = await client.users.fetch(someKeys.get(key).userID);
await webhook.send({
content: `**Inicio:** ${user.username} (${user.id})`,
username: 'MyChat v2',
avatarURL: 'https://i.imgur.com/TVaNWMn.png',
});
socket.userId = someKeys.get(key).userID;
socket.key = key;
await channel.send({
embeds: [{
title: `Inicio: ${user.username} (${user.id})`,
color: 0x8db600,
}]
});
} catch (error) {
console.error('Error processing join:', error);
}
});
socket.on('disconnect', async function () {
try {
const user = await client.users.fetch(socket.userId).catch(() => {});
if (!user) return;
const channel = await client.channels.fetch(process.env.ID_CHANNEL_LOG);
await webhook.send({
content: `**Desconectado:** ${user.username} (${user.id})`,
username: 'MyChat v2',
avatarURL: 'https://i.imgur.com/TVaNWMn.png',
});
await channel.send({
embeds: [{
title: `Desconectado: ${user.username} (${user.id})`,
color: 0xe52b50,
}]
});
} catch (error) {
console.error('Error processing disconnect:', error);
}
});
});
client.on('messageCreate', async (message) => {
if (!message.content && !message.attachments.size) return;
if (message.channel.id !== process.env.ID_CHANNEL) return;
const dataMSG = processFrontEndMessage(client, message);
io.emit('new message', dataMSG);
});
client.on('typingStart', async (typing) => {
if (typing.channel.id !== process.env.ID_CHANNEL) return;
if (typing.user.bot) return;
io.emit('typingStart', {
user: {
id: typing.user.id,
username: typing.user.username,
},
});
});
const port = process.env.PORT || 3000;
server.listen(port, function () {
client.login(process.env.DISCORD_TOKEN).then(() => {
console.log('Bot logged in successfully');
}).catch(error => {
console.error('Failed to login:', error);
});
console.log(`Server running on port ${port}`);
});
// Error handling
process.on('unhandledRejection', (error) => {
console.error('Unhandled promise rejection:', error);
});
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
});