2020-04-07 09:55:16 +02:00
|
|
|
const Client = require("./Client.js");
|
|
|
|
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");
|
2021-04-02 20:02:04 +02:00
|
|
|
const fs = require('fs');
|
2021-04-12 23:29:02 +02:00
|
|
|
const RoomSettings = require('./RoomSettings');
|
2021-03-01 05:06:39 +01:00
|
|
|
|
2020-04-07 09:55:16 +02:00
|
|
|
class Server extends EventEmitter {
|
|
|
|
constructor(config) {
|
|
|
|
super();
|
|
|
|
EventEmitter.call(this);
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2021-04-12 18:41:44 +02:00
|
|
|
if (config.ssl == true) {
|
|
|
|
this.https_server = https.createServer({
|
|
|
|
key: fs.readFileSync('ssl/privkey.pem', 'utf8'),
|
|
|
|
cert: fs.readFileSync('ssl/cert.pem'),
|
|
|
|
ca: fs.readFileSync('ssl/chain.pem')
|
|
|
|
});
|
|
|
|
|
|
|
|
this.wss = new WebSocket.Server({
|
|
|
|
server: this.https_server,
|
|
|
|
backlog: 100,
|
|
|
|
verifyClient: (info) => {
|
|
|
|
if (banned.includes((info.req.connection.remoteAddress).replace("::ffff:", ""))) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.https_server.listen(config.port);
|
|
|
|
} else {
|
|
|
|
this.wss = new WebSocket.Server({
|
|
|
|
port: 8443,
|
|
|
|
backlog: 100,
|
|
|
|
verifyClient: (info) => {
|
|
|
|
if (banned.includes((info.req.connection.remoteAddress).replace("::ffff:", ""))) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-04-12 23:29:02 +02:00
|
|
|
|
|
|
|
this.defaultUsername = "Anonymous";
|
|
|
|
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-04-02 20:02:04 +02:00
|
|
|
console.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();
|
|
|
|
this.rooms = new Map();
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2020-04-07 09:55:16 +02:00
|
|
|
this.wss.on('connection', (ws, req) => {
|
|
|
|
this.connections.set(++this.connectionid, new Client(ws, req, this));
|
|
|
|
});
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2021-04-02 20:02:04 +02:00
|
|
|
this.legit_m = ["a", "bye", "hi", "ch", "+ls", "-ls", "m", "n", "devices", "t", "chset", "userset", "chown", "kickban", "admin message", "color", "eval", "notification"]
|
|
|
|
this.welcome_motd = config.motd || "You agree to read this message.";
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2020-04-07 09:55:16 +02:00
|
|
|
this._id_Private_Key = config._id_PrivateKey || "boppity";
|
2021-04-12 23:29:02 +02:00
|
|
|
|
2020-04-07 09:55:16 +02:00
|
|
|
this.adminpass = config.adminpass || "Bop It";
|
2021-04-12 23:29:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 09:55:16 +02:00
|
|
|
updateRoom(data) {
|
|
|
|
if (!data.ch.settings.visible) return;
|
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())) {
|
|
|
|
cl.sendArray([{
|
|
|
|
"m": "ls",
|
|
|
|
"c": false,
|
|
|
|
"u": [data.ch]
|
2021-04-12 23:29:02 +02:00
|
|
|
}]);
|
2020-04-07 09:55:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-02 20:02:04 +02:00
|
|
|
|
|
|
|
ev(str) {
|
2021-04-12 18:37:19 +02:00
|
|
|
let out = "";
|
2021-04-02 20:02:04 +02:00
|
|
|
try {
|
|
|
|
out = eval(str);
|
|
|
|
} catch(err) {
|
|
|
|
out = err;
|
|
|
|
}
|
|
|
|
console.log(out);
|
|
|
|
}
|
2020-04-07 09:55:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 20:02:04 +02:00
|
|
|
module.exports = Server;
|