mirror of
				https://github.com/Ylianst/MeshCentral.git
				synced 2025-03-09 15:40:18 +00:00 
			
		
		
		
	Improved state-less server code (#4645)
This commit is contained in:
		
							parent
							
								
									ab02ffad4d
								
							
						
					
					
						commit
						cc7670cc31
					
				
					 2 changed files with 17 additions and 86 deletions
				
			
		
							
								
								
									
										99
									
								
								db.js
									
										
									
									
									
								
							
							
						
						
									
										99
									
								
								db.js
									
										
									
									
									
								
							| 
						 | 
					@ -1542,20 +1542,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
            // List all configuration files
 | 
					            // List all configuration files
 | 
				
			||||||
            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
					            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get all configuration files (TODO: This is not SQL)
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
 | 
					 | 
				
			||||||
                    if (err != null) { func(null); return; }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get database information (TODO: Complete this)
 | 
					            // Get database information (TODO: Complete this)
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
                obj.stats = { c: 4 };
 | 
					                obj.stats = { c: 4 };
 | 
				
			||||||
| 
						 | 
					@ -1796,21 +1782,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get all configuration files
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
 | 
					 | 
				
			||||||
                    const docs = [];
 | 
					 | 
				
			||||||
                    for (var i in snapshots) { docs.push(snapshots[i].val()); }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get database information
 | 
					            // Get database information
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
                obj.stats = { c: 5 };
 | 
					                obj.stats = { c: 5 };
 | 
				
			||||||
| 
						 | 
					@ -1983,20 +1954,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
            // List all configuration files
 | 
					            // List all configuration files
 | 
				
			||||||
            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
					            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get all configuration files
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
 | 
					 | 
				
			||||||
                    if (err != null) { func(null); return; }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get database information (TODO: Complete this)
 | 
					            // Get database information (TODO: Complete this)
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
                obj.stats = { c: 4 };
 | 
					                obj.stats = { c: 4 };
 | 
				
			||||||
| 
						 | 
					@ -2166,20 +2123,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // List all configuration files
 | 
					            // List all configuration files
 | 
				
			||||||
            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
					            obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }
 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get all configuration files
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
 | 
					 | 
				
			||||||
                    if (err != null) { func(null); return; }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            // Get database information (TODO: Complete this)
 | 
					            // Get database information (TODO: Complete this)
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
| 
						 | 
					@ -2451,20 +2394,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
            // List all configuration files
 | 
					            // List all configuration files
 | 
				
			||||||
            obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); }
 | 
					            obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get all configuration files
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
 | 
					 | 
				
			||||||
                    if (err != null) { func(null); return; }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get database information
 | 
					            // Get database information
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
                obj.stats = { c: 6 };
 | 
					                obj.stats = { c: 6 };
 | 
				
			||||||
| 
						 | 
					@ -2635,20 +2564,6 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
            // List all configuration files
 | 
					            // List all configuration files
 | 
				
			||||||
            obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); }
 | 
					            obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Get all configuration files
 | 
					 | 
				
			||||||
            obj.getAllConfigFiles = function (password, func) {
 | 
					 | 
				
			||||||
                obj.file.find({ type: 'cfile' }, function (err, docs) {
 | 
					 | 
				
			||||||
                    if (err != null) { func(null); return; }
 | 
					 | 
				
			||||||
                    var r = null;
 | 
					 | 
				
			||||||
                    for (var i = 0; i < docs.length; i++) {
 | 
					 | 
				
			||||||
                        var name = docs[i]._id.split('/')[1];
 | 
					 | 
				
			||||||
                        var data = obj.decryptData(password, docs[i].data);
 | 
					 | 
				
			||||||
                        if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    func(r);
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Get database information
 | 
					            // Get database information
 | 
				
			||||||
            obj.getDbStats = function (func) {
 | 
					            obj.getDbStats = function (func) {
 | 
				
			||||||
                obj.stats = { c: 5 };
 | 
					                obj.stats = { c: 5 };
 | 
				
			||||||
| 
						 | 
					@ -2682,6 +2597,20 @@ module.exports.CreateDB = function (parent, func) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Get all configuration files
 | 
				
			||||||
 | 
					        obj.getAllConfigFiles = function (password, func) {
 | 
				
			||||||
 | 
					            obj.GetAllType('cfile', function (err, docs) {
 | 
				
			||||||
 | 
					                if (err != null) { func(null); return; }
 | 
				
			||||||
 | 
					                var r = null;
 | 
				
			||||||
 | 
					                for (var i = 0; i < docs.length; i++) {
 | 
				
			||||||
 | 
					                    var name = docs[i]._id.split('/')[1];
 | 
				
			||||||
 | 
					                    var data = obj.decryptData(password, docs[i].data);
 | 
				
			||||||
 | 
					                    if (data != null) { if (r == null) { r = {}; } r[name] = data; }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                func(r);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        func(obj); // Completed function setup
 | 
					        func(obj); // Completed function setup
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1189,11 +1189,13 @@ function CreateMeshCentralServer(config, args) {
 | 
				
			||||||
                        obj.db.getAllConfigFiles(key, function (configFiles) {
 | 
					                        obj.db.getAllConfigFiles(key, function (configFiles) {
 | 
				
			||||||
                            if (configFiles == null) { console.log("Error, no configuration files found or invalid configkey."); process.exit(); return; }
 | 
					                            if (configFiles == null) { console.log("Error, no configuration files found or invalid configkey."); process.exit(); return; }
 | 
				
			||||||
                            if (!configFiles['config.json']) { console.log("Error, could not file config.json from database."); process.exit(); return; }
 | 
					                            if (!configFiles['config.json']) { console.log("Error, could not file config.json from database."); process.exit(); return; }
 | 
				
			||||||
 | 
					                            if (typeof configFiles['config.json'] == 'object') { configFiles['config.json'] = configFiles['config.json'].toString(); }
 | 
				
			||||||
 | 
					                            if (configFiles['config.json'].charCodeAt(0) == 65279) { configFiles['config.json'] = configFiles['config.json'].substring(1); }
 | 
				
			||||||
                            obj.configurationFiles = configFiles;
 | 
					                            obj.configurationFiles = configFiles;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            // Parse the new configuration file
 | 
					                            // Parse the new configuration file
 | 
				
			||||||
                            var config2 = null;
 | 
					                            var config2 = null;
 | 
				
			||||||
                            try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.'); process.exit(); return; }
 | 
					                            try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.', ex); process.exit(); return; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            // Set the command line arguments to the config file if they are not present
 | 
					                            // Set the command line arguments to the config file if they are not present
 | 
				
			||||||
                            if (!config2.settings) { config2.settings = {}; }
 | 
					                            if (!config2.settings) { config2.settings = {}; }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue