Remove extra command handlers/table
Noah Zalev committed
Jul 8, 2021 at 10:24 UTC
939088bae4607813666946aa27a485c0ee81a7e4
1 file changed
-603
meshuser.js
-603
@@ -544,609 +544,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
544
});
545
} catch (e) { console.log(e); }
546
547
-
548
- var consoleCommands = {};
549
- consoleCommands['help'] = helpConsoleCommand;
550
- consoleCommands['certexpire'] = certexpireConsoleCommand;
551
- consoleCommands['webpush'] = webpushConsoleCommand;
552
- consoleCommands['amtmanager'] = amtmanagerConsoleCommand;
553
- consoleCommands['certhashes'] = certhashesConsoleCommand;
554
- consoleCommands['amtacm'] = amtacmConsoleCommand;
555
- consoleCommands['heapdump'] = heapdumpConsoleCommand;
556
- consoleCommands['heapdump2'] = heapdump2ConsoleCommand;
557
- consoleCommands['sms'] = smsConsoleCommand;
558
- consoleCommands['email'] = emailConsoleCommand;
559
- consoleCommands['le'] = leConsoleCommand;
560
- consoleCommands['lecheck'] = lecheckConsoleCommand;
561
- consoleCommands['leevents'] = leeventsConsoleCommand;
562
- consoleCommands['badlogins'] = badloginsConsoleCommand;
563
- consoleCommands['dispatchtable'] = dispatchtableConsoleCommand;
564
- consoleCommands['dupagents'] = dupagentsConsoleCommand;
565
- consoleCommands['agentstats'] = agentstatsConsoleCommand;
566
- consoleCommands['agentissues'] = agentissuesConsoleCommand;
567
- consoleCommands['webstats'] = webstatsConsoleCommand;
568
- consoleCommands['trafficstats'] = trafficstatsConsoleCommand;
569
- consoleCommands['trafficdelta'] = trafficdeltaConsoleCommand;
570
- consoleCommands['watchdog'] = watchdogConsoleCommand;
571
- consoleCommands['acceleratorsstats'] = acceleratorsstatsConsoleCommand;
572
- consoleCommands['mpsstats'] = mpsstatsConsoleCommand;
573
- consoleCommands['mps'] = mpsConsoleCommand;
574
- consoleCommands['dbstats'] = dbstatsConsoleCommand;
575
- consoleCommands['dbcounters'] = dbcountersConsoleCommand;
576
- consoleCommands['serverupdate'] = serverupdateConsoleCommand;
577
- consoleCommands['print'] = printConsoleCommand;
578
- consoleCommands['amtpasswords'] = amtpasswordsConsoleCommand;
579
- consoleCommands['updatecheck'] = updatecheckConsoleCommand;
580
- consoleCommands['maintenance'] = maintenanceConsoleCommand;
581
- consoleCommands['info'] = infoConsoleCommand;
582
- consoleCommands['nodeconfig'] = nodeconfigConsoleCommand;
583
- consoleCommands['versions'] = versionsConsoleCommand;
584
- consoleCommands['args'] = argsConsoleCommand;
585
- consoleCommands['usersessions'] = usersessionsConsoleCommand;
586
- consoleCommands['closeusersessions'] = closeusersessionsConsoleCommand;
587
- consoleCommands['resetserver'] = resetserverConsoleCommand;
588
- consoleCommands['tasklimiter'] = tasklimiterConsoleCommand;
589
- consoleCommands['setmaxtasks'] = setmaxtasksConsoleCommand;
590
- consoleCommands['cores'] = coresConsoleCommand;
591
- consoleCommands['showpaths'] = showpathsConsoleCommand;
592
- consoleCommands['migrationagents'] = migrationagentsConsoleCommand;
593
- consoleCommands['swarmstats'] = swarmstatsConsoleCommand;
594
- consoleCommands['relays'] = relaysConsoleCommand;
595
- consoleCommands['autobackup'] = autobackupConsoleCommand;
596
- consoleCommands['backupconfig'] = backupconfigConsoleCommand;
597
- consoleCommands['firebase'] = firebaseConsoleCommand;
598
-
599
-
600
- function helpConsoleCommand(cmdData) {
601
- var fin = '', f = '', availcommands = 'help,maintenance,info,versions,resetserver,usersessions,closeusersessions,tasklimiter,setmaxtasks,cores,migrationagents,agentstats,agentissues,webstats,trafficstats,trafficdelta,mpsstats,swarmstats,acceleratorsstats,updatecheck,serverupdate,nodeconfig,heapdump,relays,autobackup,backupconfig,dupagents,dispatchtable,badlogins,showpaths,le,lecheck,leevents,dbstats,dbcounters,sms,amtacm,certhashes,watchdog,amtmanager,amtpasswords,certexpire,email';
602
- if (parent.parent.config.settings.heapdump === true) { availcommands += ',heapdump'; }
603
- availcommands = availcommands.split(',').sort();
604
- while (availcommands.length > 0) { if (f.length > 80) { fin += (f + ',\r\n'); f = ''; } f += (((f != '') ? ', ' : ' ') + availcommands.shift()); }
605
- if (f != '') { fin += f; }
606
- if (cmdData.cmdargs['_'].length == 0) {
607
- cmdData.result = 'Available commands: \r\n' + fin + '\r\nType help <command> for details.';
608
- } else {
609
- var cmd2 = cmdData.cmdargs['_'][0].toLowerCase();
610
- switch (cmd2) {
611
- case 'info': { cmdData.result = "info: Returns the most immidiatly useful information about this server, including MeshCentral and NodeJS versions. This is often information required to file a bug."; break; }
612
- case 'versions': { cmdData.result = "versions: Returns all internal versions for NodeJS running this server."; break; }
613
- case 'resetserver': { cmdData.result = "resetserver: Causes the server to reset, this is sometimes useful is the config.json file was changed."; break; }
614
- case 'usersessions': { cmdData.result = "usersessions: Returns a list of active sessions grouped by user."; break; }
615
- case 'closeusersessions': { cmdData.result = "closeusersessions: Disconnects all sessions for a specified user."; break; }
616
- case 'tasklimiter': { cmdData.result = "tasklimiter: Returns the internal status of the tasklimiter. This is a system used to smooth out work done by the server. It's used by, for example, agent updates so that not all agents are updated at the same time."; break; }
617
- case 'serverupdate': { cmdData.result = "serverupdate: Updates server to latest version. Optional version argument to install specific version. Example: serverupdate 0.8.49"; break; }
618
- default: { cmdData.result = 'No help information about this command.'; break; }
619
- }
620
- }
621
- }
622
-
623
- function certexpireConsoleCommand(cmdData) {
624
- const now = Date.now();
625
- for (var i in parent.webCertificateExpire) {
626
- const domainName = (i == '') ? '[Default]' : i;
627
- cmdData.result += domainName + ', expires in ' + Math.floor((parent.webCertificateExpire[i] - now) / 86400000) + ' day(s)\r\n';
628
- }
629
- }
630
-
631
- function webpushConsoleCommand(cmdData) {
632
- if (parent.parent.webpush == null) {
633
- cmdData.result = "Web push not supported.";
634
- } else {
635
- if (cmdData.cmdargs['_'].length != 1) {
636
- cmdData.result = "Usage: WebPush \"Message\"";
637
- } else {
638
- const pushSubscription = { "endpoint": "https://updates.push.services.mozilla.com/wpush/v2/gAAAAABgIkO9hjXHWhMPiuk-ppNRw7r_pUZitddwCEK4ykdzeIxOIjFnYhIt_nr-qUca2mpZziwQsSEhYTUCiuYrhWnVDRweMtiUj16yJJq8V5jneaEaUYjEIe5jp3DOMNpoTm1aHgX74gCR8uTXSITcM97bNi-hRxcQ4f6Ie4WSAmoXpd89B_g", "keys": { "auth": "UB2sbLVK7ALnSHw5P1dahg", "p256dh": "BIoRbcNSxBuTjN39CCCUCHo1f4NxBJ1YDdu_k4MbPW_q3NK1_RufnydUzLPDp8ibBVItSI72-s48QJvOjQ_S8Ok" } }
639
- parent.parent.webpush.sendNotification(pushSubscription, cmdData.cmdargs['_'][0]).then(
640
- function (value) { try { ws.send(JSON.stringify({ action: 'OK', value: cmdData.result, tag: cmdData.command.tag })); } catch (ex) { } },
641
- function (error) { try { ws.send(JSON.stringify({ action: 'Error', value: cmdData.result, tag: cmdData.command.tag })); } catch (ex) { } }
642
- );
643
- }
644
- }
645
- }
646
-
647
- function amtmanagerConsoleCommand(cmdData) {
648
- if (parent.parent.amtManager == null) {
649
- cmdData.result = 'Intel AMT Manager not active.';
650
- } else {
651
- cmdData.result = parent.parent.amtManager.getStatusString();
652
- }
653
- }
654
-
655
- function certhashesConsoleCommand(cmdData) {
656
- cmdData.result += 'AgentCertHash: ' + parent.agentCertificateHashHex;
657
- for (var i in parent.webCertificateHashs) { cmdData.result += '\r\nwebCertificateHash (' + i + '): ' + common.rstr2hex(parent.webCertificateHashs[i]); }
658
- for (var i in parent.webCertificateFullHashs) { cmdData.result += '\r\nwebCertificateFullHash (' + i + '): ' + common.rstr2hex(parent.webCertificateFullHashs[i]); }
659
- cmdData.result += '\r\ndefaultWebCertificateHash: ' + common.rstr2hex(parent.defaultWebCertificateHash);
660
- cmdData.result += '\r\ndefaultWebCertificateFullHash: ' + common.rstr2hex(parent.defaultWebCertificateFullHash);
661
- }
662
-
663
- function amtacmConsoleCommand(cmdData) {
664
- if ((domain.amtacmactivation == null) || (domain.amtacmactivation.acmmatch == null) || (domain.amtacmactivation.acmmatch.length == 0)) {
665
- cmdData.result = 'No Intel AMT activation certificates.';
666
- } else {
667
- if (domain.amtacmactivation.log != null) { cmdData.result += '--- Activation Log ---\r\nFile : ' + domain.amtacmactivation.log + '\r\n'; }
668
- for (var i in domain.amtacmactivation.acmmatch) {
669
- var acmcert = domain.amtacmactivation.acmmatch[i];
670
- cmdData.result += '--- Activation Certificate ' + (parseInt(i) + 1) + ' ---\r\nName : ' + acmcert.cn + '\r\nSHA1 : ' + acmcert.sha1 + '\r\nSHA256: ' + acmcert.sha256 + '\r\n';
671
- }
672
- }
673
- }
674
-
675
- function heapdumpConsoleCommand(cmdData) {
676
- // Heapdump support, see example at:
677
- // https://www.arbazsiddiqui.me/a-practical-guide-to-memory-leaks-in-nodejs/
678
- if (parent.parent.config.settings.heapdump === true) {
679
- var dumpFileName = parent.path.join(parent.parent.datapath, `heapDump-${Date.now()}.heapsnapshot`);
680
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Generating dump file at: " + dumpFileName, tag: cmdData.command.tag })); } catch (ex) { }
681
- require('heapdump').writeSnapshot(dumpFileName, (err, filename) => {
682
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Done.", tag: cmdData.command.tag })); } catch (ex) { }
683
- });
684
- } else {
685
- cmdData.result = "Heapdump not supported, add \"heapdump\":true to settings section of config.json.";
686
- }
687
- }
688
-
689
- function heapdump2ConsoleCommand(cmdData) {
690
- var heapdump = null;
691
- try { heapdump = require('heapdump'); } catch (ex) { }
692
- if (heapdump == null) {
693
- cmdData.result = 'Heapdump module not installed, run "npm install heapdump".';
694
- } else {
695
- heapdump.writeSnapshot(function (err, filename) {
696
- if (err != null) {
697
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: 'Unable to write heapdump: ' + err })); } catch (ex) { }
698
- } else {
699
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: 'Wrote heapdump at ' + filename })); } catch (ex) { }
700
- }
701
- });
702
- }
703
- }
704
-
705
- function smsConsoleCommand(cmdData) {
706
- if (parent.parent.smsserver == null) {
707
- cmdData.result = "No SMS gateway in use.";
708
- } else {
709
- if (cmdData.cmdargs['_'].length != 2) {
710
- cmdData.result = "Usage: SMS \"PhoneNumber\" \"Message\".";
711
- } else {
712
- parent.parent.smsserver.sendSMS(cmdData.cmdargs['_'][0], cmdData.cmdargs['_'][1], function (status, msg) {
713
- if (typeof msg == 'string') {
714
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? ('Success: ' + msg) : ('Failed: ' + msg), tag: cmdData.command.tag })); } catch (ex) { }
715
- } else {
716
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: status ? 'Success' : 'Failed', tag: cmdData.command.tag })); } catch (ex) { }
717
- }
718
- });
719
- }
720
- }
721
- }
722
-
723
- function emailConsoleCommand(cmdData) {
724
- if (domain.mailserver == null) {
725
- cmdData.result = "No email service enabled.";
726
- } else {
727
- if (cmdData.cmdargs['_'].length != 3) {
728
- cmdData.result = "Usage: email \"user@sample.com\" \"Subject\" \"Message\".";
729
- } else {
730
- domain.mailserver.sendMail(cmdData.cmdargs['_'][0], cmdData.cmdargs['_'][1], cmdData.cmdargs['_'][2]);
731
- cmdData.result = "Done.";
732
- }
733
- }
734
- }
735
-
736
- function leConsoleCommand(cmdData) {
737
- if (parent.parent.letsencrypt == null) {
738
- cmdData.result = "Let's Encrypt not in use.";
739
- } else {
740
- cmdData.result = JSON.stringify(parent.parent.letsencrypt.getStats(), null, 4);
741
- }
742
- }
743
-
744
- function lecheckConsoleCommand(cmdData) {
745
- if (parent.parent.letsencrypt == null) {
746
- cmdData.result = "Let's Encrypt not in use.";
747
- } else {
748
- cmdData.result = ["CertOK", "Request:NoCert", "Request:Expire", "Request:MissingNames"][parent.parent.letsencrypt.checkRkenewCertificate()];
749
- }
750
- }
751
-
752
- function leeventsConsoleCommand(cmdData) {
753
- if (parent.parent.letsencrypt == null) {
754
- cmdData.result = "Let's Encrypt not in use.";
755
- } else {
756
- cmdData.result = parent.parent.letsencrypt.events.join('\r\n');
757
- }
758
- }
759
-
760
- function badloginsConsoleCommand(cmdData) {
761
- if (parent.parent.config.settings.maxinvalidlogin == false) {
762
- cmdData.result = 'Bad login filter is disabled.';
763
- } else {
764
- if (cmdData.cmdargs['_'] == 'reset') {
765
- // Reset bad login table
766
- parent.badLoginTable = {};
767
- parent.badLoginTableLastClean = 0;
768
- cmdData.result = 'Done.'
769
- } else if (cmdData.cmdargs['_'] == '') {
770
- // Show current bad login table
771
- if (typeof parent.parent.config.settings.maxinvalidlogin.coolofftime == 'number') {
772
- cmdData.result = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s), " + parent.parent.config.settings.maxinvalidlogin.coolofftime + " minute(s) cooloff.\r\n";
773
- } else {
774
- cmdData.result = "Max is " + parent.parent.config.settings.maxinvalidlogin.count + " bad login(s) in " + parent.parent.config.settings.maxinvalidlogin.time + " minute(s).\r\n";
775
- }
776
- var badLoginCount = 0;
777
- parent.cleanBadLoginTable();
778
- for (var i in parent.badLoginTable) {
779
- badLoginCount++;
780
- if (typeof parent.badLoginTable[i] == 'number') {
781
- cmdData.result += "Cooloff for " + Math.floor((parent.badLoginTable[i] - Date.now()) / 60000) + " minute(s)\r\n";
782
- } else {
783
- if (parent.badLoginTable[i].length > 1) {
784
- cmdData.result += (i + ' - ' + parent.badLoginTable[i].length + " records\r\n");
785
- } else {
786
- cmdData.result += (i + ' - ' + parent.badLoginTable[i].length + " record\r\n");
787
- }
788
- }
789
- }
790
- if (badLoginCount == 0) { cmdData.result += 'No bad logins.'; }
791
- } else {
792
- cmdData.result = 'Usage: badlogin [reset]';
793
- }
794
- }
795
- }
796
-
797
- function dispatchtableConsoleCommand(cmdData) {
798
- for (var i in parent.parent.eventsDispatch) {
799
- cmdData.result += (i + ', ' + parent.parent.eventsDispatch[i].length + '\r\n');
800
- }
801
- }
802
-
803
- function dupagentsConsoleCommand(cmdData) {
804
- for (var i in parent.duplicateAgentsLog) {
805
- cmdData.result += JSON.stringify(parent.duplicateAgentsLog[i]) + '\r\n';
806
- }
807
- if (cmdData.result == '') { cmdData.result = 'No duplicate agents in log.'; }
808
- }
809
-
810
- function agentstatsConsoleCommand(cmdData) {
811
- var stats = parent.getAgentStats();
812
- for (var i in stats) {
813
- if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
814
- }
815
- }
816
-
817
- function agentissuesConsoleCommand(cmdData) {
818
- var stats = parent.getAgentIssues();
819
- if (stats.length == 0) {
820
- cmdData.result = "No agent issues.";
821
- } else {
822
- for (var i in stats) { cmdData.result += stats[i].join(', ') + '\r\n'; }
823
- }
824
- }
825
-
826
- function webstatsConsoleCommand(cmdData) {
827
- var stats = parent.getStats();
828
- for (var i in stats) {
829
- if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
830
- }
831
- }
832
-
833
- function trafficstatsConsoleCommand(cmdData) {
834
- var stats = parent.getTrafficStats();
835
- for (var i in stats) {
836
- if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
837
- }
838
- }
839
-
840
- function trafficdeltaConsoleCommand(cmdData) {
841
- const stats = parent.getTrafficDelta(obj.trafficStats);
842
- obj.trafficStats = stats.current;
843
- for (var i in stats.delta) {
844
- if (typeof stats.delta[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats.delta[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats.delta[i] + '\r\n'); }
845
- }
846
- }
847
-
848
- function watchdogConsoleCommand(cmdData) {
849
- if (parent.parent.watchdog == null) {
850
- cmdData.result = 'Server watchdog not active.';
851
- } else {
852
- cmdData.result = 'Server watchdog active.\r\n';
853
- if (parent.parent.watchdogmaxtime != null) { cmdData.result += 'Largest timeout was ' + parent.parent.watchdogmax + 'ms on ' + parent.parent.watchdogmaxtime + '\r\n'; }
854
- for (var i in parent.parent.watchdogtable) { cmdData.result += parent.parent.watchdogtable[i] + '\r\n'; }
855
- }
856
- }
857
-
858
- function acceleratorsstatsConsoleCommand(cmdData) {
859
- var stats = parent.parent.certificateOperations.getAcceleratorStats();
860
- for (var i in stats) {
861
- if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
862
- }
863
- }
864
-
865
- function mpsstatsConsoleCommand(cmdData) {
866
- if (parent.parent.mpsserver == null) {
867
- cmdData.result = 'MPS not enabled.';
868
- } else {
869
- var stats = parent.parent.mpsserver.getStats();
870
- for (var i in stats) {
871
- if (typeof stats[i] == 'object') { cmdData.result += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { cmdData.result += (i + ': ' + stats[i] + '\r\n'); }
872
- }
873
- }
874
- }
875
-
876
- function mpsConsoleCommand(cmdData) {
877
- if (parent.parent.mpsserver == null) {
878
- cmdData.result = 'MPS not enabled.';
879
- } else {
880
- const connectionTypes = ['CIRA', 'Relay', 'LMS'];
881
- for (var nodeid in parent.parent.mpsserver.ciraConnections) {
882
- cmdData.result += nodeid;
883
- var connections = parent.parent.mpsserver.ciraConnections[nodeid];
884
- for (var i in connections) { cmdData.result += ', ' + connectionTypes[connections[i].tag.connType]; }
885
- cmdData.result += '\r\n';
886
- }
887
- if (cmdData.result == '') { cmdData.result = 'MPS has not connections.'; }
888
- }
889
- }
890
-
891
- function dbstatsConsoleCommand(cmdData) {
892
- parent.parent.db.getStats(function (stats) {
893
- var r2 = '';
894
- for (var i in stats) { r2 += (i + ': ' + stats[i] + '\r\n'); }
895
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: cmdData.command.tag })); } catch (ex) { }
896
- });
897
- }
898
-
899
- function dbcountersConsoleCommand(cmdData) {
900
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: JSON.stringify(parent.parent.db.dbCounters, null, 2), tag: cmdData.command.tag })); } catch (ex) { }
901
- }
902
-
903
- function serverupdateConsoleCommand(cmdData) {
904
- cmdData.result = 'Performing server update...';
905
- var version = null;
906
-
907
- if (cmdData.cmdargs['_'].length > 0) {
908
- version = cmdData.cmdargs['_'][0];
909
-
910
- // This call is SLOW. We only want to validate version if we have to
911
- if (version != 'stable' && version != 'latest') {
912
- parent.parent.getServerVersions((data) => {
913
- var versions = JSON.parse(data);
914
-
915
- if (versions.includes(version)) {
916
- if (parent.parent.performServerUpdate(version) == false) {
917
- try {
918
- ws.send(JSON.stringify({ action: 'serverconsole',
919
- value: 'Server self-update not possible.'}));
920
- } catch (ex) { }
921
- }
922
- } else {
923
- try {
924
- ws.send(JSON.stringify({ action: 'serverconsole',
925
- value: 'Invalid version. Aborting update'}));
926
- } catch (ex) { }
927
- }
928
- });
929
- } else {
930
- if (parent.parent.performServerUpdate(version) == false) {
931
- cmdData.result = 'Server self-update not possible.';
932
- }
933
- }
934
- } else {
935
- if (parent.parent.performServerUpdate(version) == false) {
936
- cmdData.result = 'Server self-update not possible.';
937
- }
938
- }
939
- }
940
-
941
- function printConsoleCommand(cmdData) {
942
- console.log(cmdData.cmdargs['_'][0]);
943
- }
944
-
945
- function amtpasswordsConsoleCommand(cmdData) {
946
- if (parent.parent.amtPasswords == null) {
947
- cmdData.result = "No Intel AMT password table."
948
- } else {
949
- for (var i in parent.parent.amtPasswords) { cmdData.result += (i + ' - ' + parent.parent.amtPasswords[i].join(', ') + '\r\n'); }
950
- }
951
- }
952
-
953
- function updatecheckConsoleCommand(cmdData) {
954
- parent.parent.getServerTags(function (tags, error) {
955
- var r2 = '';
956
- if (error != null) { r2 += 'Exception: ' + error + '\r\n'; }
957
- else { for (var i in tags) { r2 += i + ': ' + tags[i] + '\r\n'; } }
958
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: r2, tag: cmdData.command.tag })); } catch (ex) { }
959
- });
960
- cmdData.result = "Checking server update...";
961
- }
962
-
963
- function maintenanceConsoleCommand(cmdData) {
964
- var arg = null, changed = false;
965
- if ((cmdData.cmdargs['_'] != null) && (cmdData.cmdargs['_'][0] != null)) { arg = cmdData.cmdargs['_'][0].toLowerCase(); }
966
- if (arg == 'enabled') { parent.parent.config.settings.maintenancemode = 1; changed = true; }
967
- else if (arg == 'disabled') { delete parent.parent.config.settings.maintenancemode; changed = true; }
968
- cmdData.result = 'Maintenance mode: ' + ((parent.parent.config.settings.maintenancemode == null) ? 'Disabled' : 'Enabled');
969
- if (changed == false) { cmdData.result += '\r\nTo change type: maintenance [enabled|disabled]'; }
970
- }
971
-
972
- function infoConsoleCommand(cmdData) {
973
- var info = process.memoryUsage();
974
- info.dbType = ['None', 'NeDB', 'MongoJS', 'MongoDB'][parent.db.databaseType];
975
- try { if (parent.parent.multiServer != null) { info.serverId = parent.parent.multiServer.serverid; } } catch (ex) { }
976
- if (parent.db.databaseType == 3) { info.dbChangeStream = parent.db.changeStream; }
977
- if (parent.parent.pluginHandler != null) { info.plugins = []; for (var i in parent.parent.pluginHandler.plugins) { info.plugins.push(i); } }
978
- try { info.nodeVersion = process.version; } catch (ex) { }
979
- try { info.meshVersion = parent.parent.currentVer; } catch (ex) { }
980
- try { info.platform = process.platform; } catch (ex) { }
981
- try { info.arch = process.arch; } catch (ex) { }
982
- try { info.pid = process.pid; } catch (ex) { }
983
- try { info.uptime = process.uptime(); } catch (ex) { }
984
- try { info.cpuUsage = process.cpuUsage(); } catch (ex) { }
985
- try { info.warnings = parent.parent.getServerWarnings(); } catch (ex) { }
986
- try { info.database = ["Unknown", "NeDB", "MongoJS", "MongoDB", "MariaDB", "MySQL"][parent.parent.db.databaseType]; } catch (ex) { }
987
- try { info.productionMode = ((process.env.NODE_ENV != null) && (process.env.NODE_ENV == 'production')); } catch (ex) { }
988
- try { info.allDevGroupManagers = parent.parent.config.settings.managealldevicegroups; } catch (ex) { }
989
- cmdData.result = JSON.stringify(info, null, 4);
990
- }
991
-
992
- function nodeconfigConsoleCommand(cmdData) {
993
- cmdData.result = JSON.stringify(process.config, null, 4);
994
- }
995
-
996
- function versionsConsoleCommand(cmdData) {
997
- cmdData.result = JSON.stringify(process.versions, null, 4);
998
- }
999
-
1000
- function argsConsoleCommand(cmdData) {
1001
- cmdData.result = 'args: ' + JSON.stringify(cmdData.cmdargs);
1002
- }
1003
-
1004
- function usersessionsConsoleCommand(cmdData) {
1005
- var userSessionCount = 0;
1006
- var filter = null;
1007
- var arg = cmdData.cmdargs['_'][0];
1008
- if (typeof arg == 'string') { if (arg.indexOf('/') >= 0) { filter = arg; } else { filter = ('user/' + domain.id + '/' + arg); } }
1009
- for (var i in parent.wssessions) {
1010
- if ((filter == null) || (filter == i)) {
1011
- userSessionCount++;
1012
- cmdData.result += (i + ', ' + parent.wssessions[i].length + ' session' + ((parent.wssessions[i].length > 1) ? 's' : '') + '.\r\n');
1013
- for (var j in parent.wssessions[i]) {
1014
- cmdData.result += ' ' + parent.wssessions[i][j].clientIp + ' --> ' + parent.wssessions[i][j].sessionId + '\r\n';
1015
- }
1016
- }
1017
- }
1018
- if (userSessionCount == 0) { cmdData.result = 'None.'; }
1019
- }
1020
-
1021
- function closeusersessionsConsoleCommand(cmdData) {
1022
- var userSessionCount = 0;
1023
- var filter = null;
1024
- var arg = cmdData.cmdargs['_'][0];
1025
- if (typeof arg == 'string') { if (arg.indexOf('/') >= 0) { filter = arg; } else { filter = ('user/' + domain.id + '/' + arg); } }
1026
- if (filter == null) {
1027
- cmdData.result += "Usage: closeusersessions <username>";
1028
- } else {
1029
- cmdData.result += "Closing user sessions for: " + filter + '\r\n';
1030
- for (var i in parent.wssessions) {
1031
- if (filter == i) {
1032
- userSessionCount++;
1033
- for (var j in parent.wssessions[i]) {
1034
- parent.wssessions[i][j].send(JSON.stringify({ action: 'stopped', msg: "Administrator forced disconnection" }));
1035
- parent.wssessions[i][j].close();
1036
- }
1037
- }
1038
- }
1039
- if (userSessionCount < 2) { cmdData.result += 'Disconnected ' + userSessionCount + ' session.'; } else { cmdData.result += 'Disconnected ' + userSessionCount + ' sessions.'; };
1040
- }
1041
- }
1042
-
1043
- function resetserverConsoleCommand(cmdData) {
1044
- console.log("Server restart...");
1045
- process.exit(0);
1046
- }
1047
-
1048
- function tasklimiterConsoleCommand(cmdData) {
1049
- if (parent.parent.taskLimiter != null) {
1050
- //var obj = { maxTasks: maxTasks, maxTaskTime: (maxTaskTime * 1000), nextTaskId: 0, currentCount: 0, current: {}, pending: [[], [], []], timer: null };
1051
- const tl = parent.parent.taskLimiter;
1052
- cmdData.result += 'MaxTasks: ' + tl.maxTasks + ', NextTaskId: ' + tl.nextTaskId + '\r\n';
1053
- cmdData.result += 'MaxTaskTime: ' + (tl.maxTaskTime / 1000) + ' seconds, Timer: ' + (tl.timer != null) + '\r\n';
1054
- var c = [];
1055
- for (var i in tl.current) { c.push(i); }
1056
- cmdData.result += 'Current (' + tl.currentCount + '): [' + c.join(', ') + ']\r\n';
1057
- cmdData.result += 'Pending (High/Med/Low): ' + tl.pending[0].length + ', ' + tl.pending[1].length + ', ' + tl.pending[2].length + '\r\n';
1058
- }
1059
- }
1060
-
1061
- function setmaxtasksConsoleCommand(cmdData) {
1062
- if ((cmdData.cmdargs["_"].length != 1) || (parseInt(cmdData.cmdargs["_"][0]) < 1) || (parseInt(cmdData.cmdargs["_"][0]) > 1000)) {
1063
- cmdData.result = 'Usage: setmaxtasks [1 to 1000]';
1064
- } else {
1065
- parent.parent.taskLimiter.maxTasks = parseInt(cmdData.cmdargs["_"][0]);
1066
- cmdData.result = 'MaxTasks set to ' + parent.parent.taskLimiter.maxTasks + '.';
1067
- }
1068
- }
1069
-
1070
- function coresConsoleCommand(cmdData) {
1071
- if (parent.parent.defaultMeshCores != null) {
1072
- for (var i in parent.parent.defaultMeshCores) {
1073
- cmdData.result += i + ': ' + parent.parent.defaultMeshCores[i].length + ' bytes\r\n';
1074
- }
1075
- }
1076
- }
1077
-
1078
- function showpathsConsoleCommand(cmdData) {
1079
- cmdData.result = 'Parent: ' + parent.parent.parentpath + '\r\n';
1080
- cmdData.result += 'Data: ' + parent.parent.datapath + '\r\n';
1081
- cmdData.result += 'Files: ' + parent.parent.filespath + '\r\n';
1082
- cmdData.result += 'Backup: ' + parent.parent.backuppath + '\r\n';
1083
- cmdData.result += 'Record: ' + parent.parent.recordpath + '\r\n';
1084
- cmdData.result += 'WebPublic: ' + parent.parent.webPublicPath + '\r\n';
1085
- cmdData.result += 'WebViews: ' + parent.parent.webViewsPath + '\r\n';
1086
- if (parent.parent.webViewsOverridePath) { cmdData.result += 'XWebPublic: ' + parent.parent.webViewsOverridePath + '\r\n'; }
1087
- if (parent.parent.webViewsOverridePath) { cmdData.result += 'XWebViews: ' + parent.parent.webPublicOverridePath + '\r\n'; }
1088
- }
1089
-
1090
- function migrationagentsConsoleCommand(cmdData) {
1091
- if (parent.parent.swarmserver == null) {
1092
- cmdData.result = 'Swarm server not running.';
1093
- } else {
1094
- for (var i in parent.parent.swarmserver.migrationAgents) {
1095
- var arch = parent.parent.swarmserver.migrationAgents[i];
1096
- for (var j in arch) { var agent = arch[j]; cmdData.result += 'Arch ' + agent.arch + ', Ver ' + agent.ver + ', Size ' + ((agent.binary == null) ? 0 : agent.binary.length) + '<br />'; }
1097
- }
1098
- }
1099
- }
1100
-
1101
- function swarmstatsConsoleCommand(cmdData) {
1102
- if (parent.parent.swarmserver == null) {
1103
- cmdData.result = 'Swarm server not running.';
1104
- } else {
1105
- for (var i in parent.parent.swarmserver.stats) {
1106
- if (typeof parent.parent.swarmserver.stats[i] == 'object') {
1107
- cmdData.result += i + ': ' + JSON.stringify(parent.parent.swarmserver.stats[i]) + '\r\n';
1108
- } else {
1109
- cmdData.result += i + ': ' + parent.parent.swarmserver.stats[i] + '\r\n';
1110
- }
1111
- }
1112
- }
1113
- }
1114
-
1115
- function relaysConsoleCommand(cmdData) {
1116
- for (var i in parent.wsrelays) {
1117
- cmdData.result += 'id: ' + i + ', ' + ((parent.wsrelays[i].state == 2) ? 'connected' : 'pending');
1118
- if (parent.wsrelays[i].peer1 != null) {
1119
- cmdData.result += ', ' + cleanRemoteAddr(parent.wsrelays[i].peer1.req.clientIp);
1120
- if (parent.wsrelays[i].peer1.user) { cmdData.result += ' (User:' + parent.wsrelays[i].peer1.user.name + ')' }
1121
- }
1122
- if (parent.wsrelays[i].peer2 != null) {
1123
- cmdData.result += ' to ' + cleanRemoteAddr(parent.wsrelays[i].peer2.req.clientIp);
1124
- if (parent.wsrelays[i].peer2.user) { cmdData.result += ' (User:' + parent.wsrelays[i].peer2.user.name + ')' }
1125
- }
1126
- cmdData.result += '\r\n';
1127
- }
1128
- if (cmdData.result == '') { cmdData.result = 'No relays.'; }
1129
- }
1130
-
1131
- function autobackupConsoleCommand(cmdData) {
1132
- var backupResult = parent.db.performBackup(function (msg) {
1133
- try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { }
1134
- });
1135
- if (backupResult == 0) { cmdData.result = 'Starting auto-backup...'; } else { cmdData.result = 'Backup alreay in progress.'; }
1136
- }
1137
-
1138
- function backupconfigConsoleCommand(cmdData) {
1139
- cmdData.result = parent.db.getBackupConfig();
1140
- }
1141
-
1142
- function firebaseConsoleCommand(cmdData) {
1143
- if (parent.parent.firebase == null) {
1144
- cmdData.result = "Firebase push messaging not supported";
1145
- } else {
1146
- cmdData.result = JSON.stringify(parent.parent.firebase.stats, null, 2);
1147
- }
1148
- }
1149
-
547
// Process incoming web socket data from the browser
548
function processWebSocketData(msg) {
549
var command, i = 0, mesh = null, meshid = null, nodeid = null, meshlinks = null, change = 0;