2Copyright 2020-2021 Intel Corporation
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
17Object.defineProperty(Array.prototype, 'getParameterEx',
19 value: function (name, defaultValue)
22 for (i = 0; i < this.length; ++i)
24 if (this[i] == name) { return (null); }
25 if (this[i].startsWith(name + '='))
27 ret = this[i].substring(name.length + 1);
28 if (ret.startsWith('"')) { ret = ret.substring(1, ret.length - 1); }
32 return (defaultValue);
35Object.defineProperty(Array.prototype, 'getParameter',
37 value: function (name, defaultValue)
39 return (this.getParameterEx('-' + name, defaultValue));
43// The following line just below with 'msh=' needs to stay exactly like this since MeshCentral will replace it with the correct settings.
45var translation = JSON.parse(msh.translation);
47var lang = require('util-language').current;
48if (lang == null) { lang = 'en'; }
51 console.log("Langauge envronment variable was not set (process.env.LANG). Defaulting to English ('en').\nSee the agent-translations.json file for a list of current languages that are implemented\nUsage: meshcentral -lang=en\n");
53if (process.argv.getParameter('lang', lang) == null)
55 console.log('\nCurrent Language: ' + lang + '\n');
60 lang = process.argv.getParameter('lang', lang).toLowerCase();
61 lang = lang.split('_').join('-');
62 if (translation[lang] == null)
64 if (translation[lang.split('-')[0]] == null)
66 console.log('Language: ' + lang + ' is not translated.');
67 console.log("try: './"+ process.execPath.split('/').pop() + " -lang=en' for English");
68 console.log("See the agent-translations.json file for a list of current languages that are implemented.")
73 lang = lang.split('-')[0];
80 for (var i in translation['en'])
82 // If translated entries are missing, substitute the english translation
83 if (translation[lang][i] == null) { translation[lang][i] = translation['en'][i]; }
88var displayName = msh.displayName ? msh.displayName : 'MeshCentral Agent';
89var s = null, buttons = [translation[lang].cancel], skip = false;
90var serviceName = msh.meshServiceName ? msh.meshServiceName : 'meshagent';
92try { s = require('service-manager').manager.getService(serviceName); } catch (e) { }
94var connectArgs = [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1'];
95connectArgs.push('--MeshName="' + msh.MeshName + '"');
96connectArgs.push('--MeshType="' + msh.MeshType + '"');
97connectArgs.push('--MeshID="' + msh.MeshID + '"');
98connectArgs.push('--ServerID="' + msh.ServerID + '"');
99connectArgs.push('--MeshServer="' + msh.MeshServer + '"');
100connectArgs.push('--AgentCapabilities="0x00000020"');
101if (msh.displayName) { connectArgs.push('--displayName="' + msh.displayName + '"'); }
102if (msh.agentName) { connectArgs.push('--agentName="' + msh.agentName + '"'); }
104function _install(parms)
107 var mstr = require('fs').createWriteStream(process.execPath + '.msh', { flags: 'wb' });
111 mstr.write(i + '=' + msh[i] + '\n');
115 if (parms == null) { parms = []; }
116 if (msh.companyName) { parms.unshift('--companyName="' + msh.companyName + '"'); }
117 if (msh.displayName) { parms.unshift('--displayName="' + msh.displayName + '"'); }
118 if (msh.meshServiceName) { parms.unshift('--meshServiceName="' + msh.meshServiceName + '"'); }
119 parms.unshift('--copy-msh=1');
120 parms.unshift('--no-embedded=1');
121 parms.unshift('-fullinstall');
122 parms.unshift(process.execPath.split('/').pop());
124 global._child = require('child_process').execFile(process.execPath, parms);
125 global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
126 global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
127 global._child.waitExit();
132 global._child = require('child_process').execFile(process.execPath,
133 [process.execPath.split('/').pop(), '-fulluninstall', '--no-embedded=1', '--meshServiceName="' + serviceName + '"']);
135 global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
136 global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
137 global._child.waitExit();
140if (msh.InstallFlags == null)
142 msh.InstallFlags = 3;
145 msh.InstallFlags = parseInt(msh.InstallFlags.toString());
148if (process.argv.includes('-mesh'))
150 console.log(JSON.stringify(msh, null, 2));
153if (process.argv.includes('-translations'))
155 console.log(JSON.stringify(translation));
158if (process.argv.includes('-help') || (process.platform == 'linux' && process.env['XAUTHORITY'] == null && process.env['DISPLAY'] == null && process.argv.length == 1))
160 console.log("\n" + translation[lang].commands + ": ");
161 if ((msh.InstallFlags & 1) == 1)
163 console.log('./' + process.execPath.split('/').pop() + ' -connect');
165 if ((msh.InstallFlags & 2) == 2)
169 console.log('./' + process.execPath.split('/').pop() + ' -update');
170 console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
174 console.log('./' + process.execPath.split('/').pop() + ' -install');
175 console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
182if ((msh.InstallFlags & 1) == 1)
184 buttons.unshift(translation[lang].connect);
185 if (process.argv.includes('-connect'))
187 global._child = require('child_process').execFile(process.execPath, connectArgs);
188 global._child.stdout.on('data', function (c) { });
189 global._child.stderr.on('data', function (c) { });
190 global._child.on('exit', function (code) { process.exit(code); });
192 console.log("\n" + translation[lang].url + ": " + msh.MeshServer);
193 console.log(translation[lang].group + ": " + msh.MeshName);
194 console.log('\n' + translation[lang].ctrlc + '\n');
199if ((!skip) && ((msh.InstallFlags & 2) == 2))
201 if (!require('user-sessions').isRoot())
203 console.log('\n' + translation[lang].elevation);
204 console.log(translation[lang].sudo);
209 if ((process.platform == 'darwin') || require('message-box').kdialog)
211 buttons.unshift(translation[lang].setup);
214 buttons.unshift(translation[lang].uninstall);
215 buttons.unshift(translation[lang].update);
219 buttons.unshift(translation[lang].install);
225 if (process.argv.includes('-install') || process.argv.includes('-update'))
228 for (var i = 0; i < process.argv.length; ++i)
230 if (process.argv[i].startsWith('--installPath='))
232 p.push('--installPath="' + process.argv[i].split('=').pop() + '"');
234 else if(process.argv[i].startsWith('--'))
236 p.push(process.argv[i]);
242 else if (process.argv.includes('-uninstall'))
249 if (!require('message-box').kdialog && ((require('message-box').zenity == null) || (!require('message-box').zenity.extra)))
251 console.log('\n' + translation[lang].graphicalerror + '.');
252 console.log(translation[lang].zenity + ".\n");
253 console.log(translation[lang].commands + ": ");
254 if ((msh.InstallFlags & 1) == 1)
256 console.log('./' + process.execPath.split('/').pop() + ' -connect');
258 if ((msh.InstallFlags & 2) == 2)
262 console.log('./' + process.execPath.split('/').pop() + ' -update');
263 console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
267 console.log('./' + process.execPath.split('/').pop() + ' -install');
268 console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
275 if (process.platform == 'darwin')
277 if (!require('user-sessions').isRoot()) { console.log('\n' + translation[lang].elevation); process.exit(); }
286 msg = translation[lang].agent + ": " + translation[lang].status[0] + '\n';
289 msg = translation[lang].agent + ": " + (s.isRunning() ? translation[lang].status[1] : translation[lang].status[2]) + '\n';
292 msg += (translation[lang].group + ": " + msh.MeshName + '\n');
293 msg += (translation[lang].url + ": " + msh.MeshServer + '\n');
295 var p = require('message-box').create(displayName + " " + translation[lang].setup, msg, 99999, buttons);
300 case translation[lang].cancel:
303 case translation[lang].setup:
304 var d = require('message-box').create(displayName, msg, 99999, [translation[lang].update, translation[lang].uninstall, translation[lang].cancel]);
309 case translation[lang].update:
310 case translation[lang].install:
313 case translation[lang].uninstall:
320 }).catch(function (v) { process.exit(); });
322 case translation[lang].connect:
323 global._child = require('child_process').execFile(process.execPath, connectArgs);
324 global._child.stdout.on('data', function (c) { });
325 global._child.stderr.on('data', function (c) { });
326 global._child.on('exit', function (code) { process.exit(code); });
328 msg = (translation[lang].group + ": " + msh.MeshName + '\n');
329 msg += (translation[lang].url + ": " + msh.MeshServer + '\n');
331 if (process.platform != 'darwin')
333 if (!require('message-box').zenity && require('message-box').kdialog)
335 msg += ('\n' + translation[lang].pressok);
339 var d = require('message-box').create(displayName, msg, 99999, [translation[lang].disconnect]);
340 d.then(function (v) { process.exit(); }).catch(function (v) { process.exit(); });
342 case translation[lang].uninstall:
346 case translation[lang].install:
347 case translation[lang].update:
356 }).catch(function (e)