Server peering cleanup.
Ylian Saint-Hilaire committed
Sep 24, 2023 at 14:59 UTC
b59b76a4f2c6219111ca814b0d32313d9d8f1e7a
1 file changed
+15
-17
multiserver.js
+15
-17
@@ -139,7 +139,7 @@ module.exports.CreateMultiServer = function (parent, args) {
139
140
// Send information about our server to the peer
141
if (obj.connectionState == 15) {
142
- obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 }));
142
+ obj.send({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 });
143
for (var i in obj.pendingData) { processServerData(obj.pendingData[i]); } // Process any pending data
144
obj.pendingData = [];
145
}
@@ -150,7 +150,7 @@ module.exports.CreateMultiServer = function (parent, args) {
150
// Peer server confirmed authentication, we are allowed to send commands to the server
151
obj.connectionState |= 8;
152
if (obj.connectionState == 15) {
153
- obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 }));
153
+ obj.send({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 });
154
for (var i in obj.pendingData) { processServerData(obj.pendingData[i]); } // Process any pending data
155
obj.pendingData = [];
156
}
@@ -187,9 +187,9 @@ module.exports.CreateMultiServer = function (parent, args) {
187
obj.send = function (msg) {
188
try {
189
if (obj.ws == null || obj.connectionState != 15) { return; }
190
- if (typeof msg == 'object') { obj.ws.send(JSON.stringify(msg)); return; }
190
if (typeof msg == 'string') { obj.ws.send(msg); return; }
192
- } catch (e) { }
191
+ if (typeof msg == 'object') { obj.ws.send(JSON.stringify(msg)); return; }
192
+ } catch (ex) { }
193
};
194
195
// Process incoming peer server JSON data
@@ -244,12 +244,11 @@ module.exports.CreateMultiServer = function (parent, args) {
244
obj.parent.parent.debug('peer', 'InPeer: Connected (' + obj.remoteaddr + ')');
245
246
// Send a message to the peer server
247
- obj.send = function (data) {
247
+ obj.send = function (msg) {
248
try {
249
- if (typeof data == 'string') { obj.ws.send(Buffer.from(data, 'binary')); return; }
250
- if (typeof data == 'object') { obj.ws.send(JSON.stringify(data)); return; }
251
- obj.ws.send(data);
252
- } catch (e) { }
249
+ if (typeof msg == 'string') { obj.ws.send(msg); return; }
250
+ if (typeof msg == 'object') { obj.ws.send(JSON.stringify(msg)); return; }
251
+ } catch (ex) { }
252
};
253
254
// Disconnect this server
@@ -281,7 +280,7 @@ module.exports.CreateMultiServer = function (parent, args) {
280
// Perform the hash signature using the server agent certificate
281
obj.parent.parent.certificateOperations.acceleratorPerformSignature(0, msg.substring(2) + obj.nonce, null, function (tag, signature) {
282
// Send back our certificate + signature
284
- obj.send(obj.common.ShortToStr(2) + obj.common.ShortToStr(obj.agentCertificateAsn1.length) + obj.agentCertificateAsn1 + signature); // Command 2, certificate + signature
283
+ obj.ws.send(Buffer.from(obj.common.ShortToStr(2) + obj.common.ShortToStr(obj.agentCertificateAsn1.length) + obj.agentCertificateAsn1 + signature, 'binary')); // Command 2, certificate + signature
284
});
285
286
// Check the peer server signature if we can
@@ -326,13 +325,13 @@ module.exports.CreateMultiServer = function (parent, args) {
325
// Start authenticate the peer server by sending a auth nonce & server TLS cert hash.
326
// Send 384 bits SHA382 hash of TLS cert public key + 384 bits nonce
327
obj.nonce = obj.crypto.randomBytes(48).toString('binary');
329
- obj.send(obj.common.ShortToStr(1) + obj.webCertificateHash + obj.nonce); // Command 1, hash + nonce
328
+ obj.ws.send(Buffer.from(obj.common.ShortToStr(1) + obj.webCertificateHash + obj.nonce, 'binary')); // Command 1, hash + nonce
329
330
// Once we get all the information about an peer server, run this to hook everything up to the server
331
function completePeerServerConnection() {
332
if (obj.authenticated != 1) return;
334
- obj.send(obj.common.ShortToStr(4));
335
- obj.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 }));
333
+ obj.ws.send(Buffer.from(obj.common.ShortToStr(4), 'binary'));
334
+ obj.send({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificateHashBase64 });
335
obj.authenticated = 2;
336
337
// Process any pending data that was received before peer authentication
@@ -405,8 +404,7 @@ module.exports.CreateMultiServer = function (parent, args) {
404
405
// Dispatch an event to all other MeshCentral2 peer servers
406
obj.DispatchEvent = function (ids, source, event) {
408
- var busmsg = JSON.stringify({ action: 'bus', ids: ids, event: event });
409
- for (var serverid in obj.peerServers) { obj.peerServers[serverid].send(busmsg); }
407
+ for (var serverid in obj.peerServers) { obj.peerServers[serverid].send({ action: 'bus', ids: ids, event: event }); }
408
};
409
410
// Dispatch a message to other MeshCentral2 peer servers
@@ -437,10 +435,10 @@ module.exports.CreateMultiServer = function (parent, args) {
435
obj.peerServers[peerServerId] = server;
436
437
// Send the list of connections to the peer
440
- server.send(JSON.stringify({ action: 'connectivityTable', connectivityTable: obj.parent.peerConnectivityByNode[obj.parent.serverId] }));
438
+ server.send({ action: 'connectivityTable', connectivityTable: obj.parent.peerConnectivityByNode[obj.parent.serverId] });
439
440
// Send a list of user sessions to the peer
443
- server.send(JSON.stringify({ action: 'sessionsTable', sessionsTable: Object.keys(obj.parent.webserver.wssessions2) }));
441
+ server.send({ action: 'sessionsTable', sessionsTable: Object.keys(obj.parent.webserver.wssessions2) });
442
};
443
444
// We disconnected to a peer server, clean up everything