meshinstall-linux.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Copyright 2020-2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. Object.defineProperty(Array.prototype, 'getParameterEx',
  14. {
  15. value: function (name, defaultValue)
  16. {
  17. var i, ret;
  18. for (i = 0; i < this.length; ++i)
  19. {
  20. if (this[i] == name) { return (null); }
  21. if (this[i].startsWith(name + '='))
  22. {
  23. ret = this[i].substring(name.length + 1);
  24. if (ret.startsWith('"')) { ret = ret.substring(1, ret.length - 1); }
  25. return (ret);
  26. }
  27. }
  28. return (defaultValue);
  29. }
  30. });
  31. Object.defineProperty(Array.prototype, 'getParameter',
  32. {
  33. value: function (name, defaultValue)
  34. {
  35. return (this.getParameterEx('-' + name, defaultValue));
  36. }
  37. });
  38. // The following line just below with 'msh=' needs to stay exactly like this since MeshCentral will replace it with the correct settings.
  39. var msh = {};
  40. var translation = JSON.parse(msh.translation);
  41. var lang = require('util-language').current;
  42. if (lang == null) { lang = 'en'; }
  43. if (lang == "C"){
  44. lang = 'en';
  45. 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");
  46. }
  47. if (process.argv.getParameter('lang', lang) == null)
  48. {
  49. console.log('\nCurrent Language: ' + lang + '\n');
  50. process.exit();
  51. }
  52. else
  53. {
  54. lang = process.argv.getParameter('lang', lang).toLowerCase();
  55. lang = lang.split('_').join('-');
  56. if (translation[lang] == null)
  57. {
  58. if (translation[lang.split('-')[0]] == null)
  59. {
  60. console.log('Language: ' + lang + ' is not translated.');
  61. console.log("try: './"+ process.execPath.split('/').pop() + " -lang=en' for English");
  62. console.log("See the agent-translations.json file for a list of current languages that are implemented.")
  63. process.exit();
  64. }
  65. else
  66. {
  67. lang = lang.split('-')[0];
  68. }
  69. }
  70. }
  71. if (lang != 'en')
  72. {
  73. for (var i in translation['en'])
  74. {
  75. // If translated entries are missing, substitute the english translation
  76. if (translation[lang][i] == null) { translation[lang][i] = translation['en'][i]; }
  77. }
  78. }
  79. var displayName = msh.displayName ? msh.displayName : 'MeshCentral Agent';
  80. var s = null, buttons = [translation[lang].cancel], skip = false;
  81. var serviceName = msh.meshServiceName ? msh.meshServiceName : 'meshagent';
  82. try { s = require('service-manager').manager.getService(serviceName); } catch (e) { }
  83. var connectArgs = [process.execPath.split('/').pop(), '--no-embedded=1', '--disableUpdate=1'];
  84. connectArgs.push('--MeshName="' + msh.MeshName + '"');
  85. connectArgs.push('--MeshType="' + msh.MeshType + '"');
  86. connectArgs.push('--MeshID="' + msh.MeshID + '"');
  87. connectArgs.push('--ServerID="' + msh.ServerID + '"');
  88. connectArgs.push('--MeshServer="' + msh.MeshServer + '"');
  89. connectArgs.push('--AgentCapabilities="0x00000020"');
  90. if (msh.displayName) { connectArgs.push('--displayName="' + msh.displayName + '"'); }
  91. if (msh.agentName) { connectArgs.push('--agentName="' + msh.agentName + '"'); }
  92. function _install(parms)
  93. {
  94. var i;
  95. var mstr = require('fs').createWriteStream(process.execPath + '.msh', { flags: 'wb' });
  96. for (i in msh)
  97. {
  98. mstr.write(i + '=' + msh[i] + '\n');
  99. }
  100. mstr.end();
  101. if (parms == null) { parms = []; }
  102. if (msh.companyName) { parms.unshift('--companyName="' + msh.companyName + '"'); }
  103. if (msh.displayName) { parms.unshift('--displayName="' + msh.displayName + '"'); }
  104. if (msh.meshServiceName) { parms.unshift('--meshServiceName="' + msh.meshServiceName + '"'); }
  105. parms.unshift('--copy-msh=1');
  106. parms.unshift('--no-embedded=1');
  107. parms.unshift('-fullinstall');
  108. parms.unshift(process.execPath.split('/').pop());
  109. global._child = require('child_process').execFile(process.execPath, parms);
  110. global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
  111. global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
  112. global._child.waitExit();
  113. }
  114. function _uninstall()
  115. {
  116. global._child = require('child_process').execFile(process.execPath,
  117. [process.execPath.split('/').pop(), '-fulluninstall', '--no-embedded=1', '--meshServiceName="' + serviceName + '"']);
  118. global._child.stdout.on('data', function (c) { process.stdout.write(c.toString()); });
  119. global._child.stderr.on('data', function (c) { process.stdout.write(c.toString()); });
  120. global._child.waitExit();
  121. }
  122. if (msh.InstallFlags == null)
  123. {
  124. msh.InstallFlags = 3;
  125. } else
  126. {
  127. msh.InstallFlags = parseInt(msh.InstallFlags.toString());
  128. }
  129. if (process.argv.includes('-mesh'))
  130. {
  131. console.log(JSON.stringify(msh, null, 2));
  132. process.exit();
  133. }
  134. if (process.argv.includes('-translations'))
  135. {
  136. console.log(JSON.stringify(translation));
  137. process.exit();
  138. }
  139. if (process.argv.includes('-help') || (process.platform == 'linux' && process.env['XAUTHORITY'] == null && process.env['DISPLAY'] == null && process.argv.length == 1))
  140. {
  141. console.log("\n" + translation[lang].commands + ": ");
  142. if ((msh.InstallFlags & 1) == 1)
  143. {
  144. console.log('./' + process.execPath.split('/').pop() + ' -connect');
  145. }
  146. if ((msh.InstallFlags & 2) == 2)
  147. {
  148. if (s)
  149. {
  150. console.log('./' + process.execPath.split('/').pop() + ' -update');
  151. console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
  152. }
  153. else
  154. {
  155. console.log('./' + process.execPath.split('/').pop() + ' -install');
  156. console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
  157. }
  158. }
  159. console.log('');
  160. process.exit();
  161. }
  162. if ((msh.InstallFlags & 1) == 1)
  163. {
  164. buttons.unshift(translation[lang].connect);
  165. if (process.argv.includes('-connect'))
  166. {
  167. global._child = require('child_process').execFile(process.execPath, connectArgs);
  168. global._child.stdout.on('data', function (c) { });
  169. global._child.stderr.on('data', function (c) { });
  170. global._child.on('exit', function (code) { process.exit(code); });
  171. console.log("\n" + translation[lang].url + ": " + msh.MeshServer);
  172. console.log(translation[lang].group + ": " + msh.MeshName);
  173. console.log('\n' + translation[lang].ctrlc + '\n');
  174. skip = true;
  175. }
  176. }
  177. if ((!skip) && ((msh.InstallFlags & 2) == 2))
  178. {
  179. if (!require('user-sessions').isRoot())
  180. {
  181. console.log('\n' + translation[lang].elevation);
  182. console.log(translation[lang].sudo);
  183. process.exit();
  184. }
  185. if (s)
  186. {
  187. if ((process.platform == 'darwin') || require('message-box').kdialog)
  188. {
  189. buttons.unshift(translation[lang].setup);
  190. } else
  191. {
  192. buttons.unshift(translation[lang].uninstall);
  193. buttons.unshift(translation[lang].update);
  194. }
  195. } else
  196. {
  197. buttons.unshift(translation[lang].install);
  198. }
  199. }
  200. if (!skip)
  201. {
  202. if (process.argv.includes('-install') || process.argv.includes('-update'))
  203. {
  204. var p = [];
  205. for (var i = 0; i < process.argv.length; ++i)
  206. {
  207. if (process.argv[i].startsWith('--installPath='))
  208. {
  209. p.push('--installPath="' + process.argv[i].split('=').pop() + '"');
  210. }
  211. else if(process.argv[i].startsWith('--'))
  212. {
  213. p.push(process.argv[i]);
  214. }
  215. }
  216. _install(p);
  217. process.exit();
  218. }
  219. else if (process.argv.includes('-uninstall'))
  220. {
  221. _uninstall();
  222. process.exit();
  223. }
  224. else
  225. {
  226. if (!require('message-box').kdialog && ((require('message-box').zenity == null) || (!require('message-box').zenity.extra)))
  227. {
  228. console.log('\n' + translation[lang].graphicalerror + '.');
  229. console.log(translation[lang].zenity + ".\n");
  230. console.log(translation[lang].commands + ": ");
  231. if ((msh.InstallFlags & 1) == 1)
  232. {
  233. console.log('./' + process.execPath.split('/').pop() + ' -connect');
  234. }
  235. if ((msh.InstallFlags & 2) == 2)
  236. {
  237. if (s)
  238. {
  239. console.log('./' + process.execPath.split('/').pop() + ' -update');
  240. console.log('./' + process.execPath.split('/').pop() + ' -uninstall');
  241. }
  242. else
  243. {
  244. console.log('./' + process.execPath.split('/').pop() + ' -install');
  245. console.log('./' + process.execPath.split('/').pop() + ' -install --installPath="/alternate/path"');
  246. }
  247. }
  248. console.log('');
  249. process.exit();
  250. }
  251. }
  252. if (process.platform == 'darwin')
  253. {
  254. if (!require('user-sessions').isRoot()) { console.log('\n' + translation[lang].elevation); process.exit(); }
  255. }
  256. }
  257. if (!skip)
  258. {
  259. if (!s)
  260. {
  261. msg = translation[lang].agent + ": " + translation[lang].status[0] + '\n';
  262. } else
  263. {
  264. msg = translation[lang].agent + ": " + (s.isRunning() ? translation[lang].status[1] : translation[lang].status[2]) + '\n';
  265. }
  266. msg += (translation[lang].group + ": " + msh.MeshName + '\n');
  267. msg += (translation[lang].url + ": " + msh.MeshServer + '\n');
  268. var p = require('message-box').create(displayName + " " + translation[lang].setup, msg, 99999, buttons);
  269. p.then(function (v)
  270. {
  271. switch (v)
  272. {
  273. case translation[lang].cancel:
  274. process.exit();
  275. break;
  276. case translation[lang].setup:
  277. var d = require('message-box').create(displayName, msg, 99999, [translation[lang].update, translation[lang].uninstall, translation[lang].cancel]);
  278. d.then(function (v)
  279. {
  280. switch (v)
  281. {
  282. case translation[lang].update:
  283. case translation[lang].install:
  284. _install();
  285. break;
  286. case translation[lang].uninstall:
  287. _uninstall();
  288. break;
  289. default:
  290. break;
  291. }
  292. process.exit();
  293. }).catch(function (v) { process.exit(); });
  294. break;
  295. case translation[lang].connect:
  296. global._child = require('child_process').execFile(process.execPath, connectArgs);
  297. global._child.stdout.on('data', function (c) { });
  298. global._child.stderr.on('data', function (c) { });
  299. global._child.on('exit', function (code) { process.exit(code); });
  300. msg = (translation[lang].group + ": " + msh.MeshName + '\n');
  301. msg += (translation[lang].url + ": " + msh.MeshServer + '\n');
  302. if (process.platform != 'darwin')
  303. {
  304. if (!require('message-box').zenity && require('message-box').kdialog)
  305. {
  306. msg += ('\n' + translation[lang].pressok);
  307. }
  308. }
  309. var d = require('message-box').create(displayName, msg, 99999, [translation[lang].disconnect]);
  310. d.then(function (v) { process.exit(); }).catch(function (v) { process.exit(); });
  311. break;
  312. case translation[lang].uninstall:
  313. _uninstall();
  314. process.exit();
  315. break;
  316. case translation[lang].install:
  317. case translation[lang].update:
  318. _install();
  319. process.exit();
  320. break;
  321. default:
  322. console.log(v);
  323. process.exit();
  324. break;
  325. }
  326. }).catch(function (e)
  327. {
  328. console.log(e);
  329. process.exit();
  330. });
  331. }