Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed Feb 8, 2024
1 parent 089adad commit c66b2e0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Protest/Front/about.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class About extends Tabs {
center.appendChild(document.createElement("br"));

const icons = ["mono/logo.svg", "mono/copyleft.svg", "mono/opensource.svg","mono/gpl.svg"];
for (let i = 0; i < icons.length; i++) {
for (let i=0; i<icons.length; i++) {
const newIcon = document.createElement("div");
newIcon.style.display = "inline-block";
newIcon.style.width = "52px";
Expand Down
19 changes: 17 additions & 2 deletions Protest/Front/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class Monitor extends Window {
let words = queryInput.value.split(" ");
let className = null;
if (wmiClasses.classes) {
for (let i = 0; i < words.length; i++) {
for (let i=0; i<words.length; i++) {
if (words[i].toUpperCase() === "FROM" && i !== words.length-1) {
className = words[i+1].toLowerCase();
break;
Expand All @@ -603,7 +603,7 @@ class Monitor extends Window {

classesBox.textContent = "";

for (let i = 0; i < wmiClasses.classes.length; i++) {
for (let i=0; i<wmiClasses.classes.length; i++) {
let matched = false;

if (wmiClasses.classes[i].class.toLowerCase().indexOf(filter) > -1) {
Expand Down Expand Up @@ -706,6 +706,15 @@ class Monitor extends Window {
canvas.height = height;
inner.appendChild(canvas);

const dot = document.createElement("div");
dot.style.position = "absolute";
dot.style.right = "192px";
dot.style.top = "10px";
dot.style.width = "9px";
dot.style.height = "9px";
dot.style.borderRadius = "5px";
inner.parentElement.appendChild(dot);

let min = Number.MAX_SAFE_INTEGER;
let max = Number.MIN_SAFE_INTEGER;
const list = [];
Expand Down Expand Up @@ -768,6 +777,12 @@ class Monitor extends Window {
}

DrawGraph();

dot.style.backgroundColor = (list[list.length-1] < 0) ? "rgb(240,16,16)" : UI.PingColor(list[list.length-1]);
dot.style.boxShadow = `${dot.style.backgroundColor} 0 0 2px`;

dot.style.animation = "";
setTimeout(()=>{ dot.style.animation = "heart-beat .1s ease-out 1"; }, 0);
};

return {
Expand Down
5 changes: 5 additions & 0 deletions Protest/Front/window.css
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@
100% { opacity: 1; }
}

@keyframes heart-beat {
0% { transform: none; }
100% { transform: scale(1.2); }
}

@keyframes highlight {
0% {
background: radial-gradient(circle, var(--clr-accent) 0%, transparent 100%);
Expand Down
7 changes: 0 additions & 7 deletions Protest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ internal class Program {
internal static readonly string[] alternativeUriPrefixes = new string[] { "http://127.0.0.1:8080/" };

static void Main(string[] args) {

var res = Arp.ArpRequest("192.168.169.38");
Console.WriteLine("result for 192.168.169.38: " + res);

res = Arp.ArpRequest("192.168.19.1");
Console.WriteLine("result for 192.168.19.1: " + res);

Console.Title = "Pro-test";

Console.WriteLine(@" _____");
Expand Down
2 changes: 2 additions & 0 deletions Protest/Protocols/Arp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ namespace Protest.Protocols;

internal static partial class Arp {

#pragma warning disable SYSLIB1092
[SupportedOSPlatform("windows")]
[LibraryImport("iphlpapi.dll")]
private static partial int SendARP(uint destIP, uint srcIP, byte[] macAddr, ref int macAddrLen);
#pragma warning restore SYSLIB1092

[GeneratedRegex("^((?:[0-9]{1,3}\\.){3}[0-9]{1,3})(?:\\s+\\w+){2}\\s+((?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2}))")]
private static partial Regex LinuxMacAddressRegex();
Expand Down
112 changes: 47 additions & 65 deletions Protest/Tools/Monitor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;
using System.Net.NetworkInformation;
using System.Management;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Net;
using System.Net.WebSockets;
using System.Net.NetworkInformation;
using System.Management;
using System.Runtime.Versioning;
using System.Diagnostics.CodeAnalysis;
using Protest.Http;

namespace Protest.Tools;
Expand Down Expand Up @@ -71,10 +71,8 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
object sendSync = new object();
bool paused = false;
bool ping = true;
bool cpu = true;
bool cores = true;
int interval = 500;
ConcurrentDictionary<string, string> wmiQueries = new ConcurrentDictionary<string, string>();
ConcurrentDictionary<int, Query> queries = new ConcurrentDictionary<int, Query>();

try {
byte[] buff = new byte[2048];
Expand Down Expand Up @@ -165,21 +163,8 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
long startTime = DateTime.UtcNow.Ticks;

if (OperatingSystem.IsWindows()) {
if (cpu || cores) {
byte[] cpuResult = DoCpuCores(scope, cores);
if (cpu && cpuResult is not null && cpuResult.Length > 0) {
lock (sendSync) {
WsWriteText(ws, $"{{\"result\":\"cpu\",\"value\":{cpuResult[0]}}}");
}
}
if (cores && cpuResult is not null && cpuResult.Length > 0) {
lock (sendSync) {
WsWriteText(ws, $"{{\"result\":\"cores\",\"value\":[{String.Join(",", cpuResult.Skip(1).ToArray())}]}}");
}
}
}

DoWmi(scope, wmiQueries);
DoQuery(scope, queries);
}

long elapsedTime = (DateTime.UtcNow.Ticks - startTime) / 10_000;
Expand Down Expand Up @@ -225,9 +210,8 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
break;

case Action.addwmi:
break;

case Action.addsnmp:
queries.TryAdd(query.id, query);
break;

case Action.remove:
Expand Down Expand Up @@ -258,37 +242,7 @@ public static async void WebSocketHandler(HttpListenerContext ctx) {
} catch {}
}

private static long DoPing(string host, int timeout) {
Ping p = new Ping();
try {
PingReply reply = p.Send(host, timeout);

return (int)reply.Status switch {
(int)IPStatus.Success => reply.RoundtripTime,

(int)IPStatus.DestinationUnreachable or
(int)IPStatus.DestinationHostUnreachable or
(int)IPStatus.DestinationNetworkUnreachable => -1,

11050 => -1,

_ => -1,
};
}
catch (ArgumentException) {
return -1;
}
catch (PingException) {
return -1;
}
catch (Exception) {
return -1;
}
finally {
p.Dispose();
}
}

/*
[SupportedOSPlatform("windows")]
private static byte[] DoCpuCores(ManagementScope scope, bool getCores) {
List<byte> cores = new List<byte>();
Expand All @@ -298,9 +252,7 @@ private static byte[] DoCpuCores(ManagementScope scope, bool getCores) {
IEnumerable<ManagementObject> perfTotalEnum = perfTotal.Cast<ManagementObject>();
if (perfTotalEnum is null) { return null; }
foreach (ManagementObject o in perfTotalEnum) {
if (o is null) {
continue;
}
if (o is null) { continue; }
ulong idle = (ulong)o!.GetPropertyValue("PercentIdleTime");
cores.Add((byte)(100 - idle));
}
Expand All @@ -310,9 +262,7 @@ private static byte[] DoCpuCores(ManagementScope scope, bool getCores) {
IEnumerable<ManagementObject> perfEnum = perf.Cast<ManagementObject>();
//if (perfEnum is null) { return null; }
foreach (ManagementObject o in perfEnum) {
if (o is null) {
continue;
}
if (o is null) { continue; }
ulong idle = (ulong)o!.GetPropertyValue("PercentIdleTime");
cores.Add((byte)(100 - idle));
}
Expand All @@ -324,9 +274,41 @@ private static byte[] DoCpuCores(ManagementScope scope, bool getCores) {
return null;
}
}
*/

private static long DoPing(string host, int timeout) {
Ping p = new Ping();
try {
PingReply reply = p.Send(host, timeout);

return (int)reply.Status switch {
(int)IPStatus.Success => reply.RoundtripTime,

(int)IPStatus.DestinationUnreachable or
(int)IPStatus.DestinationHostUnreachable or
(int)IPStatus.DestinationNetworkUnreachable => -1,

11050 => -1,

_ => -1,
};
}
catch (ArgumentException) {
return -1;
}
catch (PingException) {
return -1;
}
catch (Exception) {
return -1;
}
finally {
p.Dispose();
}
}

[SupportedOSPlatform("windows")]
private static byte[] DoWmi(ManagementScope scope, ConcurrentDictionary<string, string> queries) {
private static byte[] DoQuery(ManagementScope scope, ConcurrentDictionary<int, Query> queries) {
try {

}
Expand Down

0 comments on commit c66b2e0

Please sign in to comment.