Added database import/export

Ylian Saint-Hilaire committed Sep 1, 2017 at 17:34 UTC 789c5ef18510f55c014ae8d37be188e3788c5516
6 files changed +69 -26
agents/MeshService.exe
Binary files a/agents/MeshService.exe and b/agents/MeshService.exe differ
agents/MeshService64.exe
Binary files a/agents/MeshService64.exe and b/agents/MeshService64.exe differ
agents/meshcore.js
+42 -21
@@ -60,13 +60,20 @@ function createMeshCore(agent) {
60 headers: { Host: "ipinfo.io" }
61 },
62 function (resp) {
63 - var geoData = '';
64 - resp.data = function (geoipdata) { geoData += geoipdata; };
65 - resp.end = function () {
66 - var location = null;
67 - try { if (typeof geoData == 'string') { var result = JSON.parse(geoData); if (result.ip && result.loc) { location = result; } } } catch (e) { }
68 - if (func) { getIpLocationDataExCounts[1]++; func(location); }
69 - }
63 + if (resp.statusCode == 200) {
64 + var geoData = '';
65 + resp.data = function (geoipdata) { geoData += geoipdata; };
66 + resp.end = function () {
67 + var location = null;
68 + try {
69 + if (typeof geoData == 'string') {
70 + var result = JSON.parse(geoData);
71 + if (result.ip && result.loc) { location = result; }
72 + }
73 + } catch (e) { }
74 + if (func) { getIpLocationDataExCounts[1]++; func(location); }
75 + }
76 + } else { func(null); }
77 getIpLocationDataExInProgress = false;
78 }).end();
79 return true;
@@ -89,11 +96,15 @@ function createMeshCore(agent) {
96 if (publicLocationInfo == null) {
97 // Nothing in the cache, fetch the data
98 getIpLocationDataEx(function (locationData) {
92 - publicLocationInfo = {};
93 - publicLocationInfo.netInfoStr = lastNetworkInfo;
94 - publicLocationInfo.locationData = locationData;
95 - var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
96 - if (func) func(locationData);
99 + if (locationData != null) {
100 + publicLocationInfo = {};
101 + publicLocationInfo.netInfoStr = lastNetworkInfo;
102 + publicLocationInfo.locationData = locationData;
103 + var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
104 + if (func) func(locationData); // Report the new location
105 + } else {
106 + if (func) func(null); // Report no location
107 + }
108 });
109 } else {
110 // Check the cache
@@ -103,11 +114,15 @@ function createMeshCore(agent) {
114 } else {
115 // Cache mismatch
116 getIpLocationDataEx(function (locationData) {
106 - publicLocationInfo = {};
107 - publicLocationInfo.netInfoStr = lastNetworkInfo;
108 - publicLocationInfo.locationData = locationData;
109 - var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
110 - if (func) func(locationData);
117 + if (locationData != null) {
118 + publicLocationInfo = {};
119 + publicLocationInfo.netInfoStr = lastNetworkInfo;
120 + publicLocationInfo.locationData = locationData;
121 + var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
122 + if (func) func(locationData); // Report the new location
123 + } else {
124 + if (func) func(publicLocationInfo.locationData); // Can't get new location, report the old location
125 + }
126 });
127 }
128 }
@@ -539,7 +554,7 @@ function createMeshCore(agent) {
554 var response = null;
555 switch (cmd) {
556 case 'help': { // Displays available commands
542 - response = 'Available commands: help, info, args, print, type, dbget, dbset, dbcompact, parseurl, httpget, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power.';
557 + response = 'Available commands: help, info, args, print, type, dbget, dbset, dbcompact, parseurl, httpget, wslist, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power.';
558 break;
559 }
560 case 'notify': { // Send a notification message to the mesh
@@ -654,7 +669,9 @@ function createMeshCore(agent) {
669 response = 'Proper usage: wsconnect (url)\r\nFor example: wsconnect wss://localhost:443/meshrelay.ashx?id=abc'; // Display correct command usage
670 } else {
671 var httprequest = null;
657 - try { http.request(parseUrl(args['_'][0])); } catch (e) { response = 'Invalid HTTP websocket request'; }
672 + try {
673 + httprequest = http.request(parseUrl(args['_'][0]));
674 + } catch (e) { response = 'Invalid HTTP websocket request'; }
675 if (httprequest != null) {
676 httprequest.upgrade = onWebSocketUpgrade;
677
@@ -695,7 +712,7 @@ function createMeshCore(agent) {
712 var i = parseInt(args['_'][0]);
713 var httprequest = consoleWebSockets[i];
714 if (httprequest != undefined) {
698 - httprequest.s.end();
715 + if (httprequest.s != null) { httprequest.s.end(); } else { httprequest.end(); }
716 response = 'ok';
717 } else {
718 response = 'Invalid web socket number';
@@ -747,7 +764,11 @@ function createMeshCore(agent) {
764 break;
765 }
766 case 'location': { // Get location information about this computer
750 - getIpLocationData(function (location) { sendConsoleText('IpLocation: ' + getIpLocationDataExCounts[0] + ' querie(s), ' + getIpLocationDataExCounts[1] + ' response(s), inProgress: ' + getIpLocationDataExInProgress + "\r\nPublic IP location data:\r\n" + objToString(location, 0, '.'), sessionid); }, args['_'][0]);
767 + if (args['_'][0] == 'force') {
768 + getIpLocationDataEx(function (location) { sendConsoleText('IpLocation: ' + getIpLocationDataExCounts[0] + ' querie(s), ' + getIpLocationDataExCounts[1] + ' response(s), inProgress: ' + getIpLocationDataExInProgress + "\r\nPublic IP location data:\r\n" + objToString(location, 0, '.'), sessionid); }, args['_'][0]);
769 + } else {
770 + getIpLocationData(function (location) { sendConsoleText('IpLocation: ' + getIpLocationDataExCounts[0] + ' querie(s), ' + getIpLocationDataExCounts[1] + ' response(s), inProgress: ' + getIpLocationDataExInProgress + "\r\nPublic IP location data:\r\n" + objToString(location, 0, '.'), sessionid); }, args['_'][0]);
771 + }
772 break;
773 }
774 case 'power': { // Execute a power action on this computer
db.js
+3
@@ -145,11 +145,14 @@ module.exports.CreateDB = function (args, datapath) {
145
146 obj.Set = function (data) { obj.file.update({ _id: data._id }, data, { upsert: true }); }
147 obj.Get = function (id, func) { obj.file.find({ _id: id }, func); }
148 + obj.GetAll = function (func) { obj.file.find({}, func); }
149 obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }, { type : 0 }, func); }
150 obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, domain, type, func) { obj.file.find({ type: type, domain: domain, meshid: { $in: meshes } }, { type : 0 }, func); }
151 obj.GetAllType = function (type, func) { obj.file.find({ type: type }, func); }
152 obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.find({ type: type, domain: domain, _id: { $in: ids } }, func); }
153 obj.Remove = function (id) { obj.file.remove({ _id: id }); }
154 + obj.RemoveAll = function (func) { obj.file.remove({}, { multi: true }, func); }
155 + obj.InsertMany = function (data, func) { obj.file.insert(data, func); }
156 obj.StoreEvent = function (ids, source, event) { obj.file.insert(event); }
157 obj.GetEvents = function (ids, domain, func) { if (obj.databaseType == 1) { obj.file.find({ type: 'event', domain: domain, ids: { $in: ids } }, { type: 0, _id: 0 }).sort({ time: -1 }).exec(func); } else { obj.file.find({ type: 'event', domain: domain, ids: { $in: ids } }, { type: 0, _id: 0 }).sort({ time: -1 }, func) } }
158 obj.RemoveMesh = function (id) { obj.file.remove({ mesh: id }, { multi: true }); obj.file.remove({ _id: id }); }
meshcentral.js
+23 -4
@@ -54,7 +54,7 @@ function CreateMeshCentralServer() {
54 try { require('./pass').hash('test', function () { }); } catch (e) { console.log('Old version of node, must upgrade.'); return; } // TODO: Not sure if this test works or not.
55
56 // Check for invalid arguments
57 - var validArguments = ['_', 'notls', 'user', 'port', 'mpsport', 'redirport', 'cert', 'deletedomain', 'deletedefaultdomain', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn'];
57 + var validArguments = ['_', 'notls', 'user', 'port', 'mpsport', 'redirport', 'cert', 'deletedomain', 'deletedefaultdomain', 'showusers', 'shownodes', 'showmeshes', 'showevents', 'showpower', 'help', 'exactports', 'install', 'uninstall', 'start', 'stop', 'restart', 'debug', 'filespath', 'datapath', 'noagentupdate', 'launch', 'noserverbackup', 'mongodb', 'mongodbcol', 'wanonly', 'lanonly', 'nousers', 'mpsdebug', 'mpspass', 'ciralocalfqdn', 'dbexport', 'dbimport'];
58 for (var arg in obj.args) { if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
59 if (obj.args.mongodb == true) { console.log('Must specify: --mongodb [connectionstring] \r\nSee https://docs.mongodb.com/manual/reference/connection-string/ for MongoDB connection string.'); return; }
60
@@ -121,12 +121,15 @@ function CreateMeshCentralServer() {
121 if (xprocess.xrestart == true) {
122 setTimeout(function () { obj.launchChildServer(startLine); }, 500); // If exit with restart requested, restart the server.
123 } else {
124 - if (error != null) { console.log('ERROR: Unable to start MeshCentral: ' + error); process.exit(); }
124 + if (error != null) {
125 + console.log('ERROR: MeshCentral failed with critical error, restarting...');
126 + setTimeout(function () { obj.launchChildServer(startLine); }, 1000);
127 + }
128 }
129 });
130 xprocess.stdout.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } if (data.indexOf('Updating settings folder...') >= 0) { xprocess.xrestart = true; } console.log(data); });
128 - xprocess.stderr.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } console.error(data); });
129 - xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { console.log("Exited with code " + code); } });
131 + xprocess.stderr.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } obj.fs.appendFileSync('mesherrors.txt', '-------- ' + new Date().toLocaleString() + ' --------\r\n\r\n' + data + '\r\n\r\n\r\n'); });
132 + xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { /* console.log("Exited with code " + code); */ } });
133 }
134
135 // Get current and latest MeshCentral server versions using NPM
@@ -201,6 +204,22 @@ function CreateMeshCentralServer() {
204 if (obj.args.showmeshes) { obj.db.GetAllType('mesh', function (err, docs) { console.log(docs); process.exit(); }); return; }
205 if (obj.args.showevents) { obj.db.GetAllType('event', function (err, docs) { console.log(docs); process.exit(); }); return; }
206 if (obj.args.showpower) { obj.db.GetAllType('power', function (err, docs) { console.log(docs); process.exit(); }); return; }
207 + if (obj.args.dbexport) {
208 + // Export the entire database to a JSON file
209 + if (obj.args.dbexport == true) { console.log('Use --dbexport [filename]'); process.exit(); } else { obj.db.GetAll(function (err, docs) { obj.fs.writeFileSync(obj.args.dbexport, JSON.stringify(docs)); console.log('Exported ' + docs.length + ' document(s).'); process.exit(); }); }
210 + return;
211 + }
212 + if (obj.args.dbimport) {
213 + // Import the entire database from a JSON file
214 + if (obj.args.dbimport == true) { console.log('Use --dbimport [filename]'); process.exit(); } else {
215 + var json = null;
216 + try { json = obj.fs.readFileSync(obj.args.dbimport); } catch (e) { console.log('Invalid JSON file'); process.exit(); }
217 + try { json = JSON.parse(json); } catch (e) { console.log('Invalid JSON format'); process.exit(); }
218 + if ((json == null) || (typeof json.length != 'number') || (json.length < 1)) { console.log('Invalid JSON format'); }
219 + obj.db.RemoveAll(function () { obj.db.InsertMany(json, function () { console.log('Imported ' + json.length + ' document(s)'); process.exit(); }); });
220 + }
221 + return;
222 + }
223
224 // Clear old event entries and power entires
225 obj.db.clearOldEntries('event', 30); // Clear all event entires that are older than 30 days.
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.0.6-y",
3 + "version": "0.0.7-d",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",