Skip to content

Commit

Permalink
Issues, back-end
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Aug 18, 2024
1 parent 096cfe8 commit 66ef9a3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Protest/Misc/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> CODE_OK = new ArraySegment<byte>("{\"status\":\"ok\"}"u8.ToArray());
public static readonly ArraySegment<byte> CODE_ACK = new ArraySegment<byte>("{\"status\":\"acknowledge\"}"u8.ToArray());
Expand Down
69 changes: 58 additions & 11 deletions Protest/Workers/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using Protest.Http;
using Protest.Tools;
using static Protest.Tools.DebitNotes;

namespace Protest.Workers;

Expand Down Expand Up @@ -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 {
Expand All @@ -120,14 +121,40 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
}

private static void Scan() {
//
ScanUsers();
ScanDevices();
}

private static void ScanUsers() {
foreach (KeyValuePair<string, Database.Entry> user in DatabaseInstances.users.dictionary) {
user.Value.attributes.TryGetValue("type", out Database.Attribute typeAttribute);

}
}

private static void ScanDevices() {
foreach (KeyValuePair<string, Database.Entry> 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;
}
}
Expand All @@ -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;
}

Expand All @@ -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]));
Expand All @@ -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"
});
}
}

Expand Down
7 changes: 2 additions & 5 deletions Protest/Workers/Lifeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> pingMutexes = new ConcurrentDictionary<string, object>();
private static ConcurrentDictionary<string, object> wmiMutexes = new ConcurrentDictionary<string, object>();

Expand Down Expand Up @@ -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) {
Expand All @@ -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:
}
Expand Down

0 comments on commit 66ef9a3

Please sign in to comment.