2020-04-19 16:38:07 +02:00
|
|
|
const path = require("path");
|
2018-02-08 17:45:07 +01:00
|
|
|
|
2020-05-11 14:18:59 +02:00
|
|
|
const config = require("./config/config");
|
2020-05-10 15:34:19 +02:00
|
|
|
const WhiteboardServerSideInfo = require("./WhiteboardServerSideInfo");
|
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
function startBackendServer(port) {
|
|
|
|
var fs = require("fs-extra");
|
2020-05-09 15:40:26 +02:00
|
|
|
var express = require("express");
|
|
|
|
var formidable = require("formidable"); //form upload processing
|
|
|
|
|
|
|
|
const createDOMPurify = require("dompurify"); //Prevent xss
|
|
|
|
const { JSDOM } = require("jsdom");
|
|
|
|
const window = new JSDOM("").window;
|
2020-04-19 16:38:07 +02:00
|
|
|
const DOMPurify = createDOMPurify(window);
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
const { createClient } = require("webdav");
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
var s_whiteboard = require("./s_whiteboard.js");
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
var app = express();
|
2020-05-09 15:40:26 +02:00
|
|
|
app.use(express.static(path.join(__dirname, "..", "dist")));
|
|
|
|
app.use("/uploads", express.static(path.join(__dirname, "..", "public", "uploads")));
|
|
|
|
var server = require("http").Server(app);
|
2020-04-19 16:38:07 +02:00
|
|
|
server.listen(port);
|
2020-05-09 15:40:26 +02:00
|
|
|
var io = require("socket.io")(server, { path: "/ws-api" });
|
2020-04-19 16:38:07 +02:00
|
|
|
console.log("Webserver & socketserver running on port:" + port);
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-05-10 22:19:32 +02:00
|
|
|
const { accessToken, webdav } = config.backend;
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
app.get("/api/loadwhiteboard", function (req, res) {
|
2020-04-19 16:38:07 +02:00
|
|
|
var wid = req["query"]["wid"];
|
|
|
|
var at = req["query"]["at"]; //accesstoken
|
|
|
|
if (accessToken === "" || accessToken == at) {
|
|
|
|
var ret = s_whiteboard.loadStoredData(wid);
|
|
|
|
res.send(ret);
|
|
|
|
res.end();
|
2019-02-11 12:43:23 +01:00
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
res.status(401); //Unauthorized
|
2019-02-11 12:43:23 +01:00
|
|
|
res.end();
|
|
|
|
}
|
2018-02-08 23:16:28 +01:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
app.post("/api/upload", function (req, res) {
|
|
|
|
//File upload
|
2020-04-19 16:38:07 +02:00
|
|
|
var form = new formidable.IncomingForm(); //Receive form
|
|
|
|
var formData = {
|
|
|
|
files: {},
|
2020-05-09 15:40:26 +02:00
|
|
|
fields: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
form.on("file", function (name, file) {
|
2020-04-19 16:38:07 +02:00
|
|
|
formData["files"][file.name] = file;
|
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
form.on("field", function (name, value) {
|
2020-04-19 16:38:07 +02:00
|
|
|
formData["fields"][name] = value;
|
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
form.on("error", function (err) {
|
|
|
|
console.log("File uplaod Error!");
|
2020-04-19 16:38:07 +02:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
form.on("end", function () {
|
2020-04-19 16:38:07 +02:00
|
|
|
if (accessToken === "" || accessToken == formData["fields"]["at"]) {
|
|
|
|
progressUploadFormData(formData, function (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err == "403") {
|
|
|
|
res.status(403);
|
2019-07-01 13:15:11 +02:00
|
|
|
} else {
|
2020-04-19 16:38:07 +02:00
|
|
|
res.status(500);
|
2019-07-01 13:15:11 +02:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
res.end();
|
2019-07-01 13:15:11 +02:00
|
|
|
} else {
|
2020-04-19 16:38:07 +02:00
|
|
|
res.send("done");
|
2019-07-01 13:15:11 +02:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
res.status(401); //Unauthorized
|
2020-04-19 16:38:07 +02:00
|
|
|
res.end();
|
2019-07-01 13:15:11 +02:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
//End file upload
|
2019-07-01 13:15:11 +02:00
|
|
|
});
|
2020-04-19 16:38:07 +02:00
|
|
|
form.parse(req);
|
2018-02-08 23:38:41 +01:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
function progressUploadFormData(formData, callback) {
|
|
|
|
console.log("Progress new Form Data");
|
|
|
|
var fields = escapeAllContentStrings(formData.fields);
|
|
|
|
var files = formData.files;
|
|
|
|
var whiteboardId = fields["whiteboardId"];
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
var name = fields["name"] || "";
|
2020-05-09 15:40:26 +02:00
|
|
|
var date = fields["date"] || +new Date();
|
2020-04-19 16:38:07 +02:00
|
|
|
var filename = whiteboardId + "_" + date + ".png";
|
|
|
|
var webdavaccess = fields["webdavaccess"] || false;
|
|
|
|
try {
|
|
|
|
webdavaccess = JSON.parse(webdavaccess);
|
|
|
|
} catch (e) {
|
|
|
|
webdavaccess = false;
|
2019-03-12 11:41:04 +01:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
fs.ensureDir("./public/uploads", function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log("Could not create upload folder!", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var imagedata = fields["imagedata"];
|
2020-05-09 15:40:26 +02:00
|
|
|
if (imagedata && imagedata != "") {
|
|
|
|
//Save from base64 data
|
|
|
|
imagedata = imagedata
|
|
|
|
.replace(/^data:image\/png;base64,/, "")
|
|
|
|
.replace(/^data:image\/jpeg;base64,/, "");
|
2020-04-19 16:38:07 +02:00
|
|
|
console.log(filename, "uploaded");
|
2020-05-09 15:40:26 +02:00
|
|
|
fs.writeFile("./public/uploads/" + filename, imagedata, "base64", function (err) {
|
2020-04-19 16:38:07 +02:00
|
|
|
if (err) {
|
|
|
|
console.log("error", err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
if (webdavaccess) {
|
|
|
|
//Save image to webdav
|
2020-04-19 16:38:07 +02:00
|
|
|
if (webdav) {
|
2020-05-09 15:40:26 +02:00
|
|
|
saveImageToWebdav(
|
|
|
|
"./public/uploads/" + filename,
|
|
|
|
filename,
|
|
|
|
webdavaccess,
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.log("error", err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
);
|
2020-04-19 16:38:07 +02:00
|
|
|
} else {
|
|
|
|
callback("Webdav is not enabled on the server!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback("no imagedata!");
|
|
|
|
console.log("No image Data found for this upload!", name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
function saveImageToWebdav(imagepath, filename, webdavaccess, callback) {
|
|
|
|
if (webdavaccess) {
|
|
|
|
var webdavserver = webdavaccess["webdavserver"] || "";
|
|
|
|
var webdavpath = webdavaccess["webdavpath"] || "/";
|
|
|
|
var webdavusername = webdavaccess["webdavusername"] || "";
|
|
|
|
var webdavpassword = webdavaccess["webdavpassword"] || "";
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
const client = createClient(webdavserver, {
|
|
|
|
username: webdavusername,
|
|
|
|
password: webdavpassword,
|
2020-04-19 16:38:07 +02:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
client
|
|
|
|
.getDirectoryContents(webdavpath)
|
|
|
|
.then((items) => {
|
|
|
|
var cloudpath = webdavpath + "" + filename;
|
|
|
|
console.log("webdav saving to:", cloudpath);
|
|
|
|
fs.createReadStream(imagepath).pipe(client.createWriteStream(cloudpath));
|
|
|
|
callback();
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
callback("403");
|
|
|
|
console.log("Could not connect to webdav!");
|
|
|
|
});
|
2019-02-11 12:43:23 +01:00
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
callback("Error: no access data!");
|
2019-02-11 12:43:23 +01:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-05-10 15:34:19 +02:00
|
|
|
/**
|
|
|
|
* @type {Map<string, WhiteboardServerSideInfo>}
|
|
|
|
*/
|
|
|
|
const infoByWhiteboard = new Map();
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
infoByWhiteboard.forEach((info, whiteboardId) => {
|
|
|
|
if (info.shouldSendInfo()) {
|
|
|
|
io.sockets
|
|
|
|
.in(whiteboardId)
|
|
|
|
.compress(false)
|
|
|
|
.emit("whiteboardInfoUpdate", info.asObject());
|
|
|
|
info.infoWasSent();
|
|
|
|
}
|
|
|
|
});
|
2020-05-10 22:19:32 +02:00
|
|
|
}, (1 / config.backend.performance.whiteboardInfoBroadcastFreq) * 1000);
|
2020-05-10 15:34:19 +02:00
|
|
|
|
2020-05-09 15:40:26 +02:00
|
|
|
io.on("connection", function (socket) {
|
2020-04-19 16:38:07 +02:00
|
|
|
var whiteboardId = null;
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.on("disconnect", function () {
|
2020-05-10 23:10:54 +02:00
|
|
|
if (infoByWhiteboard.has(whiteboardId)) {
|
2020-05-10 23:13:55 +02:00
|
|
|
const whiteboardServerSideInfo = infoByWhiteboard.get(whiteboardId);
|
2020-05-10 16:43:11 +02:00
|
|
|
|
2020-05-10 23:13:55 +02:00
|
|
|
if (socket && socket.id) {
|
|
|
|
whiteboardServerSideInfo.deleteScreenResolutionOfClient(socket.id);
|
|
|
|
}
|
2020-05-10 15:34:19 +02:00
|
|
|
|
2020-05-10 23:13:55 +02:00
|
|
|
whiteboardServerSideInfo.decrementNbConnectedUsers();
|
2020-05-10 23:10:54 +02:00
|
|
|
|
2020-05-10 23:13:55 +02:00
|
|
|
if (whiteboardServerSideInfo.hasConnectedUser()) {
|
|
|
|
socket.compress(false).broadcast.emit("refreshUserBadges", null); //Removes old user Badges
|
|
|
|
} else {
|
|
|
|
infoByWhiteboard.delete(whiteboardId);
|
|
|
|
}
|
2020-05-10 23:10:54 +02:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
socket.on("drawToWhiteboard", function (content) {
|
2020-04-19 16:38:07 +02:00
|
|
|
content = escapeAllContentStrings(content);
|
|
|
|
if (accessToken === "" || accessToken == content["at"]) {
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.compress(false).broadcast.to(whiteboardId).emit("drawToWhiteboard", content); //Send to all users in the room (not own socket)
|
2020-04-19 16:38:07 +02:00
|
|
|
s_whiteboard.handleEventsAndData(content); //save whiteboardchanges on the server
|
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.emit("wrongAccessToken", true);
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
socket.on("joinWhiteboard", function (content) {
|
2020-04-19 16:38:07 +02:00
|
|
|
content = escapeAllContentStrings(content);
|
|
|
|
if (accessToken === "" || accessToken == content["at"]) {
|
2020-05-10 23:13:55 +02:00
|
|
|
socket.emit("whiteboardConfig", { common: config.frontend });
|
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
whiteboardId = content["wid"];
|
|
|
|
socket.join(whiteboardId); //Joins room name=wid
|
2020-05-10 15:34:19 +02:00
|
|
|
if (!infoByWhiteboard.has(whiteboardId)) {
|
|
|
|
infoByWhiteboard.set(whiteboardId, new WhiteboardServerSideInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
const whiteboardServerSideInfo = infoByWhiteboard.get(whiteboardId);
|
|
|
|
whiteboardServerSideInfo.incrementNbConnectedUsers();
|
2020-05-10 16:43:11 +02:00
|
|
|
whiteboardServerSideInfo.setScreenResolutionForClient(
|
|
|
|
socket.id,
|
|
|
|
content["windowWidthHeight"] || WhiteboardServerSideInfo.defaultScreenResolution
|
|
|
|
);
|
2020-04-19 16:38:07 +02:00
|
|
|
} else {
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.emit("wrongAccessToken", true);
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
socket.on("updateScreenResolution", function (content) {
|
2020-04-19 16:38:07 +02:00
|
|
|
content = escapeAllContentStrings(content);
|
2020-05-10 16:43:11 +02:00
|
|
|
if (accessToken === "" || accessToken == content["at"]) {
|
|
|
|
const whiteboardServerSideInfo = infoByWhiteboard.get(whiteboardId);
|
|
|
|
whiteboardServerSideInfo.setScreenResolutionForClient(
|
|
|
|
socket.id,
|
|
|
|
content["windowWidthHeight"] || WhiteboardServerSideInfo.defaultScreenResolution
|
|
|
|
);
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
});
|
2019-05-07 08:36:42 +02:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
//Prevent cross site scripting (xss)
|
|
|
|
function escapeAllContentStrings(content, cnt) {
|
2020-05-09 15:40:26 +02:00
|
|
|
if (!cnt) cnt = 0;
|
|
|
|
|
|
|
|
if (typeof content === "string") {
|
2020-04-19 16:38:07 +02:00
|
|
|
return DOMPurify.sanitize(content);
|
2019-05-07 08:36:42 +02:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
for (var i in content) {
|
2020-05-09 15:40:26 +02:00
|
|
|
if (typeof content[i] === "string") {
|
2020-04-19 16:38:07 +02:00
|
|
|
content[i] = DOMPurify.sanitize(content[i]);
|
2020-05-09 15:40:26 +02:00
|
|
|
}
|
|
|
|
if (typeof content[i] === "object" && cnt < 10) {
|
2020-04-19 16:38:07 +02:00
|
|
|
content[i] = escapeAllContentStrings(content[i], ++cnt);
|
|
|
|
}
|
2018-02-08 23:43:14 +01:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
return content;
|
2018-02-08 23:43:14 +01:00
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
process.on("unhandledRejection", (error) => {
|
2020-04-19 16:38:07 +02:00
|
|
|
// Will print "unhandledRejection err is not defined"
|
2020-05-09 15:40:26 +02:00
|
|
|
console.log("unhandledRejection", error.message);
|
|
|
|
});
|
2019-07-01 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 15:40:26 +02:00
|
|
|
module.exports = startBackendServer;
|