Fixed utf8 in meshcore.

Ylian Saint-Hilaire committed Jan 13, 2022 at 14:34 UTC cd85c66a1d3365582cb71acabdbc0d30adedcdde
3 files changed +10 -6
meshagent.js
+1 -1
@@ -224,7 +224,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
224 if (obj.authenticated == 2) {
225 // Send the updated core.
226 delete obj.agentCoreUpdatePending;
227 - obj.sendBinary(common.ShortToStr(10) + common.ShortToStr(0) + argument.hash + argument.core, function () { parent.parent.taskLimiter.completed(taskid); }); // MeshCommand_CoreModule, start core update
227 + 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
228 parent.agentStats.updatingCoreCount++;
229 parent.parent.debug('agent', "Updating core " + argument.name);
230 } else {
meshcentral.js
+6 -3
@@ -2479,7 +2479,8 @@ function CreateMeshCentralServer(config, args) {
2479 if (modulesDir[i].toLowerCase().endsWith('.json')) {
2480 var moduleName = modulesDir[i].substring(0, modulesDir[i].length - 5);
2481 if (moduleName.endsWith('.min')) { moduleName = moduleName.substring(0, moduleName.length - 6); } // Remove the ".min" for ".min.json" files.
2482 - var moduleData = ['var ', moduleName, ' = JSON.parse("', obj.escapeCodeString(obj.fs.readFileSync(obj.path.join(moduleDirPath, modulesDir[i])).toString('utf8')), '");\r\n'];
2482 + var d = obj.fs.readFileSync(obj.path.join(moduleDirPath, modulesDir[i])).toString('utf8').split('\'').join('\\\'');
2483 + var moduleData = ['var ', moduleName, ' = JSON.parse(\'', d, '\');\r\n'];
2484
2485 // Add to all cores
2486 modulesAdd['windows-amt'].push(...moduleData);
@@ -2546,15 +2547,17 @@ function CreateMeshCentralServer(config, args) {
2547 } else {
2548 obj.defaultMeshCores[i] = [obj.common.IntToStr(0), ...modulesAdd[i], meshCore].join('');
2549 }
2550 + obj.defaultMeshCores[i] = Buffer.from(obj.defaultMeshCores[i], 'utf8');
2551 obj.defaultMeshCoresHash[i] = obj.crypto.createHash('sha384').update(obj.defaultMeshCores[i]).digest('binary');
2552 obj.debug('main', 'Core module ' + i + ' is ' + obj.defaultMeshCores[i].length + ' bytes.');
2553 +
2554 //console.log('Core module ' + i + ' is ' + obj.defaultMeshCores[i].length + ' bytes.'); // DEBUG, Print the core size
2552 - //obj.fs.writeFile("C:\\temp\\" + i + ".js", obj.defaultMeshCores[i].substring(4), function () { }); // DEBUG, Write the core to file
2555 + //obj.fs.writeFile("C:\\temp\\" + i + ".js", obj.defaultMeshCores[i].slice(4), function () { }); // DEBUG, Write the core to file
2556
2557 // Compress the mesh cores with DEFLATE
2558 var callback = function MeshCoreDeflateCb(err, buffer) { if (err == null) { obj.defaultMeshCoresDeflate[MeshCoreDeflateCb.i] = buffer; } }
2559 callback.i = i;
2557 - require('zlib').deflate(obj.defaultMeshCores[i], { level: require('zlib').Z_BEST_COMPRESSION }, callback);
2560 + require('zlib').deflate(obj.defaultMeshCores[i], { level: require('zlib').Z_BEST_COMPRESSION }, callback);
2561 }
2562 }
2563
webserver.js
+3 -2
@@ -6902,8 +6902,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
6902 agent.send(obj.common.ShortToStr(11) + obj.common.ShortToStr(0)); // Command 11, ask for mesh core hash.
6903 } else if (coretype == 'custom') {
6904 agent.agentCoreCheck = 1000; // Tell the agent object we are using a custom core.
6905 - const hash = obj.crypto.createHash('sha384').update(Buffer.from(coredata, 'binary')).digest().toString('binary'); // Perform a SHA384 hash on the core module
6906 - agent.sendBinary(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + hash + coredata); // Send the code module to the agent
6905 + var buf = Buffer.from(coredata, 'utf8');
6906 + const hash = obj.crypto.createHash('sha384').update(buf).digest().toString('binary'); // Perform a SHA384 hash on the core module
6907 + agent.sendBinary(obj.common.ShortToStr(10) + obj.common.ShortToStr(0) + hash + buf.toString('binary')); // Send the code module to the agent
6908 }
6909 }
6910 };