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