Added user login report.

Ylian Saint-Hilaire committed Mar 1, 2022 at 15:25 UTC eae865ab8d000a8c5e28843cc35c7893872fe6b4
4 files changed +101 -11
meshuser.js
+72 -7
@@ -5914,18 +5914,27 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5914 }
5915
5916 function serverCommandReport(command) {
5917 - if (common.validateInt(command.type, 1, 2) == false) return; // Validate type
5917 + if (common.validateInt(command.type, 1, 3) == false) return; // Validate type
5918 if (common.validateInt(command.groupBy, 1, 3) == false) return; // Validate groupBy: 1 = User, 2 = Device, 3 = Day
5919 if ((typeof command.start != 'number') || (typeof command.end != 'number') || (command.start >= command.end)) return; // Validate start and end time
5920 const manageAllDeviceGroups = ((user.siteadmin == 0xFFFFFFFF) && (parent.parent.config.settings.managealldevicegroups.indexOf(user._id) >= 0));
5921 if ((command.devGroup != null) && (manageAllDeviceGroups == false) && ((user.links == null) || (user.links[command.devGroup] == null))) return; // Asking for a device group that is not allowed
5922
5923 const msgIdFilter = [5, 10, 11, 12, 122, 123, 124, 125, 126];
5924 -
5925 - if (command.type == 1)
5926 - remoteSessionReport(command, manageAllDeviceGroups, msgIdFilter);
5927 - if (command.type == 2)
5928 - trafficUsageReport(command, msgIdFilter);
5924 + switch (command.type) {
5925 + case 1: {
5926 + remoteSessionReport(command, manageAllDeviceGroups, msgIdFilter);
5927 + break;
5928 + }
5929 + case 2: {
5930 + trafficUsageReport(command, msgIdFilter);
5931 + break;
5932 + }
5933 + case 3: {
5934 + userLoginReport(command, msgIdFilter);
5935 + break;
5936 + }
5937 + }
5938 }
5939
5940 function serverCommandServerClearErrorLog(command) {
@@ -6939,7 +6948,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6948
6949 // Fetch or create the user entry
6950 var userEntry = userEntries[docs[i].userid];
6942 - if (userEntry == null) { userEntry = { userid: docs[i].userid, length: 0, bytesin: 0, bytesout: 0}; }
6951 + if (userEntry == null) { userEntry = { userid: docs[i].userid, length: 0, bytesin: 0, bytesout: 0 }; }
6952 if (docs[i].bytesin) { userEntry.bytesin += docs[i].bytesin; }
6953 if (docs[i].bytesout) { userEntry.bytesout += docs[i].bytesout; }
6954
@@ -6959,6 +6968,62 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6968 });
6969 }
6970
6971 +
6972 + function userLoginReport(command) {
6973 + // If we are not user administrator on this site, only search for events with our own user id.
6974 + var ids = [user._id]; // If we are nto user administrator, only count our own traffic.
6975 + if ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0) { ids = ['*']; } // If user administrator, count traffic of all users.
6976 +
6977 + var showInvalidLoginAttempts = true;
6978 +
6979 + // Get the events in the time range
6980 + // MySQL or MariaDB query will ignore the MsgID filter.
6981 + var msgIdFilter = [107];
6982 + if (showInvalidLoginAttempts) { msgIdFilter = [107, 108, 109, 110]; } // Includes invalid login attempts
6983 +
6984 + db.GetEventsTimeRange(ids, domain.id, msgIdFilter, new Date(command.start * 1000), new Date(command.end * 1000), function (err, docs) {
6985 + if (err != null) return;
6986 +
6987 + // Columns
6988 + var data = { groups: {} };
6989 + if (command.groupBy == 1) {
6990 + data.groupFormat = 'user';
6991 + data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'ip', title: "ip" }, { id: 'browser', title: "browser" }, { id: 'os', title: "os" }];
6992 + } else if (command.groupBy == 3) {
6993 + data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'userid', title: "user", format: 'user' }, { id: 'ip', title: "ip" }, { id: 'browser', title: "browser" }, { id: 'os', title: "os" }];
6994 + }
6995 + if (showInvalidLoginAttempts) { data.columns.push({ id: 'msg', title: "msg", format: 'msg' }); }
6996 +
6997 + // Add all log entries
6998 + var entries = [];
6999 + for (var i in docs) {
7000 + // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here.
7001 + if (msgIdFilter.indexOf(docs[i].msgid) < 0) continue;
7002 +
7003 + if (command.groupBy == 1) { // Add entry per user
7004 + if (data.groups[docs[i].userid] == null) { data.groups[docs[i].userid] = { entries: [] }; }
7005 + const entry = { time: docs[i].time.valueOf(), ip: docs[i].msgArgs[0], browser: docs[i].msgArgs[1], os: docs[i].msgArgs[2] };
7006 + if (showInvalidLoginAttempts) { entry.msg = docs[i].msgid }
7007 + data.groups[docs[i].userid].entries.push(entry);
7008 + } else if (command.groupBy == 3) { // Add entry per day
7009 + var day;
7010 + if ((typeof command.l == 'string') && (typeof command.tz == 'string')) {
7011 + day = new Date(docs[i].time).toLocaleDateString(command.l, { timeZone: command.tz });
7012 + } else {
7013 + day = docs[i].time; // TODO
7014 + }
7015 + if (data.groups[day] == null) { data.groups[day] = { entries: [] }; }
7016 + const entry = { time: docs[i].time.valueOf(), userid: docs[i].userid, ip: docs[i].msgArgs[0], browser: docs[i].msgArgs[1], os: docs[i].msgArgs[2] };
7017 + if (showInvalidLoginAttempts) { entry.msg = docs[i].msgid }
7018 + data.groups[day].entries.push(entry);
7019 + }
7020 + }
7021 +
7022 + try { ws.send(JSON.stringify({ action: 'report', data: data })); } catch (ex) { }
7023 + });
7024 + }
7025 +
7026 +
7027 // Return detailed information about an array of nodeid's
7028 function getDeviceDetailedInfo(nodeids, type, func) {
7029 if (nodeids == null) { getAllDeviceDetailedInfo(type, func); return; }
public/images/notify16.png
Binary files /dev/null and b/public/images/notify16.png differ
public/styles/style.css
+9
@@ -848,6 +848,15 @@ NoMeshesPanel img {
848 .NotifyIconSmall7 { width:24px; height:24px; background: url(../images/notify24.png) -144px 0px; }
849 .NotifyIconSmall8 { width:24px; height:24px; background: url(../images/notify24.png) -168px 0px; }
850 .NotifyIconSmall9 { width:24px; height:24px; background: url(../images/notify24.png) -192px 0px; }
851 +.NotifyIconTiny1 { width:16px; height:16px; background: url(../images/notify16.png) 0px 0px; }
852 +.NotifyIconTiny2 { width:16px; height:16px; background: url(../images/notify16.png) -16px 0px; }
853 +.NotifyIconTiny3 { width:16px; height:16px; background: url(../images/notify16.png) -32px 0px; }
854 +.NotifyIconTiny4 { width:16px; height:16px; background: url(../images/notify16.png) -48px 0px; }
855 +.NotifyIconTiny5 { width:16px; height:16px; background: url(../images/notify16.png) -64px 0px; }
856 +.NotifyIconTiny6 { width:16px; height:16px; background: url(../images/notify16.png) -80px 0px; }
857 +.NotifyIconTiny7 { width:16px; height:16px; background: url(../images/notify16.png) -96px 0px; }
858 +.NotifyIconTiny8 { width:16px; height:16px; background: url(../images/notify16.png) -112px 0px; }
859 +.NotifyIconTiny9 { width:16px; height:16px; background: url(../images/notify16.png) -128px 0px; }
860
861 .deviceBatteryLarge {
862 position:absolute;
views/default.handlebars
+20 -4
@@ -15623,7 +15623,7 @@
15623 if (xxdialogMode) return;
15624 var y = '', x = '', settings = JSON.parse(getstore('_ReportSettings', '{}'));
15625
15626 - var options = { 1 : "Remote Sessions", 2 : "User Traffic Usage" }
15626 + var options = { 1 : "Remote Sessions", 2 : "User Traffic Usage", 3 : "User Logins" }
15627 for (var i in options) { y += '<option value=' + i + ((settings.type == i)?' selected':'') + '>' + options[i] + '</option>'; }
15628 x += addHtmlValue("Type", '<select id=d2reportType style=float:right;width:250px onchange=generateReportDialogValidate()>' + y + '</select>');
15629
@@ -15634,6 +15634,13 @@
15634 x += addHtmlValue("Group by", '<select id=d2groupBy style=float:right;width:250px onchange=generateReportDialogValidate()>' + y + '</select>');
15635 x += '</div>';
15636
15637 + x += '<div id=d2GroupByDiv2 style=display:none>';
15638 + y = '';
15639 + var options = { 1 : "User", 3: "Day" }
15640 + for (var i in options) { y += '<option value=' + i + ((settings.groupBy2 == i)?' selected':'') + '>' + options[i] + '</option>'; }
15641 + x += addHtmlValue("Group by", '<select id=d2groupBy2 style=float:right;width:250px onchange=generateReportDialogValidate()>' + y + '</select>');
15642 + x += '</div>';
15643 +
15644 x += '<div id=d2DeviceGroupDiv style=display:none>';
15645 y = '<option value=0' + ((settings.devGroup == 0)?' selected':'') + '>' + "All" + '</option>';
15646 var omeshs = getOrderedList(meshes, 'name');
@@ -15667,6 +15674,7 @@
15674 function generateReportDialogValidate() {
15675 QV('d2timeRangeDiv', Q('d2timeRange').value == 0);
15676 QV('d2GroupByDiv', Q('d2reportType').value == 1);
15677 + QV('d2GroupByDiv2', Q('d2reportType').value == 3);
15678 QV('d2DeviceGroupDiv', Q('d2reportType').value == 1);
15679 QV('d2showTrafficDiv', Q('d2reportType').value == 1);
15680 }
@@ -15685,12 +15693,14 @@
15693 try { tz = Intl.DateTimeFormat().resolvedOptions().timeZone; } catch (ex) {}
15694 var devGroup = decodeURIComponent(Q('d2groupId').value);
15695 if (devGroup == 0) { devGroup = null; }
15688 - putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked }));
15689 - meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked });
15696 + putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), groupBy2: parseInt(Q('d2groupBy2').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked }));
15697 + var groupBy = parseInt(Q('d2groupBy').value);
15698 + if (Q('d2reportType').value == 3) { groupBy = parseInt(Q('d2groupBy2').value); }
15699 + meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: groupBy, start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked });
15700 }
15701
15702 function renderReport(r) {
15693 - var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", devgroup: "Device Group", guest: "Guest", length: "Length", bytesin: "Bytes In", bytesout: "Bytes Out" }
15703 + var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", devgroup: "Device Group", guest: "Guest", length: "Length", bytesin: "Bytes In", bytesout: "Bytes Out", os: "Operating System", ip: "IP Address", browser: "Browser", msg: "Message" }
15704 var x = '<table style=width:100%>', firstItem;
15705 var sumByCol = null; // Indicate by what colum we sum by
15706 var sumByValues = []; // Indicate by what values we sum by
@@ -15844,6 +15854,12 @@
15854 return '<i>' + "Unknown User" + '</i>';
15855 }
15856 }
15857 + if (f == 'msg') {
15858 + if (v == 107) return '<div style=float:left;margin-right:4px;cursor:pointer class="NotifyIconTiny1"></div>' + "Succesful login";
15859 + if (v == 108) return '<div style=float:left;margin-right:4px;cursor:pointer class="NotifyIconTiny2"></div>' + "Incorrect 2nd factor";
15860 + if (v == 109) return '<div style=float:left;margin-right:4px;cursor:pointer class="NotifyIconTiny4"></div>' + "Locked account";
15861 + if (v == 110) return '<div style=float:left;margin-right:4px;cursor:pointer class="NotifyIconTiny3"></div>' + "Invalid login attempt";
15862 + }
15863 return EscapeHtml(v);
15864 }
15865