DenisSlider2000/index.js

114 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-04-09 00:32:32 +02:00
const fs = require("fs");
const https = require("https");
const express = require("express");
const socketio = require("socket.io");
2022-04-14 15:57:54 +02:00
const basicAuth = require("express-basic-auth");
2022-04-07 19:09:47 +02:00
const app = express();
2022-04-07 19:37:26 +02:00
const https_options = {
2022-04-09 00:32:32 +02:00
key: fs.readFileSync("./privkey.pem"),
cert: fs.readFileSync("./fullchain.pem"),
2022-04-07 19:37:26 +02:00
requestCert: false,
rejectUnauthorized: false
}
2022-04-11 12:34:40 +02:00
const socketHTTPSServer = https.createServer(https_options, app);
const webHTTPSServer = https.createServer(https_options, app);
2022-04-07 19:37:26 +02:00
2022-04-11 12:34:40 +02:00
const io = socketio(socketHTTPSServer, {
2022-04-07 19:37:26 +02:00
cors: {
origin: "https://danby.aniere.fr",
},
secure: true
});
2022-04-07 19:09:47 +02:00
2022-04-12 13:38:33 +02:00
function getCurrentTime() {
2022-04-26 21:52:01 +02:00
2022-04-12 13:38:33 +02:00
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
var d = new Date();
2022-04-13 21:52:53 +02:00
var day = addZero(d.getUTCDate());
var month = addZero(d.getUTCMonth() + 1);
var year = d.getFullYear();
2022-04-12 13:38:33 +02:00
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
2022-04-13 21:52:53 +02:00
return "[" + day + "-"+ month + "-" + year + " " + h + ":" + m + ":" + s + "]";
2022-04-12 13:38:33 +02:00
2022-04-13 21:52:53 +02:00
}
function log(message) {
console.log(getCurrentTime() + " " + message);
}
2022-04-12 13:38:33 +02:00
2022-04-12 10:43:11 +02:00
usersConnected = 0;
lastUsername = 0;
2022-04-13 21:52:53 +02:00
messagesHistory = []
2022-04-07 19:09:47 +02:00
io.on("connection", (client) => {
usersConnected++;
2022-04-12 10:43:11 +02:00
io.sockets.emit("update-count", {value: usersConnected});
2022-04-12 13:38:33 +02:00
2022-04-07 19:09:47 +02:00
client.on("disconnect", () => {
usersConnected--;
io.sockets.emit("update-count", {value: usersConnected});
2022-04-13 21:52:53 +02:00
log(`${client.usernameDenis} has disconnected`);
2022-04-07 19:09:47 +02:00
});
2022-04-12 10:43:11 +02:00
client.on("message", (data) => {
2022-04-12 13:29:46 +02:00
if(data.message.toLowerCase().includes("denis")) {
io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message});
2022-04-13 21:52:53 +02:00
log(`${client.usernameDenis} : ${data.message}`);
messagesHistory.push([client.usernameDenis, data.message]);
if(messagesHistory.length > 20) {
messagesHistory.shift();
}
2022-04-12 13:29:46 +02:00
} else {
client.emit("warning");
2022-04-13 21:52:53 +02:00
log(`${client.usernameDenis} : ${data.message} (INVALID)`);
2022-04-12 13:29:46 +02:00
}
2022-04-12 10:43:11 +02:00
});
lastUsername++;
client.usernameDenis = "DenisFan" + lastUsername;
client.emit("username", {username: "DenisFan" + lastUsername});
2022-04-13 21:52:53 +02:00
log(`${client.usernameDenis} is now connected`);
client.emit("previous-messages", {messages: messagesHistory});
2022-04-12 10:43:11 +02:00
2022-04-07 19:09:47 +02:00
});
2022-04-11 12:34:40 +02:00
app.use(express.static("src"));
2022-04-14 15:57:54 +02:00
app.get("/clear", basicAuth({users:{"quentin": "BlanquerCaca89"}, challenge: true}), (req, res) => {
messagesHistory = [];
res.send("message history was cleared");
log("Admin cleared messages history");
io.sockets.emit("refresh");
});
2022-04-11 12:34:40 +02:00
app.listen(80, () => {
2022-04-13 21:52:53 +02:00
log("Web server started on port 80")
2022-04-11 12:34:40 +02:00
});
webHTTPSServer.listen(443, () => {
2022-04-13 21:52:53 +02:00
log("Web HTTPS server started on port 443");
2022-04-11 12:34:40 +02:00
});
socketHTTPSServer.listen(3000, () => {
2022-04-13 21:52:53 +02:00
log("Socket HTTPS server started on port 3000");
2022-04-07 19:09:47 +02:00
});