2023-04-21 13:18:00 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2023 artegoser (Artemy Egorov)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-04-02 10:53:32 +03:00
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const isValidNote = require("./note_validator");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2023-04-03 14:07:47 +03:00
|
|
|
const { Server } = require("socket.io");
|
2023-04-19 18:06:50 +03:00
|
|
|
const rateLimit = require("express-rate-limit");
|
2023-04-24 09:56:50 +03:00
|
|
|
const { NotesCore } = require("./core");
|
|
|
|
|
|
|
|
let core = new NotesCore();
|
2023-04-02 10:53:32 +03:00
|
|
|
|
|
|
|
require("dotenv").config();
|
|
|
|
|
2023-04-03 14:07:47 +03:00
|
|
|
const app = express(),
|
|
|
|
server = require("http").createServer(app),
|
|
|
|
io = new Server().listen(server);
|
2023-04-02 10:53:32 +03:00
|
|
|
|
2023-04-19 18:06:50 +03:00
|
|
|
const limiter = rateLimit({
|
|
|
|
windowMs: 24 * 60 * 60 * 1000, // one day limit
|
2023-04-24 09:56:50 +03:00
|
|
|
max: 50,
|
2023-04-19 18:06:50 +03:00
|
|
|
standardHeaders: true,
|
|
|
|
legacyHeaders: false,
|
|
|
|
});
|
|
|
|
|
2023-04-02 11:14:52 +03:00
|
|
|
if (!fs.existsSync("./notes")) {
|
|
|
|
fs.mkdirSync("./notes");
|
|
|
|
}
|
|
|
|
|
2023-04-04 20:10:41 +03:00
|
|
|
io.on("connection", (socket) => {
|
|
|
|
socket.on("nameChanged", ({ name, room }) => {
|
|
|
|
socket.to(room).emit("nameChanged", {
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("textChanged", ({ text, room }) => {
|
|
|
|
socket.to(room).emit("textChanged", {
|
|
|
|
text,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("joinRoom", (room) => {
|
|
|
|
let rooms = Array.from(io.sockets.adapter.sids.get(socket.id));
|
|
|
|
|
|
|
|
for (let room of rooms) {
|
|
|
|
if (socket.id != room) {
|
|
|
|
socket.leave(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.join(room);
|
2023-04-04 20:33:42 +03:00
|
|
|
socket.to(room).emit("roomJoined");
|
2023-04-04 20:10:41 +03:00
|
|
|
});
|
2023-04-05 14:23:32 +03:00
|
|
|
|
|
|
|
socket.on("leaveRoom", () => {
|
|
|
|
for (let room of rooms) {
|
|
|
|
if (socket.id != room) {
|
|
|
|
socket.leave(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("broadcastSync", ({ data, room }) => {
|
|
|
|
socket.to(room).emit("broadcastSync", data);
|
|
|
|
});
|
2023-04-04 20:10:41 +03:00
|
|
|
});
|
|
|
|
|
2023-04-02 10:53:32 +03:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
2023-04-24 09:56:50 +03:00
|
|
|
app.post("/publish", limiter, async (req, res) => {
|
2023-04-02 10:53:32 +03:00
|
|
|
if (isValidNote(req.body)) {
|
2023-04-24 09:56:50 +03:00
|
|
|
let id = await core.publishNote(req.body);
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
res.json({ id });
|
|
|
|
} else {
|
|
|
|
res.status(500).send("Publish failed!");
|
2023-04-02 10:53:32 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.status(403).send("Invalid body!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-24 09:56:50 +03:00
|
|
|
app.get("/get-note/:delorno/:id", async (req, res) => {
|
|
|
|
let note = await core.getNote(req.params.id);
|
|
|
|
if (note) {
|
|
|
|
res.json(note);
|
|
|
|
if (req.params.delorno === "del") await core.deleteNote(req.params.id);
|
|
|
|
} else {
|
2023-04-02 10:53:32 +03:00
|
|
|
res.status(404).send("There is no such note");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(express.static("dist"));
|
|
|
|
|
|
|
|
app.get("*", function (req, res) {
|
|
|
|
res.sendFile(path.join(__dirname, "./dist", "index.html"));
|
|
|
|
});
|
|
|
|
|
2023-04-24 09:56:50 +03:00
|
|
|
core.connect().then(() =>
|
|
|
|
server.listen(process.env.PORT, () => {
|
|
|
|
console.log(`Listening on port ${process.env.PORT}`);
|
|
|
|
})
|
|
|
|
);
|