Mesh agents binaries are now loaded in server RAM by default.

Ylian Saint-Hilaire committed Feb 14, 2019 at 22:16 UTC 2cab56f4adfea27ed669461f382e8505fa9e0e4c
3 files changed +82 -31
meshagent.js
+79 -30
@@ -63,7 +63,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
63 // Other clean up may be needed here
64 if (obj.unauth) { delete obj.unauth; }
65 if (obj.agentUpdate != null) {
66 - obj.fs.close(obj.agentUpdate.fd);
66 + if (obj.fs) { try { obj.fs.close(obj.agentUpdate.fd); } catch (ex) { } obj.fs = null; }
67 obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
68 obj.agentUpdate = null;
69 }
@@ -172,16 +172,48 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
172 // Mesh agent update required, do it using task limiter so not to flood the network. Medium priority task.
173 obj.parent.parent.taskLimiter.launch(function (argument, taskid, taskLimiterQueue) {
174 if (obj.nodeid != null) { obj.parent.parent.debug(1, 'Agent update required, NodeID=0x' + obj.nodeid.substring(0, 16) + ', ' + obj.agentExeInfo.desc); }
175 - obj.fs.open(obj.agentExeInfo.path, 'r', function (err, fd) {
176 - if (err) { return console.error(err); }
177 - obj.agentUpdate = { oldHash: agenthash, ptr: 0, buf: Buffer.alloc(agentUpdateBlockSize + 4), fd: fd, taskid: taskid };
175 + if (obj.agentExeInfo.data == null) {
176 + // Read the agent from disk
177 + obj.fs.open(obj.agentExeInfo.path, 'r', function (err, fd) {
178 + if (err) { return console.error(err); }
179 + obj.agentUpdate = { oldHash: agenthash, ptr: 0, buf: Buffer.alloc(agentUpdateBlockSize + 4), fd: fd, taskid: taskid };
180 +
181 + // MeshCommand_CoreModule, ask mesh agent to clear the core.
182 + // The new core will only be sent after the agent updates.
183 + obj.send(obj.common.ShortToStr(10) + obj.common.ShortToStr(0));
184 +
185 + // We got the agent file open on the server side, tell the agent we are sending an update starting with the SHA384 hash of the result
186 + //console.log("Agent update file open.");
187 + obj.send(obj.common.ShortToStr(13) + obj.common.ShortToStr(0)); // Command 13, start mesh agent download
188 +
189 + // Send the first mesh agent update data block
190 + obj.agentUpdate.buf[0] = 0;
191 + obj.agentUpdate.buf[1] = 14;
192 + obj.agentUpdate.buf[2] = 0;
193 + obj.agentUpdate.buf[3] = 1;
194 + var len = -1;
195 + try { len = obj.fs.readSync(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr); } catch (e) { }
196 + if (len == -1) {
197 + // Error reading the agent file, stop here.
198 + obj.fs.close(obj.agentUpdate.fd);
199 + obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
200 + obj.agentUpdate = null;
201 + } else {
202 + // Send the first block to the agent
203 + obj.agentUpdate.ptr += len;
204 + //console.log("Agent update send first block: " + len);
205 + obj.send(obj.agentUpdate.buf); // Command 14, mesh agent first data block
206 + }
207 + });
208 + } else {
209 + // Send the agent from RAM
210 + obj.agentUpdate = { oldHash: agenthash, ptr: 0, buf: Buffer.alloc(agentUpdateBlockSize + 4), taskid: taskid };
211
212 // MeshCommand_CoreModule, ask mesh agent to clear the core.
213 // The new core will only be sent after the agent updates.
214 obj.send(obj.common.ShortToStr(10) + obj.common.ShortToStr(0));
215
216 // We got the agent file open on the server side, tell the agent we are sending an update starting with the SHA384 hash of the result
184 - //console.log("Agent update file open.");
217 obj.send(obj.common.ShortToStr(13) + obj.common.ShortToStr(0)); // Command 13, start mesh agent download
218
219 // Send the first mesh agent update data block
@@ -189,20 +221,19 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
221 obj.agentUpdate.buf[1] = 14;
222 obj.agentUpdate.buf[2] = 0;
223 obj.agentUpdate.buf[3] = 1;
192 - var len = -1;
193 - try { len = obj.fs.readSync(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr); } catch (e) { }
194 - if (len == -1) {
195 - // Error reading the agent file, stop here.
196 - obj.fs.close(obj.agentUpdate.fd);
197 - obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
198 - obj.agentUpdate = null;
199 - } else {
200 - // Send the first block to the agent
224 +
225 + var len = Math.min(agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
226 + if (len > 0) {
227 + // Send the first block
228 + obj.agentExeInfo.data.copy(obj.agentUpdate.buf, 4, obj.agentUpdate.ptr, obj.agentUpdate.ptr + len);
229 obj.agentUpdate.ptr += len;
202 - //console.log("Agent update send first block: " + len);
230 obj.send(obj.agentUpdate.buf); // Command 14, mesh agent first data block
231 + } else {
232 + // Error
233 + obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
234 + obj.agentUpdate = null;
235 }
205 - });
236 + }
237 }, null, 1);
238
239 } else {
@@ -217,23 +248,41 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
248 if ((msg.length == 4) && (obj.agentUpdate != null)) {
249 var status = obj.common.ReadShort(msg, 2);
250 if (status == 1) {
220 - var len = -1;
221 - try { len = obj.fs.readSync(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr); } catch (e) { }
222 - if (len == -1) {
223 - // Error reading the agent file, stop here.
224 - obj.fs.close(obj.agentUpdate.fd);
225 - obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
226 - obj.agentUpdate = null;
251 + if (obj.agentExeInfo.data == null) {
252 + // Read the agent from disk
253 + var len = -1;
254 + try { len = obj.fs.readSync(obj.agentUpdate.fd, obj.agentUpdate.buf, 4, agentUpdateBlockSize, obj.agentUpdate.ptr); } catch (e) { }
255 + if (len == -1) {
256 + // Error reading the agent file, stop here.
257 + obj.fs.close(obj.agentUpdate.fd);
258 + obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
259 + obj.agentUpdate = null;
260 + } else {
261 + // Send the next block to the agent
262 + obj.agentUpdate.ptr += len;
263 + //console.log("Agent update send next block", obj.agentUpdate.ptr, len);
264 + if (len == agentUpdateBlockSize) { obj.send(obj.agentUpdate.buf); } else { obj.send(obj.agentUpdate.buf.slice(0, len + 4)); } // Command 14, mesh agent next data block
265 +
266 + if (len < agentUpdateBlockSize) {
267 + //console.log("Agent update sent from disk.");
268 + obj.send(obj.common.ShortToStr(13) + obj.common.ShortToStr(0) + obj.common.hex2rstr(obj.agentExeInfo.hash)); // Command 13, end mesh agent download, send agent SHA384 hash
269 + obj.fs.close(obj.agentUpdate.fd);
270 + obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
271 + obj.agentUpdate = null;
272 + }
273 + }
274 } else {
228 - // Send the next block to the agent
229 - obj.agentUpdate.ptr += len;
230 - //console.log("Agent update send next block", obj.agentUpdate.ptr, len);
231 - if (len == agentUpdateBlockSize) { obj.send(obj.agentUpdate.buf); } else { obj.send(obj.agentUpdate.buf.slice(0, len + 4)); } // Command 14, mesh agent next data block
275 + // Send the agent from RAM
276 + var len = Math.min(agentUpdateBlockSize, obj.agentExeInfo.data.length - obj.agentUpdate.ptr);
277 + if (len > 0) {
278 + obj.agentExeInfo.data.copy(obj.agentUpdate.buf, 4, obj.agentUpdate.ptr, obj.agentUpdate.ptr + len);
279 + if (len == agentUpdateBlockSize) { obj.send(obj.agentUpdate.buf); } else { obj.send(obj.agentUpdate.buf.slice(0, len + 4)); } // Command 14, mesh agent next data block
280 + obj.agentUpdate.ptr += len;
281 + }
282
233 - if (len < agentUpdateBlockSize) {
234 - //console.log("Agent update sent");
283 + if (obj.agentUpdate.ptr == obj.agentExeInfo.data.length) {
284 + //console.log("Agent update sent from RAM.");
285 obj.send(obj.common.ShortToStr(13) + obj.common.ShortToStr(0) + obj.common.hex2rstr(obj.agentExeInfo.hash)); // Command 13, end mesh agent download, send agent SHA384 hash
236 - obj.fs.close(obj.agentUpdate.fd);
286 obj.parent.parent.taskLimiter.completed(obj.agentUpdate.taskid); // Indicate this task complete
287 obj.agentUpdate = null;
288 }
meshcentral.js
+2
@@ -1265,10 +1265,12 @@ function CreateMeshCentralServer(config, args) {
1265 obj.meshAgentBinaries[archid].path = agentpath;
1266 obj.meshAgentBinaries[archid].url = ((obj.args.notls == true) ? 'http://' : 'https://') + obj.certificates.CommonName + ':' + obj.args.port + '/meshagents?id=' + archid;
1267 obj.meshAgentBinaries[archid].size = stats.size;
1268 + if (!obj.args.agentsondisk) { obj.meshAgentBinaries[archid].data = obj.fs.readFileSync(agentpath); }
1269 // If this is a windows binary, pull binary information
1270 if (obj.meshAgentsArchitectureNumbers[archid].platform == 'win32') {
1271 try { obj.meshAgentBinaries[archid].pe = obj.exeHandler.parseWindowsExecutable(agentpath); } catch (e) { }
1272 }
1273 +
1274 // Hash the binary
1275 var hashStream = obj.crypto.createHash('sha384');
1276 hashStream.archid = archid;
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.2.8-k",
3 + "version": "0.2.8-l",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",