DenisSlider2000/index.js

138 lines
3.8 KiB
JavaScript
Raw Normal View History

2022-04-09 00:32:32 +02:00
const fs = require("fs");
const https = require("https");
2022-05-18 10:55:52 +02:00
const axios = require("axios");
2022-04-26 22:27:56 +02:00
const dotenv = require('dotenv').config();
2022-04-09 00:32:32 +02:00
const express = require("express");
const socketio = require("socket.io");
2022-04-26 22:16:59 +02:00
const sanitizeHtml = require("sanitize-html");
2022-04-14 15:57:54 +02:00
const basicAuth = require("express-basic-auth");
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-05-18 10:55:52 +02:00
function getFact() {
axios.get("https://chuckn.neant.be/api/rand")
.then(response => {
return response.data.joke.replace("Chuck Norris", "Denis");
})
}
2022-04-13 21:52:53 +02:00
2022-04-26 22:07:06 +02:00
const app = express(); //The base http server, use by socketio and https serveur
const https_options = {
key: fs.readFileSync("./privkey.pem"),
cert: fs.readFileSync("./fullchain.pem"),
requestCert: false,
rejectUnauthorized: false
}
const socketHTTPSServer = https.createServer(https_options, app);
const webHTTPSServer = https.createServer(https_options, app);
const io = socketio(socketHTTPSServer, {
cors: {
origin: "https://danby.aniere.fr",
},
secure: true
});
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) => {
2022-04-26 22:07:06 +02:00
2022-04-07 19:09:47 +02:00
client.on("disconnect", () => {
usersConnected--;
io.sockets.emit("update-count", {value: usersConnected});
2022-04-26 22:07:06 +02:00
log(`${client.denisFanNumber} 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")) {
2022-04-13 21:52:53 +02:00
2022-04-26 22:16:59 +02:00
message = sanitizeHtml(data.message, {allowedTags: [], allowedAttributes: {}});
2022-04-26 22:07:06 +02:00
2022-04-26 22:16:59 +02:00
io.sockets.emit("new-message", {author: client.denisFanNumber, message: message});
log(`${client.denisFanNumber} : ${message}`);
messagesHistory.push([client.denisFanNumber, message]);
2022-04-13 21:52:53 +02:00
if(messagesHistory.length > 20) {
messagesHistory.shift();
}
2022-04-12 13:29:46 +02:00
2022-05-10 14:46:41 +02:00
} else if(data.message === "/prout" || data.message === "/fatma" || data.message === "/fart" || data.message === "/fartma") {
2022-04-26 22:39:09 +02:00
io.sockets.emit("fart");
log(`${client.denisFanNumber} : have farted`);
2022-05-18 10:55:52 +02:00
} else if(data.message === "/fact") {
io.sockets.emit("new-message", {author: "DenisFact", message: getFact()});
2022-04-12 13:29:46 +02:00
} else {
client.emit("warning");
2022-04-26 22:07:06 +02:00
log(`${client.denisFanNumber} : ${data.message} (INVALID)`);
2022-04-12 13:29:46 +02:00
}
2022-04-12 10:43:11 +02:00
});
lastUsername++;
2022-04-26 22:07:06 +02:00
usersConnected++;
io.sockets.emit("update-count", {value: usersConnected});
2022-04-13 21:52:53 +02:00
2022-04-26 22:07:06 +02:00
client.denisFanNumber = "DenisFan" + lastUsername;
client.emit("username", {username: "DenisFan" + lastUsername});
2022-04-13 21:52:53 +02:00
client.emit("previous-messages", {messages: messagesHistory});
2022-04-26 22:07:06 +02:00
log(`${client.denisFanNumber} is now connected`);
2022-04-07 19:09:47 +02:00
});
2022-04-11 12:34:40 +02:00
app.use(express.static("src"));
2022-05-11 14:08:18 +02:00
app.get("/clear", basicAuth({users:{"quentin": process.env.ADMIN_PASSWORD, "yvan": process.env.YVAN_PASSWORD}, challenge: true}), (req, res) => {
2022-04-14 15:57:54 +02:00
messagesHistory = [];
res.send("message history was cleared");
io.sockets.emit("refresh");
2022-05-11 14:08:18 +02:00
ADMIN_PASSWORD
2022-04-26 22:07:06 +02:00
log("Admin cleared messages history");
2022-04-14 15:57:54 +02:00
});
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
});