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-12 21:32:46 +02:00
|
|
|
const ReadOnlyBackendService = require("./services/ReadOnlyBackendService");
|
2020-05-12 22:33:33 +02:00
|
|
|
const WhiteboardInfoBackendService = require("./services/WhiteboardInfoBackendService");
|
2020-05-10 15:34:19 +02:00
|
|
|
|
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-05-12 22:33:33 +02:00
|
|
|
WhiteboardInfoBackendService.start(io);
|
|
|
|
|
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-11 16:00:45 +02:00
|
|
|
const { accessToken, enableWebdav } = config.backend;
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
app.get("/api/loadwhiteboard", function (req, res) {
|
2020-05-12 21:32:46 +02:00
|
|
|
const wid = req["query"]["wid"];
|
|
|
|
const at = req["query"]["at"]; //accesstoken
|
2020-04-19 16:38:07 +02:00
|
|
|
if (accessToken === "" || accessToken == at) {
|
2020-05-12 21:32:46 +02:00
|
|
|
const widForData = ReadOnlyBackendService.isReadOnly(wid)
|
|
|
|
? ReadOnlyBackendService.getIdFromReadOnlyId(wid)
|
|
|
|
: wid;
|
|
|
|
const ret = s_whiteboard.loadStoredData(widForData);
|
2020-04-19 16:38:07 +02:00
|
|
|
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");
|
2020-05-12 21:55:43 +02:00
|
|
|
const fields = escapeAllContentStrings(formData.fields);
|
|
|
|
const wid = fields["whiteboardId"];
|
|
|
|
if (ReadOnlyBackendService.isReadOnly(wid)) return;
|
|
|
|
|
|
|
|
const readOnlyWid = ReadOnlyBackendService.getReadOnlyId(wid);
|
|
|
|
|
|
|
|
const name = fields["name"] || "";
|
|
|
|
const date = fields["date"] || +new Date();
|
|
|
|
const filename = `${readOnlyWid}_${date}.png`;
|
|
|
|
let webdavaccess = fields["webdavaccess"] || false;
|
2020-04-19 16:38:07 +02:00
|
|
|
try {
|
|
|
|
webdavaccess = JSON.parse(webdavaccess);
|
|
|
|
} catch (e) {
|
|
|
|
webdavaccess = false;
|
2019-03-12 11:41:04 +01:00
|
|
|
}
|
2020-05-12 21:55:43 +02:00
|
|
|
|
|
|
|
const savingDir = path.join("./public/uploads", readOnlyWid);
|
|
|
|
fs.ensureDir(savingDir, function (err) {
|
2020-04-19 16:38:07 +02:00
|
|
|
if (err) {
|
|
|
|
console.log("Could not create upload folder!", err);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-12 21:55:43 +02:00
|
|
|
let 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-12 21:55:43 +02:00
|
|
|
const savingPath = path.join(savingDir, filename);
|
|
|
|
fs.writeFile(savingPath, 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-05-11 16:00:45 +02:00
|
|
|
if (enableWebdav) {
|
2020-05-12 21:55:43 +02:00
|
|
|
saveImageToWebdav(savingPath, filename, webdavaccess, function (
|
|
|
|
err
|
|
|
|
) {
|
|
|
|
if (err) {
|
|
|
|
console.log("error", err);
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
callback();
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
2020-05-12 21:55:43 +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) {
|
2020-05-12 21:55:43 +02:00
|
|
|
const webdavserver = webdavaccess["webdavserver"] || "";
|
|
|
|
const webdavpath = webdavaccess["webdavpath"] || "/";
|
|
|
|
const webdavusername = webdavaccess["webdavusername"] || "";
|
|
|
|
const 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) => {
|
2020-05-12 21:55:43 +02:00
|
|
|
const cloudpath = webdavpath + "" + filename;
|
2020-05-09 15:40:26 +02:00
|
|
|
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
|
|
|
|
|
|
|
io.on("connection", function (socket) {
|
2020-05-12 21:32:46 +02:00
|
|
|
let whiteboardId = null;
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.on("disconnect", function () {
|
2020-05-12 22:59:15 +02:00
|
|
|
WhiteboardInfoBackendService.leave(socket.id, whiteboardId);
|
2020-05-12 22:33:33 +02:00
|
|
|
socket.compress(false).broadcast.to(whiteboardId).emit("refreshUserBadges", null); //Removes old user Badges
|
2020-04-19 16:38:07 +02:00
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
|
|
|
socket.on("drawToWhiteboard", function (content) {
|
2020-05-12 21:32:46 +02:00
|
|
|
if (!whiteboardId || ReadOnlyBackendService.isReadOnly(whiteboardId)) return;
|
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
content = escapeAllContentStrings(content);
|
|
|
|
if (accessToken === "" || accessToken == content["at"]) {
|
2020-05-12 21:32:46 +02:00
|
|
|
const broadcastTo = (wid) =>
|
|
|
|
socket.compress(false).broadcast.to(wid).emit("drawToWhiteboard", content);
|
|
|
|
// broadcast to current whiteboard
|
|
|
|
broadcastTo(whiteboardId);
|
2020-05-12 22:33:33 +02:00
|
|
|
// broadcast the same content to the associated read-only whiteboard
|
2020-05-12 21:32:46 +02:00
|
|
|
const readOnlyId = ReadOnlyBackendService.getReadOnlyId(whiteboardId);
|
2020-05-12 22:33:33 +02:00
|
|
|
broadcastTo(readOnlyId);
|
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"]) {
|
|
|
|
whiteboardId = content["wid"];
|
2020-05-12 21:32:46 +02:00
|
|
|
|
|
|
|
socket.emit("whiteboardConfig", {
|
|
|
|
common: config.frontend,
|
|
|
|
whiteboardSpecific: {
|
2020-05-12 21:55:43 +02:00
|
|
|
correspondingReadOnlyWid: ReadOnlyBackendService.getReadOnlyId(
|
|
|
|
whiteboardId
|
|
|
|
),
|
2020-05-12 21:32:46 +02:00
|
|
|
isReadOnly: ReadOnlyBackendService.isReadOnly(whiteboardId),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
socket.join(whiteboardId); //Joins room name=wid
|
2020-05-12 22:33:33 +02:00
|
|
|
const screenResolution = content["windowWidthHeight"];
|
|
|
|
WhiteboardInfoBackendService.join(socket.id, whiteboardId, screenResolution);
|
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"]) {
|
2020-05-12 22:33:33 +02:00
|
|
|
const screenResolution = content["windowWidthHeight"];
|
|
|
|
WhiteboardInfoBackendService.setScreenResolution(
|
2020-05-10 16:43:11 +02:00
|
|
|
socket.id,
|
2020-05-12 22:33:33 +02:00
|
|
|
whiteboardId,
|
|
|
|
screenResolution
|
2020-05-10 16:43:11 +02:00
|
|
|
);
|
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;
|