allow only one connection per user, TODO: give a firendly message to users

This commit is contained in:
hippoz 2020-11-21 14:44:54 +02:00
parent 2c41359b9d
commit 5b34130ef4

View file

@ -41,6 +41,14 @@ GatewayServer.prototype.eventSetup = function() {
if (!token) return this.authDisconnect(socket, callback); if (!token) return this.authDisconnect(socket, callback);
if (!(typeof token === 'string')) return this.authDisconnect(socket, callback); if (!(typeof token === 'string')) return this.authDisconnect(socket, callback);
const allSockets = this._gateway.sockets;
for (let [_, e] of allSockets) {
if (e.user && e.user.token === token) {
console.log(`[E] [gateway] [handshake] User ${e.user.username} tried to connect more than once, rejecting connection...`);
return this.authDisconnect(socket, callback);
}
}
jwt.verify(token, secret.jwtPrivateKey, {}, async (err, data) => { jwt.verify(token, secret.jwtPrivateKey, {}, async (err, data) => {
if (err) return this.authDisconnect(socket, callback); if (err) return this.authDisconnect(socket, callback);
if (!data) return this.authDisconnect(socket, callback); if (!data) return this.authDisconnect(socket, callback);
@ -59,7 +67,8 @@ GatewayServer.prototype.eventSetup = function() {
socket.user = { socket.user = {
username: data.username, username: data.username,
_id: user._id _id: user._id,
token // Maybe not secure
}; };
console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`); console.log(`[*] [gateway] [handshake] User ${data.username} has successfully authenticated`);
return callback(); return callback();