Added test/experimental console command to initiate update

Bryan Roe committed Jan 14, 2021 at 01:18 UTC f11140caeb077d2784e4cb3d38d02e5fc7037df5
1 file changed +106
agents/meshcore.js
+106
@@ -40,6 +40,23 @@ var MESHRIGHT_LIMITEVENTS = 8192;
40 var MESHRIGHT_CHATNOTIFY = 16384;
41 var MESHRIGHT_UNINSTALL = 32768;
42 var MESHRIGHT_NODESKTOP = 65536;
43 +if (require('MeshAgent').ARCHID == null)
44 +{
45 + var id = null;
46 + switch (process.platform)
47 + {
48 + case 'win32':
49 + id = require('_GenericMarshal').PointerSize == 4 ? 3 : 4;
50 + break;
51 + case 'freebsd':
52 + id = require('_GenericMarshal').PointerSize == 4 ? 31 : 30;
53 + break;
54 + case 'darwin':
55 + id = require('os').arch() == 'x64' ? 16 : 29;
56 + break;
57 + }
58 + if (id != null) { Object.defineProperty(require('MeshAgent'), 'ARCHID', { value: id }); }
59 +}
60
61 function createMeshCore(agent) {
62 var obj = {};
@@ -2713,6 +2730,95 @@ function createMeshCore(agent) {
2730 response = "Available commands: \r\n" + fin + ".";
2731 break;
2732 }
2733 + case 'agentupdate':
2734 + if (require('MeshAgent').ARCHID == null)
2735 + {
2736 + response = 'Unable to initiate update, agent ARCHID is not defined';
2737 + break;
2738 + }
2739 + if (this._selfupdate != null)
2740 + {
2741 + response = "Self update already in progress...";
2742 + }
2743 + else
2744 + {
2745 + var agentfilename = process.execPath.split(process.platform == 'win32' ? '\\' : '/').pop();
2746 + var name = require('MeshAgent').serviceName;
2747 + if (name == null) { name = process.platform == 'win32' ? 'Mesh Agent' : 'meshagent'; }
2748 + try
2749 + {
2750 + var s = require('service-manager').manager.getService(name);
2751 + if (s.isMe())
2752 + {
2753 + sendConsoleText('Service check SUCCESS');
2754 + }
2755 + else
2756 + {
2757 + s.close();
2758 + throw ('not a service');
2759 + break;
2760 + }
2761 + if(process.platform=='win32') {s.close();}
2762 + }
2763 + catch (zz)
2764 + {
2765 + response = 'This is not a service instance';
2766 + break;
2767 + }
2768 + sendConsoleText('Downloading update...');
2769 + var options = require('http').parseUri(require('MeshAgent').ServerUrl);
2770 + options.protocol = 'https:';
2771 + options.path = ('/meshagents?id=' + require('MeshAgent').ARCHID);
2772 + options.rejectUnauthorized = false;
2773 + this._selfupdate = require('https').get(options);
2774 + this._selfupdate.on('response', function (img)
2775 + {
2776 + this._file = require('fs').createWriteStream(agentfilename + '.update', {flags: 'wb'});
2777 + this._filehash = require('SHA384Stream').create();
2778 + this._filehash.on('hash', function (h)
2779 + {
2780 + sendConsoleText('Download complete. HASH=' + h.toString('hex'));
2781 + if(process.platform == 'win32')
2782 + {
2783 + this.child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe',
2784 + ['/C wmic service "' + name + '" call stopservice && copy "' + process.cwd() + agentfilename + '.update" "' + process.execPath + '" && wmic service "' + name + '" call startservice && erase "' + process.cwd() + agentfilename + '.update"'], { type: 4 | 0x8000 });
2785 + }
2786 + else
2787 + {
2788 + // remove binary
2789 + require('fs').unlinkSync(process.execPath);
2790 +
2791 + // copy update
2792 + require('fs').copyFileSync(process.cwd() + agentfilename + '.update', process.execPath);
2793 +
2794 + // erase update
2795 + require('fs').unlinkSync(process.cwd() + agentfilename + '.update');
2796 +
2797 + // add execute permissions
2798 + var m = require('fs').statSync(process.execPath).mode;
2799 + m |= (require('fs').CHMOD_MODES.S_IXUSR | require('fs').CHMOD_MODES.S_IXGRP | require('fs').CHMOD_MODES.S_IXOTH);
2800 + require('fs').chmodSync(process.execPath, m);
2801 +
2802 + sendConsoleText('Restarting service...');
2803 + try
2804 + {
2805 + // restart service
2806 + var s = require('service-manager').manager.getService(name);
2807 + s.restart();
2808 + }
2809 + catch(zz)
2810 + {
2811 + sendConsoleText('Error restarting service');
2812 + }
2813 + }
2814 + });
2815 + img.pipe(this._file);
2816 + img.pipe(this._filehash);
2817 + });
2818 + this._selfupdate.on('error', function (e) { sendConsoleText('Error fetching update'); });
2819 + }
2820 +
2821 + break;
2822 case 'msh':
2823 response = JSON.stringify(_MSH(), null, 2);
2824 break;