Integrated MeshMessenger for user-to-user.

Ylian Saint-Hilaire committed Dec 1, 2018 at 23:41 UTC fb48cb39459432548e0f83637e1924bec7c91b6e
9 files changed +80 -31
meshcentral.js
+2 -1
@@ -183,7 +183,8 @@ function CreateMeshCentralServer(config, args) {
183 xprocess.stdout.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } if (data.indexOf('Updating settings folder...') >= 0) { xprocess.xrestart = 1; } else if (data.indexOf('Updating server certificates...') >= 0) { xprocess.xrestart = 1; } else if (data.indexOf('Server Ctrl-C exit...') >= 0) { xprocess.xrestart = 2; } else if (data.indexOf('Starting self upgrade...') >= 0) { xprocess.xrestart = 3; } console.log(data); });
184 xprocess.stderr.on('data', function (data) {
185 if (data.startsWith('le.challenges[tls-sni-01].loopback')) { return; } // Ignore this error output from GreenLock
186 - if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } obj.fs.appendFileSync(obj.getConfigFilePath('mesherrors.txt'), '-------- ' + new Date().toLocaleString() + ' ---- ' + obj.currentVer + ' --------\r\n\r\n' + data + '\r\n\r\n\r\n');
186 + if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); }
187 + try { obj.fs.appendFileSync(obj.getConfigFilePath('mesherrors.txt'), '-------- ' + new Date().toLocaleString() + ' ---- ' + obj.currentVer + ' --------\r\n\r\n' + data + '\r\n\r\n\r\n'); } catch (ex) { console.log('ERROR: Unable to write to mesherrors.txt.'); }
188 });
189 xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { /* console.log("Exited with code " + code); */ } });
190 };
meshrelay.js
+7
@@ -94,6 +94,13 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
94 if (obj.id == null) { try { obj.close(); } catch (e) { } return null; } // Attempt to connect without id, drop this.
95 ws._socket.setKeepAlive(true, 240000); // Set TCP keep alive
96
97 + // If this is a MeshMessenger session, the ID is the two userid's and authentication must match one of them.
98 + if (obj.id.startsWith('meshmessenger/')) {
99 + if (obj.user == null) { try { obj.close(); } catch (e) { } return null; }
100 + var x = obj.id.split('/'), user1 = x[1] + '/' + x[2] + '/' + x[3], user2 = x[4] + '/' + x[5] + '/' + x[6];
101 + if ((obj.user._id != user1) && (obj.user._id != user2)) { try { obj.close(); } catch (e) { } return null; }
102 + }
103 +
104 // Validate that the id is valid, we only need to do this on non-authenticated sessions.
105 // TODO: Figure out when this needs to be done.
106 /*
meshuser.js
+19
@@ -592,6 +592,25 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
592 var sessions = obj.parent.wssessions[command.userid];
593 if (sessions != null) { for (i in sessions) { try { sessions[i].send(JSON.stringify(notification)); } catch (ex) { } } }
594
595 + if (obj.parent.parent.multiServer != null) {
596 + // TODO: Add multi-server support
597 + }
598 + break;
599 + }
600 + case 'meshmessenger':
601 + {
602 + // Send a notification message to a user
603 + if ((user.siteadmin & 2) == 0) break;
604 + if (obj.common.validateString(command.userid, 1, 2048) == false) break;
605 +
606 + // Create the notification message
607 + var notification = {
608 + "action": "msg", "type": "notify", "value": "<b>" + user.name + "</b>: Chat Request, Click here to accept.", "userid": user._id, "username": user.name, "tag": 'meshmessenger/' + encodeURIComponent(command.userid) + '/' + encodeURIComponent(user._id) };
609 +
610 + // Get the list of sessions for this user
611 + var sessions = obj.parent.wssessions[command.userid];
612 + if (sessions != null) { for (i in sessions) { try { sessions[i].send(JSON.stringify(notification)); } catch (ex) { } } }
613 +
614 if (obj.parent.parent.multiServer != null) {
615 // TODO: Add multi-server support
616 }
public/images/icon-chat.png
Binary files /dev/null and b/public/images/icon-chat.png differ
public/messenger.htm
+25 -21
@@ -1,6 +1,7 @@
1 <!DOCTYPE html>
2 <html style=height:100%>
3 <head>
4 + <title>MeshMessenger</title>
5 <meta http-equiv=X-UA-Compatible content="IE=edge">
6 <meta content="text/html;charset=utf-8" http-equiv=Content-Type>
7 <meta name=format-detection content="telephone=no">
@@ -8,7 +9,7 @@
9 <script type="text/javascript" src="scripts/common-0.0.1.js"></script>
10 </head>
11 <body style="font-family:Arial,Helvetica,sans-serif">
11 - <div id="xtop" style="position:absolute;left:0;right:0;top:0;height:30px;background-color:#036;color:#c8c8c8"><div style="padding-top:6px;padding-left:6px;font-size:medium"><b>Messenger<span id="xtitle"></span></b></div></div>
12 + <div id="xtop" style="position:absolute;left:0;right:0;top:0;height:30px;background-color:#036;color:#c8c8c8"><div style="padding-top:6px;padding-left:6px;font-size:medium"><b>MeshMessenger<span id="xtitle"></span></b></div></div>
13 <div id="xmiddle" style="position:absolute;left:0;right:0;top:30px;bottom:30px">
14 <div id="xmsg" style="position:absolute;left:0;right:0;top:0px;bottom:0px;padding:5px;overflow-y:scroll"></div>
15 </div>
@@ -25,7 +26,7 @@
26 var state = 0;
27
28 // Set the title
28 - if (args.title) { QH('xtitle', ' - ' + args.title); }
29 + if (args.title) { QH('xtitle', ' - ' + args.title); document.title = document.title + ' - ' + args.title; }
30
31 // Trap document key presses
32 document.onkeypress = function ondockeypress(e) {
@@ -76,31 +77,34 @@
77 function haltEvent(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); return false; }
78 function parseUriArgs() { var name, r = {}, parsedUri = window.document.location.href.split(/[\?&|\=]/); parsedUri.splice(0, 1); for (x in parsedUri) { switch (x % 2) { case 0: { name = parsedUri[x]; break; } case 1: { r[name] = parsedUri[x]; var x = parseInt(r[name]); if (x == r[name]) { r[name] = x; } break; } } } return r; }
79
79 - // Get started
80 - enableControls(false);
81 - if ((typeof args.id == 'string') && (args.id.length > 0)) {
82 - xcontrol('Connecting...');
83 - socket = new WebSocket(window.location.protocol.replace("http", "ws") + "//" + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/meshrelay.ashx?id=' + args.id);
84 - socket.onopen = function () { xcontrol('Waiting for other user...'); }
85 - socket.onerror = function (e) { console.error(e); }
86 - socket.onclose = function () { enableControls(false); xcontrol('Connection closed.'); socket = null; }
87 - socket.onmessage = function (msg) {
88 - if ((state == 0) && (typeof msg.data == 'string')) { enableControls(true); xcontrol('Connected.'); state = 1; return; }
89 - if (state == 1) {
90 - if (typeof msg.data == 'string') {
91 - var obj = JSON.parse(msg.data);
92 - switch (obj.action) {
93 - case 'chat': { xrecv(obj.msg); break; }
80 + function start() {
81 + // Get started
82 + enableControls(false);
83 + if ((typeof args.id == 'string') && (args.id.length > 0)) {
84 + //xcontrol('Connecting...');
85 + socket = new WebSocket(window.location.protocol.replace("http", "ws") + "//" + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/meshrelay.ashx?id=' + args.id);
86 + socket.onopen = function () { state = 1; xcontrol('Waiting for other user...'); }
87 + socket.onerror = function (e) { console.error(e); }
88 + socket.onclose = function () { enableControls(false); if (state > 0) { xcontrol('Connection closed.'); } socket = null; if (state > 1) { setTimeout(start, 500); } state = 0; }
89 + socket.onmessage = function (msg) {
90 + if ((state < 2) && (typeof msg.data == 'string')) { enableControls(true); xcontrol('Connected.'); state = 2; return; }
91 + if (state == 2) {
92 + if (typeof msg.data == 'string') {
93 + var obj = JSON.parse(msg.data);
94 + switch (obj.action) {
95 + case 'chat': { xrecv(obj.msg); break; }
96 + }
97 + } else {
98 + //xrecv(JSON.stringify(msg));
99 }
95 - } else {
96 - //xrecv(JSON.stringify(msg));
100 }
101 }
102 + } else {
103 + xcontrol('Error: No connection key specified.');
104 }
100 - } else {
101 - xcontrol('Error: No connection key specified.');
105 }
106
107 + start();
108 </script>
109 </body>
110 </html>
views/default.handlebars
+20 -3
@@ -1195,6 +1195,7 @@
1195 var n = { text:message.value };
1196 if (message.nodeid != null) { n.nodeid = message.nodeid; }
1197 if (message.tag != null) { n.tag = message.tag; }
1198 + if (message.username != null) { n.username = message.username; }
1199 addNotification(n);
1200 } else if (message.type == 'ps') {
1201 showDeskToolsProcesses(message);
@@ -1204,6 +1205,7 @@
1205 if (message.type == 'notify') { // This is a notification message.
1206 var n = { text:message.value };
1207 if (message.tag != null) { n.tag = message.tag; }
1208 + if (message.username != null) { n.username = message.username; }
1209 addNotification(n);
1210 }
1211 }
@@ -5720,10 +5722,13 @@
5722 }
5723
5724 function addUserHtml(user, sessions) {
5723 - var x = '', gray = ' gray', icon = 'm2', msg = '', self = (user.name != userinfo.name);
5725 + var x = '', gray = ' gray', icon = 'm2', msg = '', msg2 = '', self = (user.name != userinfo.name);
5726 if (sessions != null) {
5727 gray = '';
5726 - if (self) { msg += "<a onclick=showUserAlertDialog(event,\"" + encodeURIComponent(user._id) + "\")>"; }
5728 + if (self) {
5729 + msg2 = "<span style=float:right;margin-top:1px;margin-left:4px;margin-right:4px title=Chat><a onclick=userChat(event,\"" + encodeURIComponent(user._id) + "\",\"" + encodeURIComponent(user.name) + "\")><img src='images/icon-chat.png' height=16 width=16 style=padding-top:2px /></a></span>";
5730 + msg += "<a onclick=showUserAlertDialog(event,\"" + encodeURIComponent(user._id) + "\")>";
5731 + }
5732 if (sessions == 1) { msg += '1 active session'; } else { msg += sessions + ' active sessions'; }
5733 if (self) { msg += "</a>"; }
5734 } else {
@@ -5752,7 +5757,7 @@
5757 x += '<div class=bar style=height:24px;width:100%;font-size:medium>';
5758 x += '<div style=float:left;height:24px;width:24px;background-color:white><div class="' + icon + gray + '" style=width:16px;margin-top:4px;margin-left:2px;height:16px></div></div>';
5759 x += '<div class=g1 style=height:24px;float:left></div><div class=g2 style=height:24px;float:right></div>';
5755 - x += '<div><span>' + username + '</span><span style=float:right>' + msg + '</span></div></div>'; // </td></tr>
5760 + x += '<div><span>' + username + '</span>' + msg2 + '<span style=float:right>' + msg + '</span></div></div>'; // </td></tr>
5761 return x;
5762 }
5763
@@ -5765,6 +5770,13 @@
5770 element.children[0].children[0].style['background-color'] = ((over == 0) ? '#c9c9c9' : '#b9b9b9');
5771 }
5772
5773 + function userChat(e, userid, name) {
5774 + haltEvent(e);
5775 + window.open('/messenger.htm?id=meshmessenger/' + userid + '/' + encodeURIComponent(userinfo._id) + '&title=' + name, 'meshmessenger:' + userid);
5776 + meshserver.send({ action: 'meshmessenger', userid: decodeURIComponent(userid) });
5777 + return false;
5778 + }
5779 +
5780 function showUserAlertDialog(e, userid) {
5781 if (xxdialogMode) return;
5782 haltEvent(e);
@@ -6223,6 +6235,11 @@
6235 else if (n.tag == 'intelamt') gotoDevice(n.nodeid, 14); // Intel AMT
6236 else if (n.tag == 'console') gotoDevice(n.nodeid, 15); // Files
6237 else gotoDevice(n.nodeid, 10); // General
6238 + } else {
6239 + if (n.tag.startsWith('meshmessenger/')) {
6240 + window.open('/messenger.htm?id=' + n.tag + '&title=' + encodeURIComponent(n.username), n.tag.split('/')[2]);
6241 + notificationDelete(id);
6242 + }
6243 }
6244 }
6245 }
views/login-mobile.handlebars
+1 -1
@@ -193,7 +193,7 @@
193 validateLogin();
194 validateCreate();
195 if ('{{loginmode}}' != '') { go(parseInt('{{loginmode}}')); } else { go(1); }
196 - QV('newAccountDiv', '{{{newAccount}}}' != '0' );
196 + QV('newAccountDiv', ('{{{newAccount}}}' != '0') && ('{{{newAccount}}}' != 'false')); // If new accounts are not allowed, don't display the new account link.
197 if ((passhint != null) && (passhint.length > 0)) { QV("showPassHintLink", true); }
198 QV("newAccountPass", (newAccountPass == 1));
199 QV("resetAccountDiv", (emailCheck == true));
views/login.handlebars
+1 -1
@@ -276,7 +276,7 @@
276 validateLogin();
277 validateCreate();
278 if ('{{loginmode}}' != '') { go(parseInt('{{loginmode}}')); } else { go(1); }
279 - QV('newAccountDiv', '{{{newAccount}}}' != '0' );
279 + QV('newAccountDiv', ('{{{newAccount}}}' != '0') && ('{{{newAccount}}}' != 'false')); // If new accounts are not allowed, don't display the new account link.
280 if ((passhint != null) && (passhint.length > 0)) { QV("showPassHintLink", true); }
281 QV("newAccountPass", (newAccountPass == 1));
282 QV("resetAccountDiv", (emailCheck == true));
webserver.js
+5 -4
@@ -220,7 +220,8 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
220 for (i in docs) { var u = obj.users[docs[i]._id] = docs[i]; domainUserCount[u.domain]++; }
221 for (i in parent.config.domains) {
222 if (domainUserCount[i] == 0) {
223 - if (parent.config.domains[i].newaccounts == 0) { parent.config.domains[i].newaccounts = 2; }
223 + // If newaccounts is set to no new accounts, but no accounts exists, temporarly allow account creation.
224 + if ((parent.config.domains[i].newaccounts === 0) || (parent.config.domains[i].newaccounts === false)) { parent.config.domains[i].newaccounts = 2; }
225 console.log('Server ' + ((i == '') ? '' : (i + ' ')) + 'has no users, next new account will be site administrator.');
226 }
227 }
@@ -390,7 +391,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
391 function handleCreateAccountRequest(req, res) {
392 var domain = checkUserIpAddress(req, res);
393 if (domain == null) return;
393 - if (domain.newaccounts == 0) { res.sendStatus(401); return; }
394 + if ((domain.newaccounts === 0) || (domain.newaccounts === false)) { res.sendStatus(401); return; }
395 if (!obj.common.validateUsername(req.body.username, 1, 64) || !obj.common.validateEmail(req.body.email, 1, 256) || !obj.common.validateString(req.body.password1, 1, 256) || !obj.common.validateString(req.body.password2, 1, 256) || (req.body.password1 != req.body.password2) || req.body.username == '~') {
396 req.session.loginmode = 2;
397 req.session.error = '<b style=color:#8C001A>Unable to create account.</b>';
@@ -420,7 +421,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
421 var user = { type: 'user', _id: 'user/' + domain.id + '/' + req.body.username.toLowerCase(), name: req.body.username, email: req.body.email, creation: Date.now(), login: Date.now(), domain: domain.id, passhint: hint };
422 var usercount = 0;
423 for (var i in obj.users) { if (obj.users[i].domain == domain.id) { usercount++; } }
423 - if (usercount == 0) { user.siteadmin = 0xFFFFFFFF; if (domain.newaccounts == 2) { domain.newaccounts = 0; } } // If this is the first user, give the account site admin.
424 + if (usercount == 0) { user.siteadmin = 0xFFFFFFFF; if (domain.newaccounts === 2) { domain.newaccounts = 0; } } // If this is the first user, give the account site admin.
425 obj.users[user._id] = user;
426 req.session.userid = user._id;
427 req.session.domainid = domain.id;
@@ -444,7 +445,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
445 function handleResetAccountRequest(req, res) {
446 var domain = checkUserIpAddress(req, res);
447 if (domain == null) return;
447 - if (domain.newaccounts == 0) { res.sendStatus(401); return; }
448 + if ((domain.newaccounts === 0) || (domain.newaccounts === false)) { res.sendStatus(401); return; }
449 if (!req.body.email || checkEmail(req.body.email) == false) {
450 req.session.loginmode = 3;
451 req.session.error = '<b style=color:#8C001A>Invalid email.</b>';