2023-04-02 10:53:32 +03:00
|
|
|
const sha3 = require("js-sha3").sha3_512;
|
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const isValidNote = require("./note_validator");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const cryptojs = require("crypto-js");
|
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-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-19 18:10:03 +03:00
|
|
|
max: 10,
|
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-19 18:06:50 +03:00
|
|
|
app.post("/publish", limiter, function (req, res) {
|
2023-04-02 10:53:32 +03:00
|
|
|
if (isValidNote(req.body)) {
|
|
|
|
let hash = sha3(JSON.stringify(req.body));
|
|
|
|
req.body.time = Date.now();
|
2023-04-03 17:03:01 +03:00
|
|
|
req.body.pub = true;
|
|
|
|
req.body.pubTime = req.body.time;
|
|
|
|
|
2023-04-02 10:53:32 +03:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(
|
|
|
|
`./notes/${hash}.json`,
|
|
|
|
cryptojs.AES.encrypt(
|
|
|
|
JSON.stringify(req.body),
|
|
|
|
process.env.KEY
|
|
|
|
).toString()
|
|
|
|
);
|
|
|
|
res.send({ id: hash });
|
|
|
|
} catch {
|
|
|
|
res.status(500).send("Failed to write file");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.status(403).send("Invalid body!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/get-note/:delorno/:id", function (req, res) {
|
|
|
|
let path = `./notes/${req.params.id}.json`;
|
|
|
|
try {
|
|
|
|
let data = JSON.parse(
|
|
|
|
cryptojs.AES.decrypt(
|
|
|
|
fs.readFileSync(path, "utf-8"),
|
|
|
|
process.env.KEY
|
|
|
|
).toString(cryptojs.enc.Utf8)
|
|
|
|
);
|
|
|
|
res.send(data);
|
|
|
|
if (req.params.delorno === "del") fs.unlinkSync(path);
|
|
|
|
} catch {
|
|
|
|
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-03 14:07:47 +03:00
|
|
|
server.listen(process.env.PORT, () => {
|
2023-04-02 10:53:32 +03:00
|
|
|
console.log(`Listening on port ${process.env.PORT}`);
|
|
|
|
});
|