-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
102 lines (76 loc) · 3.39 KB
/
server.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
const express = require('express');
const { join } = require('path');
const { createServer } = require('http');
const { renderFile } = require('ejs');
const app = express();
const server = createServer(app);
const io = (require('socket.io'))(server);
app.use(express.static(join(__dirname, 'public')));
app.set('views', join(__dirname, 'public'));
app.engine('html', renderFile);
app.set('view engine', 'html');
app.use('/', (_, res) => res.render('index.html'));
app.use('/chat', (_, res) => res.render('index.html'));
const lastDate = {};
const rateLimiter = {};
const linkRegExp = new RegExp('(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})')
const mentionRegExp = new RegExp('\@([a-zA-Z0-9]+)', 'gm')
const breakString = (s, a) => s.length > a ? s.slice(0, a) : s;
const getMentions = (s) => s.match(mentionRegExp) || [];
let connections = 0;
function escapeHTML(string) {
return string
.replace(/&/g, "&")
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/"/g, """)
}
io.on('connection', (socket) => {
connections++;
socket.broadcast.emit("userJoin", socket.id);
socket.on('disconnect', () => {
connections--;
delete lastDate[socket.id];
delete rateLimiter[socket.id];
socket.broadcast.emit("userLeft", socket.id);
socket.broadcast.emit("usersCountUpdated", connections);
});
socket.on('sendMessage', (data) => {
// Remove additional information sended by client.
const avatar = data.author.avatar_url
data = {
author: {
name: escapeHTML(breakString(data.author.name, 10)),
avatar_url: avatar ? encodeURI(avatar) : DEFAULT_USER_AVATAR,
},
content: escapeHTML(breakString(data.content, 250)),
};
data.mentions = getMentions(data.content)
data.content = data.content.replace(linkRegExp, (item) => {
item = encodeURI(item)
return `<a href='${item}' class='link' target='_blank'>${item}</a>`
});
let lastMessageSendedDate = lastDate[socket.id];
if (!lastMessageSendedDate) lastMessageSendedDate = Date.now();
let rateLimit = rateLimiter[socket.id]
if (!rateLimit) {
rateLimiter[socket.id] = [];
rateLimit = rateLimiter[socket.id];
}
if (rateLimit.length >= 5 && rateLimit.reduce((a, b) => a + b, 0) <= 800) {
if (lastMessageSendedDate + 30 * 1000 <= Date.now()) rateLimiter[socket.id] = [];
return socket.emit('messageNotSended', data);
}
if (lastMessageSendedDate <= Date.now()) {
rateLimiter[socket.id].push(Date.now() - lastMessageSendedDate);
lastDate[socket.id] = Date.now() + 1000;
socket.broadcast.emit('receivedMessage', data);
return socket.emit('messageSended', data);
}
return socket.emit('messageNotSended', data);
});
socket.emit("usersCountUpdated", connections);
socket.broadcast.emit("usersCountUpdated", connections);
});
server.listen(process.env.PORT || 3000, () => console.log('Running!'));
var DEFAULT_USER_AVATAR = 'https://i.imgur.com/HXv93eV.jpg'; // quem mudar daqui, vai cair o pau quando for mijar