From 66ef9a3d46b6ef7cec4f0a3830f76c16c2e6e192 Mon Sep 17 00:00:00 2001 From: venizelou andreas Date: Sun, 18 Aug 2024 22:32:52 +0300 Subject: [PATCH] Issues, back-end --- Protest/Misc/Data.cs | 4 +++ Protest/Workers/Issues.cs | 69 +++++++++++++++++++++++++++++++------ Protest/Workers/Lifeline.cs | 7 ++-- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/Protest/Misc/Data.cs b/Protest/Misc/Data.cs index e95637e2..52bbe4e0 100644 --- a/Protest/Misc/Data.cs +++ b/Protest/Misc/Data.cs @@ -18,6 +18,10 @@ public static class Data { public const string DATETIME_FORMAT_LONG = "dddd dd MMM yyyy HH:mm:ss"; public const string DATETIME_FORMAT_FILE = "yyyy-MM-dd HH:mm:ss"; + public static readonly string[] PRINTER_TYPES = new string[] { "fax", "multiprinter", "ticket printer", "printer"}; + public static readonly string[] SWITCH_TYPES = new string[] { "switch", "router", "firewall"}; + + //pre-baked json responses: public static readonly ArraySegment CODE_OK = new ArraySegment("{\"status\":\"ok\"}"u8.ToArray()); public static readonly ArraySegment CODE_ACK = new ArraySegment("{\"status\":\"acknowledge\"}"u8.ToArray()); diff --git a/Protest/Workers/Issues.cs b/Protest/Workers/Issues.cs index 696a0d5a..f087bba0 100644 --- a/Protest/Workers/Issues.cs +++ b/Protest/Workers/Issues.cs @@ -10,6 +10,7 @@ using Protest.Http; using Protest.Tools; +using static Protest.Tools.DebitNotes; namespace Protest.Workers; @@ -105,7 +106,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { } await WsWriteText(ws, "{\"test\":\"test\"}"u8.ToArray()); - await Task.Delay(1000); + await Task.Delay(10_000); } } catch { @@ -120,14 +121,40 @@ public static async void WebSocketHandler(HttpListenerContext ctx) { } private static void Scan() { - // + ScanUsers(); + ScanDevices(); + } + + private static void ScanUsers() { + foreach (KeyValuePair user in DatabaseInstances.users.dictionary) { + user.Value.attributes.TryGetValue("type", out Database.Attribute typeAttribute); + + } + } + + private static void ScanDevices() { + foreach (KeyValuePair device in DatabaseInstances.devices.dictionary) { + device.Value.attributes.TryGetValue("type", out Database.Attribute typeAttribute); + device.Value.attributes.TryGetValue("ip", out Database.Attribute ipAttribute); + + if (Data.PRINTER_TYPES.Contains(typeAttribute.value)) { + + } + else if (Data.SWITCH_TYPES.Contains(typeAttribute.value)) { + + } + } } public static bool CheckPasswordStrength(Database.Entry entry, out Issue? issue) { if (entry.attributes.TryGetValue("password", out Database.Attribute password)) { string value = password.value; if (value.Length > 0 && PasswordStrength.Entropy(value) < WEAK_PASSWORD_ENTROPY_THRESHOLD) { - issue = new Issues.Issue { level = Issues.IssueLevel.critical, message = "Weak password", source = "Internal check" }; + issue = new Issues.Issue { + level = Issues.IssueLevel.critical, + message = "Weak password", + source = "Internal check" + }; return true; } } @@ -138,17 +165,29 @@ public static bool CheckPasswordStrength(Database.Entry entry, out Issue? issue) public static bool CheckDiskCapacity(double percent, string diskCaption, out Issue? issue) { if (percent <= 1) { - issue = new Issues.Issue { level = IssueLevel.critical, message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", source = "WMI" }; + issue = new Issues.Issue { + level = IssueLevel.critical, + message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", + source = "WMI" + }; return true; } if (percent <= 5) { - issue = new Issues.Issue { level = IssueLevel.error, message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", source = "WMI" }; + issue = new Issues.Issue { + level = IssueLevel.error, + message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", + source = "WMI" + }; return true; } if (percent < 15) { - issue = new Issues.Issue { level = IssueLevel.warning, message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", source = "WMI" }; + issue = new Issues.Issue { + level = IssueLevel.warning, + message = $"{percent}% free space on disk {Data.EscapeJsonText(diskCaption)}", + source = "WMI" + }; return true; } @@ -164,9 +203,9 @@ public static bool CheckPrinterComponent(IPAddress ipAddress, SnmpProfiles.Profi if (componentName is not null && componentCurrent is not null && componentMax is not null && componentName.Count == componentCurrent.Count && componentCurrent.Count == componentMax.Count) { - string[][] componentNameArray = componentName.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); - string[][] componentMaxArray = componentMax.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); - string[][] componentCurrentArray = componentCurrent.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); + string[][] componentNameArray = componentName.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); + string[][] componentMaxArray = componentMax.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); + string[][] componentCurrentArray = componentCurrent.Select(pair=> new string[] { pair.Key, pair.Value }).ToArray(); Array.Sort(componentNameArray, (x, y) => string.Compare(x[0], y[0])); Array.Sort(componentMaxArray, (x, y) => string.Compare(x[0], y[0])); @@ -185,10 +224,18 @@ public static bool CheckPrinterComponent(IPAddress ipAddress, SnmpProfiles.Profi int used = 100 * current / max; if (used < 5) { - arrays.Add(new Issues.Issue { level = IssueLevel.error, message = $"{used}% {componentNameArray[i][1]}", source = "SNMP" }); + arrays.Add(new Issues.Issue { + level = IssueLevel.error, + message = $"{used}% {componentNameArray[i][1]}", + source = "SNMP" + }); } else if (used < 15) { - arrays.Add(new Issues.Issue { level = IssueLevel.warning, message = $"{used}% {componentNameArray[i][1]}", source = "SNMP" }); + arrays.Add(new Issues.Issue { + level = IssueLevel.warning, + message = $"{used}% {componentNameArray[i][1]}", + source = "SNMP" + }); } } diff --git a/Protest/Workers/Lifeline.cs b/Protest/Workers/Lifeline.cs index 877a31a3..62100b28 100644 --- a/Protest/Workers/Lifeline.cs +++ b/Protest/Workers/Lifeline.cs @@ -16,9 +16,6 @@ namespace Protest.Workers; internal static partial class Lifeline { private const long FOUR_HOURS_IN_TICKS = 144_000_000_000L; - private static readonly string[] PRINTER_TYPES = new string[] { "fax", "multiprinter", "ticket printer", "printer"}; - private static readonly string[] SWITCH_TYPES = new string[] { "switch", "router", "firewall"}; - private static ConcurrentDictionary pingMutexes = new ConcurrentDictionary(); private static ConcurrentDictionary wmiMutexes = new ConcurrentDictionary(); @@ -154,7 +151,7 @@ private static void LifelineLoop() { if (!DatabaseInstances.devices.dictionary.TryGetValue(data[0], out Database.Entry entry)) { continue; } if (!entry.attributes.TryGetValue("type", out Database.Attribute typeAttr)) { continue; } string type = typeAttr.value.ToLower(); - if (PRINTER_TYPES.Contains(type)) { + if (Data.PRINTER_TYPES.Contains(type)) { mutex.TryGetValue(data[0], out object obj); try { lock (obj) { @@ -165,7 +162,7 @@ private static void LifelineLoop() { } catch { } } - else if (SWITCH_TYPES.Contains(type)) { + else if (Data.SWITCH_TYPES.Contains(type)) { //mutex.TryGetValue(data[0], out object obj); //TODO: }