2020-04-19 16:38:07 +02:00
|
|
|
const { getArgs } = require("./utils");
|
|
|
|
const path = require("path");
|
2018-02-08 17:45:07 +01:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
function startBackendServer(port) {
|
|
|
|
var accessToken = ""; //Can be set here or as start parameter (node server.js --accesstoken=MYTOKEN)
|
|
|
|
var disableSmallestScreen = false; //Can be set to true if you dont want to show (node server.js --disablesmallestscreen=true)
|
|
|
|
var webdav = false; //Can be set to true if you want to allow webdav save (node server.js --webdav=true)
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
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);
|
|
|
|
if (process.env.accesstoken) {
|
|
|
|
accessToken = process.env.accesstoken;
|
2019-02-11 12:43:23 +01:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
if (process.env.disablesmallestscreen) {
|
|
|
|
disablesmallestscreen = true;
|
2018-02-08 23:16:28 +01:00
|
|
|
}
|
2020-04-19 16:38:07 +02:00
|
|
|
if (process.env.webdav) {
|
|
|
|
webdav = true;
|
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
var startArgs = getArgs();
|
|
|
|
if (startArgs["accesstoken"]) {
|
|
|
|
accessToken = startArgs["accesstoken"];
|
|
|
|
}
|
|
|
|
if (startArgs["disablesmallestscreen"]) {
|
|
|
|
disableSmallestScreen = true;
|
|
|
|
}
|
|
|
|
if (startArgs["webdav"]) {
|
|
|
|
webdav = true;
|
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
if (accessToken !== "") {
|
|
|
|
console.log("AccessToken set to: " + accessToken);
|
|
|
|
}
|
|
|
|
if (disableSmallestScreen) {
|
|
|
|
console.log("Disabled showing smallest screen resolution!");
|
|
|
|
}
|
|
|
|
if (webdav) {
|
|
|
|
console.log("Webdav save is enabled!");
|
|
|
|
}
|
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-04-19 16:38:07 +02:00
|
|
|
var smallestScreenResolutions = {};
|
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 () {
|
|
|
|
if (
|
|
|
|
smallestScreenResolutions &&
|
|
|
|
smallestScreenResolutions[whiteboardId] &&
|
|
|
|
socket &&
|
|
|
|
socket.id
|
|
|
|
) {
|
2020-04-19 16:38:07 +02:00
|
|
|
delete smallestScreenResolutions[whiteboardId][socket.id];
|
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
socket.compress(false).broadcast.emit("refreshUserBadges", null); //Removes old user Badges
|
2019-05-07 08:36:42 +02:00
|
|
|
sendSmallestScreenResolution();
|
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"]) {
|
|
|
|
whiteboardId = content["wid"];
|
|
|
|
socket.join(whiteboardId); //Joins room name=wid
|
2020-05-09 15:40:26 +02:00
|
|
|
smallestScreenResolutions[whiteboardId] = smallestScreenResolutions[whiteboardId]
|
|
|
|
? smallestScreenResolutions[whiteboardId]
|
|
|
|
: {};
|
|
|
|
smallestScreenResolutions[whiteboardId][socket.id] = content[
|
|
|
|
"windowWidthHeight"
|
|
|
|
] || { w: 10000, h: 10000 };
|
2020-04-19 16:38:07 +02:00
|
|
|
sendSmallestScreenResolution();
|
|
|
|
} 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-09 15:40:26 +02:00
|
|
|
if (
|
|
|
|
smallestScreenResolutions[whiteboardId] &&
|
|
|
|
(accessToken === "" || accessToken == content["at"])
|
|
|
|
) {
|
|
|
|
smallestScreenResolutions[whiteboardId][socket.id] = content[
|
|
|
|
"windowWidthHeight"
|
|
|
|
] || { w: 10000, h: 10000 };
|
2020-04-19 16:38:07 +02:00
|
|
|
sendSmallestScreenResolution();
|
|
|
|
}
|
|
|
|
});
|
2020-05-09 15:40:26 +02:00
|
|
|
|
2020-04-19 16:38:07 +02:00
|
|
|
function sendSmallestScreenResolution() {
|
|
|
|
if (disableSmallestScreen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var smallestWidth = 10000;
|
|
|
|
var smallestHeight = 10000;
|
|
|
|
for (var i in smallestScreenResolutions[whiteboardId]) {
|
2020-05-09 15:40:26 +02:00
|
|
|
smallestWidth =
|
|
|
|
smallestWidth > smallestScreenResolutions[whiteboardId][i]["w"]
|
|
|
|
? smallestScreenResolutions[whiteboardId][i]["w"]
|
|
|
|
: smallestWidth;
|
|
|
|
smallestHeight =
|
|
|
|
smallestHeight > smallestScreenResolutions[whiteboardId][i]["h"]
|
|
|
|
? smallestScreenResolutions[whiteboardId][i]["h"]
|
|
|
|
: smallestHeight;
|
2020-04-19 16:38:07 +02:00
|
|
|
}
|
2020-05-09 15:40:26 +02:00
|
|
|
io.to(whiteboardId).emit("updateSmallestScreenResolution", {
|
|
|
|
w: smallestWidth,
|
|
|
|
h: smallestHeight,
|
|
|
|
});
|
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;
|