mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-22 13:21:31 +03:00
Added database import/export
This commit is contained in:
parent
362b23c088
commit
789c5ef185
Binary file not shown.
Binary file not shown.
@ -60,13 +60,20 @@ function createMeshCore(agent) {
|
||||
headers: { Host: "ipinfo.io" }
|
||||
},
|
||||
function (resp) {
|
||||
if (resp.statusCode == 200) {
|
||||
var geoData = '';
|
||||
resp.data = function (geoipdata) { geoData += geoipdata; };
|
||||
resp.end = function () {
|
||||
var location = null;
|
||||
try { if (typeof geoData == 'string') { var result = JSON.parse(geoData); if (result.ip && result.loc) { location = result; } } } catch (e) { }
|
||||
try {
|
||||
if (typeof geoData == 'string') {
|
||||
var result = JSON.parse(geoData);
|
||||
if (result.ip && result.loc) { location = result; }
|
||||
}
|
||||
} catch (e) { }
|
||||
if (func) { getIpLocationDataExCounts[1]++; func(location); }
|
||||
}
|
||||
} else { func(null); }
|
||||
getIpLocationDataExInProgress = false;
|
||||
}).end();
|
||||
return true;
|
||||
@ -89,11 +96,15 @@ function createMeshCore(agent) {
|
||||
if (publicLocationInfo == null) {
|
||||
// Nothing in the cache, fetch the data
|
||||
getIpLocationDataEx(function (locationData) {
|
||||
if (locationData != null) {
|
||||
publicLocationInfo = {};
|
||||
publicLocationInfo.netInfoStr = lastNetworkInfo;
|
||||
publicLocationInfo.locationData = locationData;
|
||||
var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
|
||||
if (func) func(locationData);
|
||||
if (func) func(locationData); // Report the new location
|
||||
} else {
|
||||
if (func) func(null); // Report no location
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Check the cache
|
||||
@ -103,11 +114,15 @@ function createMeshCore(agent) {
|
||||
} else {
|
||||
// Cache mismatch
|
||||
getIpLocationDataEx(function (locationData) {
|
||||
if (locationData != null) {
|
||||
publicLocationInfo = {};
|
||||
publicLocationInfo.netInfoStr = lastNetworkInfo;
|
||||
publicLocationInfo.locationData = locationData;
|
||||
var x = db.Put('publicLocationInfo', JSON.stringify(publicLocationInfo)); // Save to database
|
||||
if (func) func(locationData);
|
||||
if (func) func(locationData); // Report the new location
|
||||
} else {
|
||||
if (func) func(publicLocationInfo.locationData); // Can't get new location, report the old location
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -539,7 +554,7 @@ function createMeshCore(agent) {
|
||||
var response = null;
|
||||
switch (cmd) {
|
||||
case 'help': { // Displays available commands
|
||||
response = 'Available commands: help, info, args, print, type, dbget, dbset, dbcompact, parseurl, httpget, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power.';
|
||||
response = 'Available commands: help, info, args, print, type, dbget, dbset, dbcompact, parseurl, httpget, wslist, wsconnect, wssend, wsclose, notify, ls, amt, netinfo, location, power.';
|
||||
break;
|
||||
}
|
||||
case 'notify': { // Send a notification message to the mesh
|
||||
@ -654,7 +669,9 @@ function createMeshCore(agent) {
|
||||
response = 'Proper usage: wsconnect (url)\r\nFor example: wsconnect wss://localhost:443/meshrelay.ashx?id=abc'; // Display correct command usage
|
||||
} else {
|
||||
var httprequest = null;
|
||||
try { http.request(parseUrl(args['_'][0])); } catch (e) { response = 'Invalid HTTP websocket request'; }
|
||||
try {
|
||||
httprequest = http.request(parseUrl(args['_'][0]));
|
||||
} catch (e) { response = 'Invalid HTTP websocket request'; }
|
||||
if (httprequest != null) {
|
||||
httprequest.upgrade = onWebSocketUpgrade;
|
||||
|
||||
@ -695,7 +712,7 @@ function createMeshCore(agent) {
|
||||
var i = parseInt(args['_'][0]);
|
||||
var httprequest = consoleWebSockets[i];
|
||||
if (httprequest != undefined) {
|
||||
httprequest.s.end();
|
||||
if (httprequest.s != null) { httprequest.s.end(); } else { httprequest.end(); }
|
||||
response = 'ok';
|
||||
} else {
|
||||
response = 'Invalid web socket number';
|
||||
@ -747,7 +764,11 @@ function createMeshCore(agent) {
|
||||
break;
|
||||
}
|
||||
case 'location': { // Get location information about this computer
|
||||
if (args['_'][0] == 'force') {
|
||||
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]);
|
||||
} else {
|
||||
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]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'power': { // Execute a power action on this computer
|
||||
|
3
db.js
3
db.js
@ -145,11 +145,14 @@ module.exports.CreateDB = function (args, datapath) {
|
||||
|
||||
obj.Set = function (data) { obj.file.update({ _id: data._id }, data, { upsert: true }); }
|
||||
obj.Get = function (id, func) { obj.file.find({ _id: id }, func); }
|
||||
obj.GetAll = function (func) { obj.file.find({}, func); }
|
||||
obj.GetAllTypeNoTypeField = function (type, domain, func) { obj.file.find({ type: type, domain: domain }, { type : 0 }, func); }
|
||||
obj.GetAllTypeNoTypeFieldMeshFiltered = function (meshes, domain, type, func) { obj.file.find({ type: type, domain: domain, meshid: { $in: meshes } }, { type : 0 }, func); }
|
||||
obj.GetAllType = function (type, func) { obj.file.find({ type: type }, func); }
|
||||
obj.GetAllIdsOfType = function (ids, domain, type, func) { obj.file.find({ type: type, domain: domain, _id: { $in: ids } }, func); }
|
||||
obj.Remove = function (id) { obj.file.remove({ _id: id }); }
|
||||
obj.RemoveAll = function (func) { obj.file.remove({}, { multi: true }, func); }
|
||||
obj.InsertMany = function (data, func) { obj.file.insert(data, func); }
|
||||
obj.StoreEvent = function (ids, source, event) { obj.file.insert(event); }
|
||||
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) } }
|
||||
obj.RemoveMesh = function (id) { obj.file.remove({ mesh: id }, { multi: true }); obj.file.remove({ _id: id }); }
|
||||
|
@ -54,7 +54,7 @@ function CreateMeshCentralServer() {
|
||||
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.
|
||||
|
||||
// Check for invalid arguments
|
||||
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'];
|
||||
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'];
|
||||
for (var arg in obj.args) { if (validArguments.indexOf(arg.toLocaleLowerCase()) == -1) { console.log('Invalid argument "' + arg + '", use --help.'); return; } }
|
||||
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; }
|
||||
|
||||
@ -121,12 +121,15 @@ function CreateMeshCentralServer() {
|
||||
if (xprocess.xrestart == true) {
|
||||
setTimeout(function () { obj.launchChildServer(startLine); }, 500); // If exit with restart requested, restart the server.
|
||||
} else {
|
||||
if (error != null) { console.log('ERROR: Unable to start MeshCentral: ' + error); process.exit(); }
|
||||
if (error != null) {
|
||||
console.log('ERROR: MeshCentral failed with critical error, restarting...');
|
||||
setTimeout(function () { obj.launchChildServer(startLine); }, 1000);
|
||||
}
|
||||
}
|
||||
});
|
||||
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); });
|
||||
xprocess.stderr.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } console.error(data); });
|
||||
xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { console.log("Exited with code " + code); } });
|
||||
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'); });
|
||||
xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { /* console.log("Exited with code " + code); */ } });
|
||||
}
|
||||
|
||||
// Get current and latest MeshCentral server versions using NPM
|
||||
@ -201,6 +204,22 @@ function CreateMeshCentralServer() {
|
||||
if (obj.args.showmeshes) { obj.db.GetAllType('mesh', function (err, docs) { console.log(docs); process.exit(); }); return; }
|
||||
if (obj.args.showevents) { obj.db.GetAllType('event', function (err, docs) { console.log(docs); process.exit(); }); return; }
|
||||
if (obj.args.showpower) { obj.db.GetAllType('power', function (err, docs) { console.log(docs); process.exit(); }); return; }
|
||||
if (obj.args.dbexport) {
|
||||
// Export the entire database to a JSON file
|
||||
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(); }); }
|
||||
return;
|
||||
}
|
||||
if (obj.args.dbimport) {
|
||||
// Import the entire database from a JSON file
|
||||
if (obj.args.dbimport == true) { console.log('Use --dbimport [filename]'); process.exit(); } else {
|
||||
var json = null;
|
||||
try { json = obj.fs.readFileSync(obj.args.dbimport); } catch (e) { console.log('Invalid JSON file'); process.exit(); }
|
||||
try { json = JSON.parse(json); } catch (e) { console.log('Invalid JSON format'); process.exit(); }
|
||||
if ((json == null) || (typeof json.length != 'number') || (json.length < 1)) { console.log('Invalid JSON format'); }
|
||||
obj.db.RemoveAll(function () { obj.db.InsertMany(json, function () { console.log('Imported ' + json.length + ' document(s)'); process.exit(); }); });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear old event entries and power entires
|
||||
obj.db.clearOldEntries('event', 30); // Clear all event entires that are older than 30 days.
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "meshcentral",
|
||||
"version": "0.0.6-y",
|
||||
"version": "0.0.7-d",
|
||||
"keywords": [
|
||||
"Remote Management",
|
||||
"Intel AMT",
|
||||
|
Loading…
Reference in New Issue
Block a user