2020-04-07 09:55:16 +02:00
|
|
|
const Room = require("./Room.js");
|
2020-04-07 23:02:35 +02:00
|
|
|
const Quota = require ("./Quota.js");
|
|
|
|
const quotas = require('../Quotas');
|
2020-04-08 00:19:04 +02:00
|
|
|
const RateLimit = require('./Ratelimit.js').RateLimit;
|
|
|
|
const RateLimitChain = require('./Ratelimit.js').RateLimitChain;
|
2020-04-07 09:55:16 +02:00
|
|
|
require('node-json-color-stringify');
|
|
|
|
class Client extends EventEmitter {
|
|
|
|
constructor(ws, req, server) {
|
|
|
|
super();
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.user;
|
|
|
|
this.connectionid = server.connectionid;
|
|
|
|
this.server = server;
|
|
|
|
this.participantId;
|
|
|
|
this.channel;
|
2020-04-07 23:02:35 +02:00
|
|
|
this.staticQuotas = {
|
|
|
|
room: new RateLimit(quotas.room.time)
|
|
|
|
};
|
|
|
|
this.quotas = {};
|
2020-04-07 09:55:16 +02:00
|
|
|
this.ws = ws;
|
|
|
|
this.req = req;
|
|
|
|
this.ip = (req.connection.remoteAddress).replace("::ffff:", "");
|
|
|
|
this.destroied = false;
|
|
|
|
this.bindEventListeners();
|
|
|
|
require('./Message.js')(this);
|
|
|
|
}
|
|
|
|
isConnected() {
|
|
|
|
return this.ws && this.ws.readyState === WebSocket.OPEN;
|
|
|
|
}
|
|
|
|
isConnecting() {
|
|
|
|
return this.ws && this.ws.readyState === WebSocket.CONNECTING;
|
|
|
|
}
|
|
|
|
setChannel(_id, settings) {
|
|
|
|
if (this.channel && this.channel._id == _id) return;
|
|
|
|
if (this.server.rooms.get(_id)) {
|
2021-03-01 05:06:39 +01:00
|
|
|
let room = this.server.rooms.get(_id, settings);
|
2020-04-07 09:55:16 +02:00
|
|
|
let userbanned = room.bans.get(this.user._id);
|
|
|
|
if (userbanned && (Date.now() - userbanned.bannedtime >= userbanned.msbanned)) {
|
|
|
|
room.bans.delete(userbanned.user._id);
|
|
|
|
userbanned = undefined;
|
|
|
|
}
|
|
|
|
if (userbanned) {
|
|
|
|
room.Notification(this.user._id,
|
|
|
|
"Notice",
|
|
|
|
`Currently banned from \"${_id}\" for ${Math.ceil(Math.floor((userbanned.msbanned - (Date.now() - userbanned.bannedtime)) / 1000) / 60)} minutes.`,
|
|
|
|
7000,
|
|
|
|
"",
|
|
|
|
"#room",
|
|
|
|
"short"
|
|
|
|
);
|
2021-03-01 05:06:39 +01:00
|
|
|
this.setChannel("test/awkward", settings);
|
2020-04-07 09:55:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let channel = this.channel;
|
|
|
|
if (channel) this.channel.emit("bye", this);
|
|
|
|
if (channel) this.channel.updateCh();
|
|
|
|
this.channel = this.server.rooms.get(_id);
|
|
|
|
this.channel.join(this);
|
|
|
|
} else {
|
|
|
|
let room = new Room(this.server, _id, settings);
|
|
|
|
this.server.rooms.set(_id, room);
|
|
|
|
if (this.channel) this.channel.emit("bye", this);
|
|
|
|
this.channel = this.server.rooms.get(_id);
|
|
|
|
this.channel.join(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sendArray(arr) {
|
|
|
|
if (this.isConnected()) {
|
|
|
|
//console.log(`SEND: `, JSON.colorStringify(arr));
|
|
|
|
this.ws.send(JSON.stringify(arr));
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:02:35 +02:00
|
|
|
initParticipantQuotas() {
|
|
|
|
this.quotas = {
|
|
|
|
//"chat": new Quota(Quota.PARAMS_A_NORMAL),
|
|
|
|
chat: {
|
|
|
|
lobby: new RateLimitChain(quotas.chat.lobby.amount, quotas.chat.lobby.time),
|
|
|
|
normal: new RateLimitChain(quotas.chat.normal.amount, quotas.chat.normal.time),
|
|
|
|
insane: new RateLimitChain(quotas.chat.insane.amount, quotas.chat.insane.time)
|
|
|
|
},
|
|
|
|
cursor: new RateLimit(quotas.cursor.time),
|
|
|
|
chown: new RateLimitChain(quotas.chown.amount, quotas.chown.time),
|
|
|
|
userset: new RateLimitChain(quotas.userset.amount, quotas.userset.time),
|
|
|
|
kickban: new RateLimitChain(quotas.kickban.amount, quotas.kickban.time),
|
|
|
|
note: new Quota(Quota.PARAMS_LOBBY),
|
|
|
|
chset: new Quota(Quota.PARAMS_USED_A_LOT),
|
|
|
|
"+ls": new Quota(Quota.PARAMS_USED_A_LOT),
|
|
|
|
"-ls": new Quota(Quota.PARAMS_USED_A_LOT)
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 09:55:16 +02:00
|
|
|
destroy() {
|
|
|
|
this.ws.close();
|
|
|
|
if (this.channel) {
|
|
|
|
this.channel.emit("bye", this)
|
|
|
|
}
|
|
|
|
this.user;
|
|
|
|
this.participantId;
|
|
|
|
this.channel;
|
|
|
|
this.server.roomlisteners.delete(this.connectionid);
|
|
|
|
this.connectionid;
|
|
|
|
this.server.connections.delete(this.connectionid);
|
|
|
|
this.destroied = true;
|
2021-03-01 05:17:50 +01:00
|
|
|
console.log(`Removed Connection ${this.connectionid}.`);
|
2020-04-07 09:55:16 +02:00
|
|
|
}
|
|
|
|
bindEventListeners() {
|
|
|
|
this.ws.on("message", (evt, admin) => {
|
|
|
|
try {
|
2021-04-02 20:02:04 +02:00
|
|
|
if (typeof(evt) !== 'string') evt = evt.toJSON();
|
2020-04-07 09:55:16 +02:00
|
|
|
let transmission = JSON.parse(evt);
|
|
|
|
for (let msg of transmission) {
|
2021-04-12 18:37:19 +02:00
|
|
|
if (typeof(msg) !== 'object' || msg == null || msg == NaN) return;
|
2020-04-07 09:55:16 +02:00
|
|
|
if (!msg.hasOwnProperty("m")) return;
|
|
|
|
if (!this.server.legit_m.includes(msg.m)) return;
|
|
|
|
this.emit(msg.m, msg, !!admin);
|
|
|
|
//console.log(`RECIEVE: `, JSON.colorStringify(msg));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
2021-04-02 20:02:04 +02:00
|
|
|
// this.destroy();
|
2020-04-07 09:55:16 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.ws.on("close", () => {
|
|
|
|
if (!this.destroied)
|
|
|
|
this.destroy();
|
|
|
|
});
|
|
|
|
this.ws.addEventListener("error", (err) => {
|
|
|
|
console.error(err);
|
|
|
|
if (!this.destroied)
|
|
|
|
this.destroy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 23:09:00 +02:00
|
|
|
module.exports = Client;
|