From cd85c66a1d3365582cb71acabdbc0d30adedcdde Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Thu, 13 Jan 2022 14:34:39 -0800 Subject: [PATCH] Fixed utf8 in meshcore. --- meshagent.js | 2 +- meshcentral.js | 9 ++++++--- webserver.js | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/meshagent.js b/meshagent.js index 72e0f9fa..28e43a68 100644 --- a/meshagent.js +++ b/meshagent.js @@ -224,7 +224,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { if (obj.authenticated == 2) { // Send the updated core. delete obj.agentCoreUpdatePending; - obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0) + argument.hash + argument.core, function () { parent.parent.taskLimiter.completed(taskid); }); // MeshCommand_CoreModule, start core update + obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0) + argument.hash + argument.core.toString('binary'), function () { parent.parent.taskLimiter.completed(taskid); }); // MeshCommand_CoreModule, start core update parent.agentStats.updatingCoreCount++; parent.parent.debug('agent', "Updating core " + argument.name); } else { diff --git a/meshcentral.js b/meshcentral.js index 0bfd6f68..21cc5ea7 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -2479,7 +2479,8 @@ function CreateMeshCentralServer(config, args) { if (modulesDir[i].toLowerCase().endsWith('.json')) { var moduleName = modulesDir[i].substring(0, modulesDir[i].length - 5); if (moduleName.endsWith('.min')) { moduleName = moduleName.substring(0, moduleName.length - 6); } // Remove the ".min" for ".min.json" files. - var moduleData = ['var ', moduleName, ' = JSON.parse("', obj.escapeCodeString(obj.fs.readFileSync(obj.path.join(moduleDirPath, modulesDir[i])).toString('utf8')), '");\r\n']; + var d = obj.fs.readFileSync(obj.path.join(moduleDirPath, modulesDir[i])).toString('utf8').split('\'').join('\\\''); + var moduleData = ['var ', moduleName, ' = JSON.parse(\'', d, '\');\r\n']; // Add to all cores modulesAdd['windows-amt'].push(...moduleData); @@ -2546,15 +2547,17 @@ function CreateMeshCentralServer(config, args) { } else { obj.defaultMeshCores[i] = [obj.common.IntToStr(0), ...modulesAdd[i], meshCore].join(''); } + obj.defaultMeshCores[i] = Buffer.from(obj.defaultMeshCores[i], 'utf8'); obj.defaultMeshCoresHash[i] = obj.crypto.createHash('sha384').update(obj.defaultMeshCores[i]).digest('binary'); obj.debug('main', 'Core module ' + i + ' is ' + obj.defaultMeshCores[i].length + ' bytes.'); + //console.log('Core module ' + i + ' is ' + obj.defaultMeshCores[i].length + ' bytes.'); // DEBUG, Print the core size - //obj.fs.writeFile("C:\\temp\\" + i + ".js", obj.defaultMeshCores[i].substring(4), function () { }); // DEBUG, Write the core to file + //obj.fs.writeFile("C:\\temp\\" + i + ".js", obj.defaultMeshCores[i].slice(4), function () { }); // DEBUG, Write the core to file // Compress the mesh cores with DEFLATE var callback = function MeshCoreDeflateCb(err, buffer) { if (err == null) { obj.defaultMeshCoresDeflate[MeshCoreDeflateCb.i] = buffer; } } callback.i = i; - require('zlib').deflate(obj.defaultMeshCores[i], { level: require('zlib').Z_BEST_COMPRESSION }, callback); + require('zlib').deflate(obj.defaultMeshCores[i], { level: require('zlib').Z_BEST_COMPRESSION }, callback); } } diff --git a/webserver.js b/webserver.js index 009b946d..41c0f9f4 100644 --- a/webserver.js +++ b/webserver.js @@ -6902,8 +6902,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF agent.send(obj.common.ShortToStr(11) + obj.common.ShortToStr(0)); // Command 11, ask for mesh core hash. } else if (coretype == 'custom') { agent.agentCoreCheck = 1000; // Tell the agent object we are using a custom core. - const hash = obj.crypto.createHash('sha384').update(Buffer.from(coredata, 'binary')).digest().toString('binary'); // Perform a SHA384 hash on the core module - agent.sendBinary(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + hash + coredata); // Send the code module to the agent + var buf = Buffer.from(coredata, 'utf8'); + const hash = obj.crypto.createHash('sha384').update(buf).digest().toString('binary'); // Perform a SHA384 hash on the core module + agent.sendBinary(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + hash + buf.toString('binary')); // Send the code module to the agent } } };