Added send function for serverCommands
Noah Zalev committed
Jan 10, 2022 at 15:39 UTC
979ad000b4cd96daba3a17b4eb9f95e1b061afef
1 file changed
+39
-40
meshuser.js
+39
-40
@@ -116,8 +116,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
116
obj.expireTimer = setTimeout(function () { for (var i in req.session) { delete req.session[i]; } obj.close(); }, delta);
117
}
118
119
- // Send a message to the user
120
- //obj.send = function (data) { try { if (typeof data == 'string') { ws.send(Buffer.from(data, 'binary')); } else { ws.send(data); } } catch (e) { } }
119
+ // Send data through the websocket
120
+ obj.send = function (object) { try { ws.send(JSON.stringify(object)); } catch(e) {} }
121
122
// Clean a IPv6 address that encodes a IPv4 address
123
function cleanRemoteAddr(addr) { if (addr.startsWith('::ffff:')) { return addr.substring(7); } else { return addr; } }
@@ -4813,7 +4813,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4813
4814
// Handle any errors
4815
if (err != null) {
4816
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: err })); } catch (ex) { } }
4816
+ if (command.responseid != null) { obj.send({ action: 'adddeviceuser', responseid: command.responseid, result: err }); }
4817
return;
4818
}
4819
@@ -4929,7 +4929,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4929
parent.parent.DispatchEvent(dispatchTargets, obj, event);
4930
}
4931
4932
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
4932
+ if (command.responseid != null) { obj.send({ action: 'adddeviceuser', responseid: command.responseid, result: 'ok' }); }
4933
});
4934
}
4935
@@ -4964,7 +4964,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4964
4965
// Handle any errors
4966
if (err != null) {
4967
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'addmeshuser', responseid: command.responseid, result: err })); } catch (ex) { } }
4967
+ if (command.responseid != null) { obj.send({ action: 'addmeshuser', responseid: command.responseid, result: err }); }
4968
return;
4969
}
4970
@@ -5062,7 +5062,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5062
displayNotificationMessage('User' + ((unknownUsers.length > 1) ? 's' : '') + ' ' + EscapeHtml(unknownUsers.join(', ')) + ' not found.', "Device Group", 'ServerNotify', 5, (unknownUsers.length > 1) ? 16 : 15, [EscapeHtml(unknownUsers.join(', '))]);
5063
}
5064
5065
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'addmeshuser', responseid: command.responseid, result: msgs.join(', '), success: successCount, failed: failCount })); } catch (ex) { } }
5065
+ if (command.responseid != null) { obj.send({ action: 'addmeshuser', responseid: command.responseid, result: msgs.join(', '), success: successCount, failed: failCount }); }
5066
}
5067
5068
function serverCommandAddUser(command) {
@@ -5099,7 +5099,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5099
// Handle any errors
5100
if (err != null) {
5101
if (command.responseid != null) {
5102
- try { ws.send(JSON.stringify({ action: 'adduser', responseid: command.responseid, result: err, msgid: errid })); } catch (ex) { }
5102
+ obj.send({ action: 'adduser', responseid: command.responseid, result: err, msgid: errid });
5103
} else {
5104
// Send error back, user not found.
5105
displayNotificationMessage(err, "New Account", 'ServerNotify', null, 1, errid);
@@ -5113,7 +5113,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5113
// Account count exceed, do notification
5114
if (command.responseid != null) {
5115
// Respond privately if requested
5116
- try { ws.send(JSON.stringify({ action: 'adduser', responseid: command.responseid, result: 'maxUsersExceed' })); } catch (ex) { }
5116
+ obj.send({ action: 'adduser', responseid: command.responseid, result: 'maxUsersExceed' });
5117
} else {
5118
// Create the notification message
5119
var notification = { action: 'msg', type: 'notify', id: Math.random(), value: "Account limit reached.", title: "Server Limit", userid: user._id, username: user.name, domain: newuserdomain.id, titleid: 2, msgid: 10 };
@@ -5189,9 +5189,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5189
if (parent.parent.authlog) { parent.parent.authLog('https', 'User ' + user.name + ' created a user account ' + newuser.name); }
5190
5191
// OK Response
5192
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adduser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
5192
+ if (command.responseid != null) { obj.send({ action: 'adduser', responseid: command.responseid, result: 'ok' }); }
5193
} else {
5194
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adduser', responseid: command.responseid, result: 'passwordHashError' })); } catch (ex) { } }
5194
+ if (command.responseid != null) { obj.send({ action: 'adduser', responseid: command.responseid, result: 'passwordHashError' }); }
5195
}
5196
}, 0);
5197
}
@@ -5220,7 +5220,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5220
5221
// Handle any errors
5222
if (err != null) {
5223
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adduserbatch', responseid: command.responseid, result: err })); } catch (ex) { } }
5223
+ if (command.responseid != null) { obj.send({ action: 'adduserbatch', responseid: command.responseid, result: err }); }
5224
return;
5225
}
5226
@@ -5295,7 +5295,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5295
5296
// Handle any errors
5297
if (err != null) {
5298
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'addusertousergroup', responseid: command.responseid, result: err })); } catch (ex) { } }
5298
+ if (command.responseid != null) { obj.send({ action: 'addusertousergroup', responseid: command.responseid, result: err }); }
5299
return;
5300
}
5301
@@ -5349,7 +5349,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5349
}
5350
}
5351
5352
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'addusertousergroup', responseid: command.responseid, result: 'ok', added: addedCount, failed: failCount })); } catch (ex) { } }
5352
+ if (command.responseid != null) { obj.send({ action: 'addusertousergroup', responseid: command.responseid, result: 'ok', added: addedCount, failed: failCount }); }
5353
}
5354
5355
function serverCommandAgentDisconnect(command) {
@@ -5396,7 +5396,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5396
db.GetUserWithVerifiedEmail(domain.id, command.email, function (err, docs) {
5397
if ((docs != null) && (docs.length > 0)) {
5398
// Notify the duplicate email error
5399
- try { ws.send(JSON.stringify({ action: 'msg', type: 'notify', title: 'Account Settings', id: Math.random(), tag: 'ServerNotify', value: 'Failed to change email address, another account already using: ' + command.email + '.', titleid: 4, msgid: 13, args: [command.email] })); } catch (ex) { }
5399
+ obj.send({ action: 'msg', type: 'notify', title: 'Account Settings', id: Math.random(), tag: 'ServerNotify', value: 'Failed to change email address, another account already using: ' + command.email + '.', titleid: 4, msgid: 13, args: [command.email] });
5400
} else {
5401
// Update the user's email
5402
var oldemail = user.email;
@@ -5541,7 +5541,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5541
if (node.mtype == 3) { cookieContent.lc = 1; command.localRelay = true; } // Indicate this is for a local connection
5542
command.cookie = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
5543
command.trustedCert = parent.isTrustedCert(domain);
5544
- try { ws.send(JSON.stringify(command)); } catch (ex) { }
5544
+ obj.send(command);
5545
});
5546
}
5547
@@ -5550,11 +5550,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5550
5551
// Get the node and the rights for this node
5552
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5553
- if ((visible == false) || ((rights & MESHRIGHT_DEVICEDETAILS) == 0)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; }
5553
+ if ((visible == false) || ((rights & MESHRIGHT_DEVICEDETAILS) == 0)) { obj.send({ action: 'getnetworkinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' }); return; }
5554
5555
// Get network information about this node
5556
db.Get('if' + node._id, function (err, netinfos) {
5557
- if ((netinfos == null) || (netinfos.length != 1)) { try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null })); } catch (ex) { } return; }
5557
+ if ((netinfos == null) || (netinfos.length != 1)) { obj.send({ action: 'getnetworkinfo', nodeid: node._id, netif: null, netif2: null }); return; }
5558
var netinfo = netinfos[0];
5559
5560
// Unescape any field names that have special characters if needed
@@ -5565,7 +5565,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5565
}
5566
}
5567
5568
- try { ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 })); } catch (ex) { }
5568
+ obj.send({ action: 'getnetworkinfo', nodeid: node._id, updateTime: netinfo.updateTime, netif: netinfo.netif, netif2: netinfo.netif2 });
5569
});
5570
});
5571
}
@@ -5575,7 +5575,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5575
5576
// Get the node and the rights for this node
5577
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5578
- if ((visible == false) || ((rights & MESHRIGHT_DEVICEDETAILS) == 0)) { try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; }
5578
+ if ((visible == false) || ((rights & MESHRIGHT_DEVICEDETAILS) == 0)) { obj.send({ action: 'getsysinfo', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' }); return; }
5579
// Query the database system information
5580
db.Get('si' + command.nodeid, function (err, docs) {
5581
if ((docs != null) && (docs.length > 0)) {
@@ -5587,9 +5587,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5587
delete doc.domain;
5588
delete doc._id;
5589
if (command.nodeinfo === true) { doc.node = node; doc.rights = rights; }
5590
- try { ws.send(JSON.stringify(doc)); } catch (ex) { }
5590
+ obj.send(doc);
5591
} else {
5592
- try { ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: node._id, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { }
5592
+ obj.send({ action: 'getsysinfo', nodeid: node._id, tag: command.tag, noinfo: true, result: 'Invalid device id' });
5593
}
5594
});
5595
});
@@ -5635,14 +5635,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5635
5636
// Get the node and the rights for this node
5637
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5638
- if (visible == false) { try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' })); } catch (ex) { } return; }
5638
+ if (visible == false) { obj.send({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'Invalid device id' }); return; }
5639
5640
// Query the database for the last time this node connected
5641
db.Get('lc' + command.nodeid, function (err, docs) {
5642
if ((docs != null) && (docs.length > 0)) {
5643
- try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, time: docs[0].time, addr: docs[0].addr })); } catch (ex) { }
5643
+ obj.send({ action: 'lastconnect', nodeid: command.nodeid, time: docs[0].time, addr: docs[0].addr });
5644
} else {
5645
- try { ws.send(JSON.stringify({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'No data' })); } catch (ex) { }
5645
+ obj.send({ action: 'lastconnect', nodeid: command.nodeid, tag: command.tag, noinfo: true, result: 'No data' });
5646
}
5647
});
5648
});
@@ -5663,7 +5663,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5663
if (docs == null) return;
5664
const response = {};
5665
for (var j in docs) { response[docs[j]._id.substring(2)] = docs[j].time; }
5666
- try { ws.send(JSON.stringify({ action: 'lastconnects', lastconnects: response, tag: command.tag })); } catch (ex) { }
5666
+ obj.send({ action: 'lastconnects', lastconnects: response, tag: command.tag });
5667
});
5668
});
5669
}
@@ -5671,13 +5671,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5671
function serverCommandLoginCookie(command) {
5672
// If allowed, return a login cookie
5673
if (parent.parent.config.settings.allowlogintoken === true) {
5674
- try { ws.send(JSON.stringify({ action: 'logincookie', cookie: parent.parent.encodeCookie({ u: user._id, a: 3 }, parent.parent.loginCookieEncryptionKey) })); } catch (ex) { }
5674
+ obj.send({ action: 'logincookie', cookie: parent.parent.encodeCookie({ u: user._id, a: 3 }, parent.parent.loginCookieEncryptionKey) });
5675
}
5676
}
5677
5678
function serverCommandMeshes(command) {
5679
// Request a list of all meshes this user as rights to
5680
- try { ws.send(JSON.stringify({ action: 'meshes', meshes: parent.GetAllMeshWithRights(user).map(parent.CloneSafeMesh), tag: command.tag })); } catch (ex) { }
5680
+ obj.send({ action: 'meshes', meshes: parent.GetAllMeshWithRights(user).map(parent.CloneSafeMesh), tag: command.tag });
5681
}
5682
5683
function serverCommandPing(command) { try { ws.send('{action:"pong"}'); } catch (ex) { } }
@@ -5708,11 +5708,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5708
}
5709
previousPower = doc.power;
5710
}
5711
- try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag })); } catch (ex) { }
5711
+ obj.send({ action: 'powertimeline', nodeid: node._id, timeline: timeline, tag: command.tag });
5712
} else {
5713
// No records found, send current state if we have it
5714
var state = parent.parent.GetConnectivityState(command.nodeid);
5715
- if (state != null) { try { ws.send(JSON.stringify({ action: 'powertimeline', nodeid: node._id, timeline: [state.powerState, Date.now(), state.powerState], tag: command.tag })); } catch (ex) { } }
5715
+ if (state != null) { obj.send({ action: 'powertimeline', nodeid: node._id, timeline: [state.powerState, Date.now(), state.powerState], tag: command.tag }); }
5716
}
5717
});
5718
});
@@ -5756,7 +5756,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5756
5757
// Handle any errors
5758
if (err != null) {
5759
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removeuserfromusergroup', responseid: command.responseid, result: err })); } catch (ex) { } }
5759
+ if (command.responseid != null) { obj.send({ action: 'removeuserfromusergroup', responseid: command.responseid, result: err }); }
5760
return;
5761
}
5762
@@ -5797,7 +5797,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5797
}
5798
}
5799
5800
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removeuserfromusergroup', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
5800
+ if (command.responseid != null) { obj.send({ action: 'removeuserfromusergroup', responseid: command.responseid, result: 'ok' }); }
5801
}
5802
5803
function serverCommandReport(command) {
@@ -5840,13 +5840,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5840
} else { cmdData.result = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.'; }
5841
5842
// Send back the command result
5843
- if (cmdData.result != '') { try { ws.send(JSON.stringify({ action: 'serverconsole', value: cmdData.result, tag: command.tag })); } catch (ex) { } }
5843
+ if (cmdData.result != '') { obj.send({ action: 'serverconsole', value: cmdData.result, tag: command.tag }); }
5844
}
5845
5846
function serverCommandServerErrors(command) {
5847
// Load the server error log
5848
if (userHasSiteUpdate() && domainHasMyServerErrorLog())
5849
- fs.readFile(parent.parent.getConfigFilePath('mesherrors.txt'), 'utf8', function (err, data) { try { ws.send(JSON.stringify({ action: 'servererrors', data: data })); } catch (ex) { } });
5849
+ fs.readFile(parent.parent.getConfigFilePath('mesherrors.txt'), 'utf8', function (err, data) { obj.send({ action: 'servererrors', data: data }); });
5850
}
5851
5852
function serverCommandServerStats(command) {
@@ -5871,7 +5871,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5871
if ((user.siteadmin & 21) == 0) return; // Only site administrators with "site backup" or "site restore" or "site update" permissions can use this.
5872
if (common.validateInt(command.hours, 0, 24 * 30) == false) return;
5873
db.GetServerStats(command.hours, function (err, docs) {
5874
- if (err == null) { try { ws.send(JSON.stringify({ action: 'servertimelinestats', events: docs })); } catch (ex) { } }
5874
+ if (err == null) { obj.send({ action: 'servertimelinestats', events: docs }); }
5875
});
5876
}
5877
@@ -5890,7 +5890,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5890
5891
// Check the server version
5892
if (userHasSiteUpdate() && domainHasMyServerUpgrade())
5893
- parent.parent.getServerTags(function (tags, err) { try { ws.send(JSON.stringify({ action: 'serverversion', tags: tags })); } catch (ex) { } });
5893
+ parent.parent.getServerTags(function (tags, err) { obj.send({ action: 'serverversion', tags: tags }); });
5894
}
5895
5896
function serverCommandSetClip(command) {
@@ -5932,11 +5932,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5932
function serverCommandTrafficDelta(command) {
5933
const stats = parent.getTrafficDelta(obj.trafficStats);
5934
obj.trafficStats = stats.current;
5935
- try { ws.send(JSON.stringify({ action: 'trafficdelta', delta: stats.delta })); } catch (ex) { }
5935
+ obj.send({ action: 'trafficdelta', delta: stats.delta });
5936
}
5937
5938
function serverCommandTrafficStats(command) {
5939
- try { ws.send(JSON.stringify({ action: 'trafficstats', stats: parent.getTrafficStats() })); } catch (ex) { }
5939
+ obj.send({ action: 'trafficstats', stats: parent.getTrafficStats() });
5940
}
5941
5942
function serverCommandUpdateAgents(command) {
@@ -5988,7 +5988,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5988
5989
function serverCommandUsers(command) {
5990
// Request a list of all users
5991
- if ((user.siteadmin & 2) == 0) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'users', responseid: command.responseid, result: 'Access denied' })); } catch (ex) { } } return; }
5991
+ if ((user.siteadmin & 2) == 0) { if (command.responseid != null) { obj.send({ action: 'users', responseid: command.responseid, result: 'Access denied' }); } return; }
5992
var docs = [];
5993
for (i in parent.users) {
5994
if (((obj.crossDomain === true) || (parent.users[i].domain == domain.id)) && (parent.users[i].name != '~')) {
@@ -5998,7 +5998,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5998
}
5999
}
6000
}
6001
- try { ws.send(JSON.stringify({ action: 'users', users: docs, tag: command.tag })); } catch (ex) { }
6001
+ obj.send({ action: 'users', users: docs, tag: command.tag });
6002
}
6003
6004
function serverCommandVerifyEmail(command) {
@@ -6037,7 +6037,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6037
});
6038
}
6039
6040
-
6040
function serverUserCommandHelp(cmdData) {
6041
var fin = '', f = '', availcommands = [];
6042
for (var i in serverUserCommands) { availcommands.push(i); }