Reverted recoverycore.js

Ylian Saint-Hilaire committed Jun 4, 2021 at 11:26 UTC 13409348c20255a170f41a2b0f48c8e00a7e19c8
1 file changed +31 -197
agents/recoverycore.js
+31 -197
@@ -6,83 +6,9 @@ var nextTunnelIndex = 1;
6 var tunnels = {};
7 var fs = require('fs');
8
9 -
10 -function _getPotentialServiceNames()
11 -{
12 - var registry = require('win-registry');
13 - var ret = [];
14 - var K = registry.QueryKey(registry.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services');
15 - var service, s;
16 - while (K.subkeys.length > 0)
17 - {
18 - service = K.subkeys.shift();
19 - try
20 - {
21 - s = registry.QueryKey(registry.HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Services\\' + service, 'ImagePath');
22 - if (s.startsWith(process.execPath) || s.startsWith('"' + process.execPath + '"'))
23 - {
24 - ret.push(service);
25 - }
26 - }
27 - catch (x)
28 - {
29 - }
30 - }
31 - return (ret);
32 -}
33 -function _verifyServiceName(names)
34 -{
35 - var i;
36 - var s;
37 - var ret = null;
38 - for (i = 0; i < names.length; ++i)
39 - {
40 - try
41 - {
42 - s = require('service-manager').manager.getService(names[i]);
43 - if (s.isMe())
44 - {
45 - ret = names[i];
46 - s.close();
47 - break;
48 - }
49 - s.close();
50 - }
51 - catch (z) { }
52 - }
53 - return (ret);
54 -}
55 -
56 -function windows_getCommandLine()
57 -{
58 - var parms = [];
59 - var GM = require('_GenericMarshal');
60 - var k32 = GM.CreateNativeProxy('kernel32.dll');
61 - var s32 = GM.CreateNativeProxy('shell32.dll');
62 - k32.CreateMethod('GetCommandLineW');
63 - k32.CreateMethod('LocalFree');
64 - s32.CreateMethod('CommandLineToArgvW');
65 - var v = k32.GetCommandLineW();
66 - var i;
67 - var len = GM.CreateVariable(4);
68 - var val = s32.CommandLineToArgvW(v, len);
69 - len = len.toBuffer().readInt32LE(0);
70 - if (len > 0)
71 - {
72 - for (i = 0; i < len; ++i)
73 - {
74 - parms.push(val.Deref(i * GM.PointerSize, GM.PointerSize).Deref().Wide2UTF8);
75 - }
76 - }
77 - k32.LocalFree(val);
78 - return (parms);
79 -}
80 -
81 -if (require('MeshAgent').ARCHID == null)
82 -{
9 +if (require('MeshAgent').ARCHID == null) {
10 var id = null;
84 - switch (process.platform)
85 - {
11 + switch (process.platform) {
12 case 'win32':
13 id = require('_GenericMarshal').PointerSize == 4 ? 3 : 4;
14 break;
@@ -90,12 +16,10 @@ if (require('MeshAgent').ARCHID == null)
16 id = require('_GenericMarshal').PointerSize == 4 ? 31 : 30;
17 break;
18 case 'darwin':
93 - try
94 - {
19 + try {
20 id = require('os').arch() == 'x64' ? 16 : 29;
21 }
97 - catch (xx)
98 - {
22 + catch (xx) {
23 id = 16;
24 }
25 break;
@@ -105,22 +29,17 @@ if (require('MeshAgent').ARCHID == null)
29
30 //attachDebugger({ webport: 9994, wait: 1 }).then(function (p) { console.log('Debug on port: ' + p); });
31
108 -function sendConsoleText(msg, sessionid)
109 -{
110 - if (sessionid != null)
111 - {
32 +function sendConsoleText(msg, sessionid) {
33 + if (sessionid != null) {
34 require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg, sessionid: sessionid });
35 }
114 - else
115 - {
36 + else {
37 require('MeshAgent').SendCommand({ action: 'msg', type: 'console', value: msg });
38 }
39 }
40
120 -function sendAgentMessage(msg, icon)
121 -{
122 - if (sendAgentMessage.messages == null)
123 - {
41 +function sendAgentMessage(msg, icon) {
42 + if (sendAgentMessage.messages == null) {
43 sendAgentMessage.messages = {};
44 sendAgentMessage.nextid = 1;
45 }
@@ -129,11 +48,9 @@ function sendAgentMessage(msg, icon)
48 }
49
50 // Add to the server event log
132 -function MeshServerLog(msg, state)
133 -{
51 +function MeshServerLog(msg, state) {
52 if (typeof msg == 'string') { msg = { action: 'log', msg: msg }; } else { msg.action = 'log'; }
135 - if (state)
136 - {
53 + if (state) {
54 if (state.userid) { msg.userid = state.userid; }
55 if (state.username) { msg.username = state.username; }
56 if (state.sessionid) { msg.sessionid = state.sessionid; }
@@ -143,11 +60,9 @@ function MeshServerLog(msg, state)
60 }
61
62 // Add to the server event log, use internationalized events
146 -function MeshServerLogEx(id, args, msg, state)
147 -{
63 +function MeshServerLogEx(id, args, msg, state) {
64 var msg = { action: 'log', msgid: id, msgArgs: args, msg: msg };
149 - if (state)
150 - {
65 + if (state) {
66 if (state.userid) { msg.userid = state.userid; }
67 if (state.username) { msg.username = state.username; }
68 if (state.sessionid) { msg.sessionid = state.sessionid; }
@@ -431,34 +346,6 @@ function windows_execve(name, agentfilename, sessionid) {
346 var arg2 = require('_GenericMarshal').CreateVariable('/C wmic service "' + name + '" call stopservice & "' + cwd + agentfilename + '.update.exe" -b64exec ' + 'dHJ5CnsKICAgIHZhciBzZXJ2aWNlTG9jYXRpb24gPSBwcm9jZXNzLmFyZ3YucG9wKCk7CiAgICByZXF1aXJlKCdwcm9jZXNzLW1hbmFnZXInKS5lbnVtZXJhdGVQcm9jZXNzZXMoKS50aGVuKGZ1bmN0aW9uIChwcm9jKQogICAgewogICAgICAgIGZvciAodmFyIHAgaW4gcHJvYykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChwcm9jW3BdLnBhdGggPT0gc2VydmljZUxvY2F0aW9uKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwcm9jZXNzLmtpbGwocHJvY1twXS5waWQpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHByb2Nlc3MuZXhpdCgpOwogICAgfSk7Cn0KY2F0Y2goZSkKewogICAgcHJvY2Vzcy5leGl0KCk7Cn0=' +
347 ' "' + process.execPath + '" & copy "' + cwd + agentfilename + '.update.exe" "' + process.execPath + '" & wmic service "' + name + '" call startservice & erase "' + cwd + agentfilename + '.update.exe"', { wide: true });
348
434 - if (name == null)
435 - {
436 - // We can continue with self update for Temp/Console Mode on Windows
437 - var db = null;
438 - var update = cwd + agentfilename + '.update.exe';
439 - var updatedb = cwd + agentfilename + '.update.db';
440 - var parms = windows_getCommandLine(); parms.shift();
441 -
442 - var updatesource = parms.find(function (v) { return (v.startsWith('--updateSourcePath=')); });
443 - if (updatesource == null)
444 - {
445 - parms.push('--updateSourcePath="' + cwd + agentfilename + '"');
446 - updatesource = (cwd + agentfilename).split('.exe'); updatesource.pop(); updatesource = updatesource.join('.exe');
447 - db = updatesource + '.db';
448 - updatesource = (' & move "' + updatedb + '" "' + db + '"') + (' & erase "' + updatedb + '"');
449 - }
450 - else
451 - {
452 - updatesource = updatesource.substring(19).split('.exe');
453 - updatesource.pop(); updatesource = updatesource.join('.exe');
454 - db = updatesource + '.db';
455 - updatesource = (' & move "' + update + '" "' + updatesource + '.exe" & move "' + updatedb + '" "' + db + '" & erase "' + updatedb + '"') + (' & echo move "' + update + '" "' + updatesource + '.exe" & echo move "' + updatedb + '" "' + db + '"');
456 - }
457 -
458 - var tmp = '/C echo copy "' + db + '" "' + updatedb + '" & copy "' + db + '" "' + updatedb + '"' + ' & "' + update + '" ' + parms.join(' ') + updatesource + ' & erase "' + update + '" & echo ERASE "' + update + '"';
459 - arg2 = require('_GenericMarshal').CreateVariable(tmp, { wide: true });
460 - }
461 -
349 arg1.pointerBuffer().copy(args.toBuffer());
350 arg2.pointerBuffer().copy(args.toBuffer(), require('_GenericMarshal').PointerSize);
351
@@ -476,8 +363,7 @@ function agentUpdate_Start(updateurl, updateoptions) {
363 if (updateurl.startsWith("wss://")) { updateurl = "https://" + updateurl.substring(6); }
364 }
365
479 - if (agentUpdate_Start._selfupdate != null)
480 - {
366 + if (agentUpdate_Start._selfupdate != null) {
367 // We were already called, so we will ignore this duplicate request
368 if (sessionid != null) { sendConsoleText('Self update already in progress...', sessionid); }
369 }
@@ -487,58 +373,25 @@ function agentUpdate_Start(updateurl, updateoptions) {
373 // This agent doesn't have the ability to tell us which ARCHID it is, so we don't know which agent to pull
374 sendConsoleText('Unable to initiate update, agent ARCHID is not defined', sessionid);
375 }
490 - else
491 - {
376 + else {
377 var agentfilename = process.execPath.split(process.platform == 'win32' ? '\\' : '/').pop(); // Local File Name, ie: MeshAgent.exe
378 var name = require('MeshAgent').serviceName;
494 - if (name == null) { name = process.platform == 'win32' ? 'Mesh Agent' : 'meshagent'; }
495 - if (process.platform == 'win32')
496 - {
497 - // Special Processing for Temporary/Console Mode Agents on Windows
498 - var parms = windows_getCommandLine();
499 - if (parms.findIndex(function (val) { return (val.toUpperCase() == 'RUN' || val.toUpperCase() == 'CONNECT');})>=0)
500 - {
501 - // This is a Temporary/Console Mode Agent
502 - sendConsoleText('This is a temporary/console agent, checking for conflicts with background services...');
503 -
504 - // Check to see if our binary conflicts with an installed agent
505 - var agents = _getPotentialServiceNames();
506 - if(_getPotentialServiceNames().length>0)
507 - {
508 - sendConsoleText('Self update cannot continue because the installed agent (' + agents[0] + ') conflicts with the currently running Temp/Console agent...', sessionid);
509 - return;
510 - }
511 -
512 -
513 - sendConsoleText('No conflicts detected...');
514 - name = null;
515 - }
516 - else
517 - {
518 - // Not running in Temp/Console Mode... No Op here....
519 - }
520 - }
521 - else
522 - {
523 - // Non Windows Self Update
524 - try
525 - {
526 - var s = require('service-manager').manager.getService(name);
527 - if (!s.isMe())
528 - {
529 - if (process.platform == 'win32') { s.close(); }
530 - sendConsoleText('Self Update cannot continue, this agent is not an instance of background service (' + name + ')', sessionid);
531 - return;
532 - }
379 + if (name == null) { name = (process.platform == 'win32' ? 'Mesh Agent' : 'meshagent'); } // This is an older agent that doesn't expose the service name, so use the default
380 + try {
381 + var s = require('service-manager').manager.getService(name);
382 + if (!s.isMe()) {
383 if (process.platform == 'win32') { s.close(); }
534 - }
535 - catch (zz)
536 - {
537 - sendConsoleText('Self Update Failed because this agent is not an instance of (' + name + ')', sessionid);
538 - sendAgentMessage('Self Update Failed because this agent is not an instance of (' + name + ')', 3);
384 + sendConsoleText('Self Update cannot continue, this agent is not an instance of (' + name + ')', sessionid);
385 return;
386 }
387 + if (process.platform == 'win32') { s.close(); }
388 + }
389 + catch (zz) {
390 + sendConsoleText('Self Update Failed because this agent is not an instance of (' + name + ')', sessionid);
391 + sendAgentMessage('Self Update Failed because this agent is not an instance of (' + name + ')', 3);
392 + return;
393 }
394 +
395 if ((sessionid != null) && (updateurl != null)) { sendConsoleText('Downloading update from: ' + updateurl, sessionid); }
396 var options = require('http').parseUri(updateurl != null ? updateurl : require('MeshAgent').ServerUrl);
397 options.protocol = 'https:';
@@ -1154,25 +1007,7 @@ require('MeshAgent').AddCommandHandler(function (data) {
1007 function processConsoleCommand(cmd, args, rights, sessionid) {
1008 try {
1009 var response = null;
1157 - switch (cmd)
1158 - {
1159 - default:
1160 - { // This is an unknown command, return an error message
1161 - response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.';
1162 - break;
1163 - }
1164 - case 'commandline':
1165 - {
1166 - if (process.platform == 'win32')
1167 - {
1168 - response = JSON.stringify(windows_getCommandLine(), null, 1);
1169 - }
1170 - else
1171 - {
1172 - response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.';
1173 - }
1174 - }
1175 - break;
1010 + switch (cmd) {
1011 case 'help':
1012 response = "Available commands are: agentupdate, agentupdateex, dbkeys, dbget, dbset, dbcompact, eval, netinfo, osinfo, setdebug, versions.";
1013 break;
@@ -1262,11 +1097,10 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
1097 response = objToString(interfaces, 0, ' ', true);
1098 break;
1099 }
1265 - case 'name':
1266 - {
1267 - response = 'Service Name = ' + require('MeshAgent').serviceName;
1268 - }
1100 + default: { // This is an unknown command, return an error message
1101 + response = 'Unknown command \"' + cmd + '\", type \"help\" for list of available commands.';
1102 break;
1103 + }
1104 }
1105 } catch (e) { response = "Command returned an exception error: " + e; console.log(e); }
1106 if (response != null) { sendConsoleText(response, sessionid); }