Improvements for automatic device removal, #3089

This commit is contained in:
Ylian Saint-Hilaire 2021-09-01 22:21:39 -07:00
parent a9116c2d26
commit 7d25391f78
2 changed files with 19 additions and 8 deletions

17
db.js
View File

@ -114,7 +114,7 @@ module.exports.CreateDB = function (parent, func) {
}
// Remove inactive devices
obj.removeInactiveDevices = function () {
obj.removeInactiveDevices = function (showall, cb) {
// Get a list of domains and what their inactive device removal setting is
var removeInactiveDevicesPerDomain = {}, minRemoveInactiveDevicesPerDomain = {}, minRemoveInactiveDevice = 9999;
for (var i in parent.config.domains) {
@ -144,7 +144,7 @@ module.exports.CreateDB = function (parent, func) {
}
// If there are no such settings for any domain, we can exit now.
if (minRemoveInactiveDevice == 9999) return;
if (minRemoveInactiveDevice == 9999) { if (cb) { cb("No device removal policy set, nothing to do."); } return; }
const now = Date.now();
// For each domain with a inactive device removal setting, get a list of last device connections
@ -152,10 +152,14 @@ module.exports.CreateDB = function (parent, func) {
obj.GetAllTypeNoTypeField('lastconnect', domainid, function (err, docs) {
if ((err != null) || (docs == null)) return;
for (var j in docs) {
const days = (now - docs[j].time) / 86400000; // Calculate the number of inactive days
const days = Math.floor((now - docs[j].time) / 86400000); // Calculate the number of inactive days
var expireDays = -1;
if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; }
const mesh = parent.webserver.meshes[docs[j].meshid];
if (mesh && (typeof mesh.expireDevs == 'number') && (expireDays > mesh.expireDevs)) { expireDays = mesh.expireDevs; }
var remove = false;
if (removeInactiveDevicesPerDomain[docs[j].domain] && (removeInactiveDevicesPerDomain[docs[j].domain] < days)) { remove = true; }
else { const mesh = parent.webserver.meshes[docs[j].meshid]; if (mesh && (typeof mesh.expireDevs == 'number') && (mesh.expireDevs < days)) { remove = true; } }
if ((expireDays > 0) && (expireDays < days)) { remove = true; }
if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
if (remove) {
// Check if this device is connected right now
const nodeid = docs[j]._id.substring(2);
@ -163,7 +167,8 @@ module.exports.CreateDB = function (parent, func) {
if (conn == null) {
// Remove the device
obj.Get(nodeid, function (err, docs) {
if ((err != null) || (docs == null) || (docs.length != 1)) return;
if (err != null) return;
if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
const node = docs[0];
// Delete this node including network interface information, events and timeline

View File

@ -6248,9 +6248,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
if (cmdData.result == '') { cmdData.result = 'No relays.'; }
}
// removeinactivedevices showall|showremoved
function serverUserCommandRemoveInactiveDevices(cmdData) {
parent.db.removeInactiveDevices();
cmdData.result = 'Ok';
var arg = cmdData.cmdargs['_'][0];
if ((arg == null) && (arg != 'showremoved') && (arg != 'showall')) {
cmdData.result = 'Usage: removeinactivedevices [showremoved|showall]';
} else {
parent.db.removeInactiveDevices((arg == 'showall'), function (msg) { try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { } });
cmdData.result = 'Ok';
}
}
function serverUserCommandAutoBackup(cmdData) {