Added AgentUpdateBlockSize to settings section of config.json
Ylian Saint-Hilaire committed
Oct 28, 2019 at 16:52 UTC
545520563a65c600150c1c934f104c40bc42a551
3 files changed
+17
-16
meshagent.js
+14
-15
@@ -18,7 +18,6 @@
18
module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
19
const forge = parent.parent.certificateOperations.forge;
20
const common = parent.parent.common;
21
- const agentUpdateBlockSize = 65531;
21
parent.agentStats.createMeshAgentCount++;
22
23
var obj = {};
@@ -110,7 +109,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
109
if (obj.connectTime) { delete obj.connectTime; }
110
if (obj.agentInfo) { delete obj.agentInfo; }
111
if (obj.agentExeInfo) { delete obj.agentExeInfo; }
113
- ws.removeAllListeners(["message", "close", "error"]);
112
+ ws.removeAllListeners(['message', 'close', 'error']);
113
};
114
115
// When data is received from the mesh agent web socket
@@ -123,7 +122,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
122
if (msg.length < 2) return;
123
const cmdid = common.ReadShort(msg, 0);
124
if (cmdid == 11) { // MeshCommand_CoreModuleHash
126
- if (msg.length == 4) { ChangeAgentCoreInfo({ "caps": 0 }); } // If the agent indicated that no core is running, clear the core information string.
125
+ if (msg.length == 4) { ChangeAgentCoreInfo({ 'caps': 0 }); } // If the agent indicated that no core is running, clear the core information string.
126
// Mesh core hash, sent by agent with the hash of the current mesh core.
127
128
// If we are performing an agent update, don't update the core.
@@ -171,7 +170,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
170
// Clear the core
171
obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0)); // MeshCommand_CoreModule, ask mesh agent to clear the core
172
parent.agentStats.clearingCoreCount++;
174
- parent.parent.debug('agent', 'Clearing core');
173
+ parent.parent.debug('agent', "Clearing core");
174
} else {
175
// Update new core
176
//obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0) + meshcorehash + parent.parent.defaultMeshCores[corename]); // MeshCommand_CoreModule, start core update
@@ -185,7 +184,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
184
delete obj.agentCoreUpdatePending;
185
obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0) + argument.hash + argument.core, function () { parent.parent.taskLimiter.completed(taskid); }); // MeshCommand_CoreModule, start core update
186
parent.agentStats.updatingCoreCount++;
188
- parent.parent.debug('agent', 'Updating core ' + argument.name);
187
+ parent.parent.debug('agent', "Updating core " + argument.name);
188
agentCoreIsStable();
189
} else {
190
// This agent is probably disconnected, nothing to do.
@@ -233,14 +232,14 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
232
// Mesh agent update required, do it using task limiter so not to flood the network. Medium priority task.
233
parent.parent.taskLimiter.launch(function (argument, taskid, taskLimiterQueue) {
234
if (obj.authenticated != 2) { parent.parent.taskLimiter.completed(taskid); return; } // If agent disconnection, complete and exit now.
236
- if (obj.nodeid != null) { parent.parent.debug('agent', 'Agent update required, NodeID=0x' + obj.nodeid.substring(0, 16) + ', ' + obj.agentExeInfo.desc); }
235
+ if (obj.nodeid != null) { parent.parent.debug('agent', "Agent update required, NodeID=0x" + obj.nodeid.substring(0, 16) + ', ' + obj.agentExeInfo.desc); }
236
parent.agentStats.agentBinaryUpdate++;
237
if (obj.agentExeInfo.data == null) {
238
// Read the agent from disk
239
parent.fs.open(obj.agentExeInfo.path, 'r', function (err, fd) {
240
if (obj.agentExeInfo == null) return; // Agent disconnected during this call.
241
if (err) { parent.parent.debug('agentupdate', "ERROR: " + err); return console.error(err); }
243
- obj.agentUpdate = { ptr: 0, buf: Buffer.alloc(agentUpdateBlockSize + 4), fd: fd, taskid: taskid };
242
+ obj.agentUpdate = { ptr: 0, buf: Buffer.alloc(parent.parent.agentUpdateBlockSize + 4), fd: fd, taskid: taskid };
243
244
// MeshCommand_CoreModule, ask mesh agent to clear the core.
245
// The new core will only be sent after the agent updates.
@@ -255,7 +254,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
254
obj.agentUpdate.buf[1] = 14;
255
obj.agentUpdate.buf[2] = 0;
256
obj.agentUpdate.buf[3] = 1;
258
- parent.fs.read(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr, function (err, bytesRead, buffer) {
257
+ parent.fs.read(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, parent.parent.agentUpdateBlockSize, obj.agentUpdate.ptr, function (err, bytesRead, buffer) {
258
if ((err != null) || (bytesRead == 0)) {
259
// Error reading the agent file, stop here.
260
try { parent.fs.close(obj.agentUpdate.fd); } catch (ex) { }
@@ -273,7 +272,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
272
});
273
} else {
274
// Send the agent from RAM
276
- obj.agentUpdate = { ptr: 0, buf: Buffer.alloc(agentUpdateBlockSize + 4), taskid: taskid };
275
+ obj.agentUpdate = { ptr: 0, buf: Buffer.alloc(parent.parent.agentUpdateBlockSize + 4), taskid: taskid };
276
277
// MeshCommand_CoreModule, ask mesh agent to clear the core.
278
// The new core will only be sent after the agent updates.
@@ -288,7 +287,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
287
obj.agentUpdate.buf[2] = 0;
288
obj.agentUpdate.buf[3] = 1;
289
291
- const len = Math.min(agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
290
+ const len = Math.min(parent.parent.agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
291
if (len > 0) {
292
// Send the first block
293
obj.agentExeInfo.data.copy(obj.agentUpdate.buf, 4, obj.agentUpdate.ptr, obj.agentUpdate.ptr + len);
@@ -319,7 +318,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
318
if (status == 1) {
319
if (obj.agentExeInfo.data == null) {
320
// Read the agent from disk
322
- parent.fs.read(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr, function (err, bytesRead, buffer) {
321
+ parent.fs.read(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, parent.parent.agentUpdateBlockSize, obj.agentUpdate.ptr, function (err, bytesRead, buffer) {
322
if ((obj.agentExeInfo == null) || (obj.agentUpdate == null)) return; // Agent disconnected during this async call.
323
if ((err != null) || (bytesRead < 0)) {
324
// Error reading the agent file, stop here.
@@ -332,8 +331,8 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
331
// Send the next block to the agent
332
parent.parent.debug('agentupdate', "Sending disk agent #" + obj.agentExeInfo.id + " block, ptr=" + obj.agentUpdate.ptr + ", len=" + bytesRead + ".");
333
obj.agentUpdate.ptr += bytesRead;
335
- if (bytesRead == agentUpdateBlockSize) { obj.sendBinary(obj.agentUpdate.buf); } else { obj.sendBinary(obj.agentUpdate.buf.slice(0, bytesRead + 4)); } // Command 14, mesh agent next data block
336
- if ((bytesRead < agentUpdateBlockSize) || (obj.agentUpdate.ptr == obj.agentExeInfo.size)) {
334
+ if (bytesRead == parent.parent.agentUpdateBlockSize) { obj.sendBinary(obj.agentUpdate.buf); } else { obj.sendBinary(obj.agentUpdate.buf.slice(0, bytesRead + 4)); } // Command 14, mesh agent next data block
335
+ if ((bytesRead < parent.parent.agentUpdateBlockSize) || (obj.agentUpdate.ptr == obj.agentExeInfo.size)) {
336
parent.parent.debug('agentupdate', "Completed agent #" + obj.agentExeInfo.id + " update from disk, ptr=" + obj.agentUpdate.ptr + ".");
337
obj.sendBinary(common.ShortToStr(13) + common.ShortToStr(0) + obj.agentExeInfo.hash); // Command 13, end mesh agent download, send agent SHA384 hash
338
try { parent.fs.close(obj.agentUpdate.fd); } catch (ex) { }
@@ -345,10 +344,10 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
344
});
345
} else {
346
// Send the agent from RAM
348
- const len = Math.min(agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
347
+ const len = Math.min(parent.parent.agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
348
if (len > 0) {
349
obj.agentExeInfo.data.copy(obj.agentUpdate.buf, 4, obj.agentUpdate.ptr, obj.agentUpdate.ptr + len);
351
- if (len == agentUpdateBlockSize) { obj.sendBinary(obj.agentUpdate.buf); } else { obj.sendBinary(obj.agentUpdate.buf.slice(0, len + 4)); } // Command 14, mesh agent next data block
350
+ if (len == parent.parent.agentUpdateBlockSize) { obj.sendBinary(obj.agentUpdate.buf); } else { obj.sendBinary(obj.agentUpdate.buf.slice(0, len + 4)); } // Command 14, mesh agent next data block
351
parent.parent.debug('agentupdate', "Sending RAM agent #" + obj.agentExeInfo.id + " block, ptr=" + obj.agentUpdate.ptr + ", len=" + len + ".");
352
obj.agentUpdate.ptr += len;
353
}
meshcentral.js
+2
@@ -67,6 +67,7 @@ function CreateMeshCentralServer(config, args) {
67
obj.serverSelfWriteAllowed = true;
68
obj.serverStatsCounter = Math.floor(Math.random() * 1000);
69
obj.taskLimiter = obj.common.createTaskLimiterQueue(50, 20, 60); // (maxTasks, maxTaskTime, cleaningInterval) This is a task limiter queue to smooth out server work.
70
+ obj.agentUpdateBlockSize = 65531; // MeshAgent update block size
71
try { obj.currentVer = JSON.parse(obj.fs.readFileSync(obj.path.join(__dirname, 'package.json'), 'utf8')).version; } catch (e) { } // Fetch server version
72
73
// Setup the default configuration and files paths
@@ -367,6 +368,7 @@ function CreateMeshCentralServer(config, args) {
368
if (typeof obj.args.agentallowedip == 'string') { if (obj.args.agentallowedip == '') { obj.args.agentallowedip = null; } else { obj.args.agentallowedip = obj.args.agentallowedip.split(','); } }
369
if (typeof obj.args.agentblockedip == 'string') { if (obj.args.agentblockedip == '') { obj.args.agentblockedip = null; } else { obj.args.agentblockedip = obj.args.agentblockedip.split(','); } }
370
if (typeof obj.args.swarmallowedip == 'string') { if (obj.args.swarmallowedip == '') { obj.args.swarmallowedip = null; } else { obj.args.swarmallowedip = obj.args.swarmallowedip.split(','); } }
371
+ if ((typeof obj.args.agentupdateblocksize == 'number') && (obj.args.agentupdateblocksize >= 1024) && (obj.args.agentupdateblocksize <= 65531)) { obj.agentUpdateBlockSize = obj.args.agentupdateblocksize; }
372
373
// Local console tracing
374
if (typeof obj.args.debug == 'string') { obj.debugSources = obj.args.debug.toLowerCase().split(','); }
package.json
+1
-1
@@ -1,6 +1,6 @@
1
{
2
"name": "meshcentral",
3
- "version": "0.4.3-i",
3
+ "version": "0.4.3-j",
4
"keywords": [
5
"Remote Management",
6
"Intel AMT",