mpp-server-dev2/oldsrc/Server.js

231 lines
6.4 KiB
JavaScript
Raw Normal View History

2020-04-07 09:55:16 +02:00
const Client = require("./Client.js");
2023-03-31 16:43:24 +02:00
const banned = require("../banned.json");
2021-04-02 20:02:04 +02:00
const https = require("https");
2021-04-12 18:41:44 +02:00
const http = require("http");
2023-03-31 16:43:24 +02:00
const fs = require("fs");
const RoomSettings = require("./RoomSettings");
2021-05-09 22:34:22 +02:00
const Logger = require("./Logger.js");
2023-03-31 16:43:24 +02:00
const Notification = require("./Notification");
2023-04-01 07:05:12 +02:00
const Database = require("./Database.js");
2022-08-16 15:42:09 +02:00
class Server {
static on = EventEmitter.prototype.on;
static off = EventEmitter.prototype.off;
static emit = EventEmitter.prototype.emit;
static once = EventEmitter.prototype.once;
2023-03-31 16:43:24 +02:00
static startTime = Date.now();
2023-01-15 01:48:04 +01:00
2022-08-16 15:42:09 +02:00
static start(config) {
// super();
// EventEmitter.call(this);
2021-05-09 22:34:22 +02:00
this.logger = new Logger("Server");
2023-03-31 16:43:24 +02:00
2022-01-26 01:44:26 +01:00
if (config.ssl == "true") {
2021-04-12 18:41:44 +02:00
this.https_server = https.createServer({
2023-03-31 16:43:24 +02:00
key: fs.readFileSync("ssl/privkey.pem", "utf8"),
cert: fs.readFileSync("ssl/cert.pem"),
ca: fs.readFileSync("ssl/chain.pem")
2021-04-12 18:41:44 +02:00
});
this.wss = new WebSocket.Server({
server: this.https_server,
backlog: 100,
2023-03-31 16:43:24 +02:00
verifyClient: info => {
const ip = info.req.connection.remoteAddress.replace(
"::ffff:",
""
);
if (
!ip.match(
/^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.){3}(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)$/gi
)
)
return false;
2022-05-29 04:41:58 +02:00
if (banned.includes(ip)) return false;
2021-04-12 18:41:44 +02:00
return true;
}
});
2023-03-31 16:43:24 +02:00
this.https_server.listen(config.port, "0.0.0.0");
2021-04-12 18:41:44 +02:00
} else {
this.wss = new WebSocket.Server({
2021-04-13 20:27:01 +02:00
port: config.port,
2021-04-12 18:41:44 +02:00
backlog: 100,
2023-03-31 16:43:24 +02:00
verifyClient: info => {
const ip = info.req.connection.remoteAddress.replace(
"::ffff:",
""
);
2022-08-16 15:42:09 +02:00
if (banned.includes(ip)) return false;
2023-04-01 07:46:16 +02:00
if (Database.isIPBanned(ip)) return false;
2021-04-12 18:41:44 +02:00
return true;
}
});
}
2021-04-12 23:29:02 +02:00
2021-05-09 22:34:22 +02:00
this.defaultUsername = config.defaultUsername;
2021-04-12 23:29:02 +02:00
this.defaultRoomSettings = new RoomSettings(config.defaultRoomSettings);
this.lobbySettings = new RoomSettings(config.defaultRoomSettings);
this.lobbySettings.lobby = true;
this.lobbySettings.color = config.defaultLobbyColor || "#9900ff";
this.lobbySettings.color2 = config.defaultLobbyColor2 || "#9900ff";
2021-05-09 22:34:22 +02:00
this.logger.log(`Server started on port ${config.port}`);
2020-04-07 09:55:16 +02:00
this.connectionid = 0;
this.connections = new Map();
this.roomlisteners = new Map();
2023-03-31 16:43:24 +02:00
this.channels = new Map();
2023-04-20 10:22:28 +02:00
this.cycle = require("./cycle");
2021-04-12 23:29:02 +02:00
2021-05-09 10:52:41 +02:00
this.specialIntervals = {};
2023-03-31 16:43:24 +02:00
this.wss.on("connection", (ws, req) => {
2023-04-01 07:05:12 +02:00
// console.log("socket connected");
2023-03-31 16:43:24 +02:00
this.connections.set(
++this.connectionid,
new Client(ws, req, this)
);
2020-04-07 09:55:16 +02:00
});
2021-04-12 23:29:02 +02:00
2022-05-24 20:22:19 +02:00
this.legit_m = [
"a",
"bye",
"hi",
"ch",
"+ls",
"-ls",
"m",
"n",
"devices",
"t",
"chset",
"userset",
"chown",
"kickban",
2023-01-15 01:48:04 +01:00
"unban",
2022-05-24 20:22:19 +02:00
"admin message",
"color",
"eval",
"notification",
"user_flag",
"room_flag",
"clear_chat",
"sudo",
"subscribe to admin stream",
"unsubscribe from admin stream",
2022-06-17 08:36:49 +02:00
"data",
2022-06-18 06:44:22 +02:00
"channel message",
2022-07-09 06:44:49 +02:00
"channel_flag",
2022-07-09 11:06:51 +02:00
"name",
2023-04-01 07:05:12 +02:00
"restart",
"ipban",
"ipunban"
2022-05-24 20:22:19 +02:00
];
2022-08-16 15:42:09 +02:00
// this.welcome_motd = config.motd || "You agree to read this message.";
2021-04-12 23:29:02 +02:00
2022-05-24 20:22:19 +02:00
this._id_Private_Key = config._id_PrivateKey || "amogus";
2021-04-12 23:29:02 +02:00
2022-05-24 20:22:19 +02:00
this.adminpass = config.adminpass || "123123sucks";
2021-04-12 23:29:02 +02:00
}
2023-03-31 16:43:24 +02:00
static updateChannelList(channelDataArray) {
const listData = [];
for (let chm of Object.values(channelDataArray)) {
if (!chm.ch.settings.visible) return;
listData.push(chm.ch);
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
for (let cl of Array.from(this.roomlisteners.values())) {
2021-05-09 10:52:41 +02:00
if (cl.destroied) {
cl = undefined;
return;
}
2023-01-15 01:48:04 +01:00
2023-03-31 16:43:24 +02:00
for (const ch of Object.values(listData)) {
const c = this.channels.get(ch._id);
if (!c) continue;
ch.banned = typeof c.bans.get(cl.user._id) !== "undefined";
}
2023-01-15 01:48:04 +01:00
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "ls",
c: false,
u: listData
}
]);
2020-04-07 09:55:16 +02:00
}
}
2021-04-02 20:02:04 +02:00
2022-08-16 15:42:09 +02:00
static ev(str) {
2021-04-12 18:37:19 +02:00
let out = "";
2021-04-02 20:02:04 +02:00
try {
out = eval(str);
2023-03-31 16:43:24 +02:00
} catch (err) {
2021-04-02 20:02:04 +02:00
out = err;
}
2023-04-01 07:05:12 +02:00
// console.log(out);
return `(${typeof out}) ${out}`;
2021-04-02 20:02:04 +02:00
}
2022-05-24 21:40:09 +02:00
2022-08-16 15:42:09 +02:00
static getClient(id) {
2022-05-24 21:40:09 +02:00
return this.connections.get(id);
}
2022-08-16 15:42:09 +02:00
static getClientByParticipantID(id) {
2022-06-17 08:36:49 +02:00
for (let cl of Array.from(this.connections.values())) {
if (cl.participantID == id) return cl;
}
return null;
}
2022-08-16 15:42:09 +02:00
static getAllClientsByUserID(_id) {
2022-05-24 21:40:09 +02:00
let out = [];
for (let cl of Array.from(this.connections.values())) {
if (cl.user._id == _id) out.push(cl);
}
return out;
}
2022-07-09 11:06:51 +02:00
2023-03-31 16:43:24 +02:00
static restart(
notif = {
m: "notification",
id: "server-restart",
title: "Notice",
text: "The server will restart in a few moments.",
target: "#piano",
duration: 20000,
2023-04-01 07:05:12 +02:00
class: "classic",
targetChannel: "all"
2023-03-31 16:43:24 +02:00
}
) {
2023-04-01 07:05:12 +02:00
let n = new Notification(this, notif);
n.send();
2022-07-09 11:06:51 +02:00
setTimeout(() => {
process.exit();
}, n.duration || 20000);
}
2023-04-01 07:05:12 +02:00
static banIP(ip) {
Database.addIPBan(ip);
for (const cl of this.connections.values()) {
if (cl.ip == ip) {
cl.destroy();
}
}
}
static unbanIP(ip) {
Database.removeIPBan(ip);
}
2020-04-07 09:55:16 +02:00
}
2021-04-02 20:02:04 +02:00
module.exports = Server;