Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
krisdb2009 committed Jan 8, 2024
1 parent b86183a commit 3e7f78c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
8 changes: 5 additions & 3 deletions Tikhole.Engine/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override void UpdateList(object? a = null, object? b = null)
}
}
}
public abstract class RuleHashSetDownloadable : RuleHashSet, IDisposable
public abstract class RuleHashSetDownloadable : RuleHashSet
{
public Uri Uri;
public System.Timers.Timer UpdateTimer;
Expand All @@ -138,7 +138,7 @@ public override bool Matches(string Hostname)
{
return List.Contains(Hostname);
}
public void Dispose()
public override void Dispose()
{
UpdateTimer.Stop();
UpdateTimer.Dispose();
Expand All @@ -156,19 +156,21 @@ public override bool Matches(string Hostname)
{
return Regex.IsMatch(Hostname);
}
public override void Dispose() { }
}
public abstract class RuleHashSet : Rule
{
protected RuleHashSet(string Name) : base(Name) { }
public HashSet<string> List = new();
}
public abstract class Rule
public abstract class Rule : IDisposable
{
public string Name;
public Rule(string Name)
{
this.Name = Name;
}
public abstract void Dispose();
public abstract bool Matches(string Hostname);
}
public class ResponseMatchedEventArgs : EventArgs
Expand Down
1 change: 1 addition & 0 deletions Tikhole.Engine/Tikhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Tikhole()
public void Dispose()
{
Listener?.Dispose();
foreach (Rule rule in Matcher.Rules) rule.Dispose();
if (Committers != null) foreach (Committer c in Committers) c.Dispose();
}
}
Expand Down
43 changes: 43 additions & 0 deletions Tikhole.Website/Components/Pages/Dashboard.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/"
@using System.Net
@using System.Timers
@using System.Diagnostics
@using global::Tikhole.Engine

<PageTitle>🚦 Dashboard</PageTitle>
Expand All @@ -9,6 +11,32 @@
<small>Real-time counters and events.</small>
</p>
<hr />
<div class="row row-cols-1 row-cols-md-3 g-4 mb-3">
<div class="col">
<div class="card h-100">
<div class="card-header">🏃 <b>CPU</b></div>
<div class="card-body text-center">
<h1>@CPUUsage %</h1>
</div>
</div>
</div>
<div class="col">
<div class="card h-100">
<div class="card-header">🧠 <b>Memory</b></div>
<div class="card-body text-center">
<h1>@(Math.Round((float)Process.PrivateMemorySize64 / 1024 / 1024, 1)) MB</h1>
</div>
</div>
</div>
<div class="col">
<div class="card h-100">
<div class="card-header">⏱️ <b>Uptime</b></div>
<div class="card-body text-center">
<h1>@DateTime.Now.Subtract(Process.StartTime).ToString("dd\\.hh\\:mm\\:ss")</h1>
</div>
</div>
</div>
</div>
<div class="row row-cols-1 row-cols-md-3 g-4 mb-3">
<div class="col">
<div class="card h-100">
Expand Down Expand Up @@ -78,13 +106,28 @@
private static uint RequestQueueMax = 100;
private Queue<DNSPacket> DNSPackets = new();
private SemaphoreSlim Semaphore = new(1, 1);
private Timer Timer = new(1000);
private Process Process = Process.GetCurrentProcess();
private TimeSpan CPULast;
double CPUUsage = 0;
protected override void OnInitialized()
{
base.OnInitialized();
CPULast = Process.TotalProcessorTime;
Timer.Elapsed += TimerElapsed;
Timer.Enabled = true;
Timer.Start();
if (Engine.Tikhole.Listener != null) Engine.Tikhole.Listener.RecievedRequestData += InvokeStateChange;
if (Engine.Tikhole.Matcher != null) Engine.Tikhole.Matcher.MatchesMatchedAndOrCommitted += InvokeStateChange;
if (Engine.Tikhole.Parser != null) Engine.Tikhole.Parser.ParsedResponseData += Parser_ParsedResponseData;
}
private void TimerElapsed(object? sender = null, EventArgs? e = null)
{
Process.Refresh();
CPUUsage = Math.Round(Process.TotalProcessorTime.Subtract(CPULast).Milliseconds / Timer.Interval * 10, 2);
CPULast = Process.TotalProcessorTime;
InvokeStateChange();
}
private void InvokeStateChange(object? sender = null, EventArgs? e = null)
{
InvokeAsync(() => StateHasChanged());
Expand Down
41 changes: 35 additions & 6 deletions Tikhole.Website/Components/Pages/Rule.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/Rules/{Index:int}"
@inject NavigationManager NavigationManager
@using global::Tikhole.Engine

<PageTitle>📃 Rule: @ERule?.Name</PageTitle>
Expand All @@ -20,14 +21,25 @@
<small>@ERule?.GetType().GetDisplayName()</small>
</p>
<hr />
<a href="/Rules" class="btn btn-dark mb-3" @onclick="() => { }">⬅️ Back</a>
<a href="/Rules" class="btn btn-dark">⬅️ Back</a>
@if (DeleteIntent)
{
<button class="btn btn-dark float-end" @onclick="InvokeDelete">⚠️ Confirm?</button>
}
else
{
<button class="btn btn-dark float-end" @onclick="InvokeDelete">🗑️ Delete</button>
}
<hr />

<div style="max-width:500px;">
<div class="mb-3">
<label class="form-label">Name</label>
<InputText @bind-Value="ERule.Name" class="form-control" />
</div>
@if (ERule != null)
{
<div class="mb-3">
<label class="form-label">Name</label>
<InputText @bind-Value="ERule!.Name" class="form-control" />
</div>
}
@if (ERule?.GetType() == typeof(Engine.RuleRegex))
{
Engine.RuleRegex rule = (Engine.RuleRegex)ERule;
Expand All @@ -45,9 +57,26 @@
public int Index { get; set; }
private string Error { get; set; } = "";
private Engine.Rule? ERule;
private bool DeleteIntent = false;
protected override void OnInitialized()
{
base.OnInitialized();
if (Engine.Matcher.Rules[Index] != null) ERule = Engine.Matcher.Rules[Index];
if (Index < Engine.Matcher.Rules.Count && Index >= 0) ERule = Engine.Matcher.Rules[Index];
}
private async void InvokeDelete()
{
if (DeleteIntent == true)
{
Delete();
return;
}
DeleteIntent = true;
await Task.Delay(2000);
DeleteIntent = false;
StateHasChanged();
}
private void Delete()
{
NavigationManager.NavigateTo("/Rules");
}
}
4 changes: 2 additions & 2 deletions Tikhole.Website/Components/Pages/Rules.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<small>A list of rules that Tikhole uses to identify domains to send to RouterOS IP lists.</small>
</p>
<hr />
<button class="btn btn-dark float-end mb-3" @onclick="() => { }">➕ Add</button>

<button class="btn btn-dark" @onclick="() => { }">➕ Add</button>
<hr />
@{
int index = 0;
}
Expand Down
1 change: 1 addition & 0 deletions Tikhole.Website/Tikhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void Main(string[] args)
public static void StopTikhole()
{
Engine.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive | GCCollectionMode.Forced, true);
}
public static void RestartTikhole()
{
Expand Down

0 comments on commit 3e7f78c

Please sign in to comment.