Unfiltered object is now a struct.

This commit is contained in:
2024-11-28 14:24:33 +01:00
parent eedca27a11
commit 3d8f903d30
14 changed files with 149 additions and 77 deletions
+15 -11
View File
@@ -55,26 +55,29 @@ public class ContentFilter
{
if (_stop) break;
Unfiltered? unfiltered = _dbHandler.ReadUnfilteredWithId(i);
Unfiltered unfiltered = _dbHandler.ReadUnfilteredWithId(i);
if (unfiltered is null || unfiltered.Filtered == 1) continue;
if (unfiltered.Filtered) continue;
unfiltered.Filtered = 1;
Ip ip = unfiltered.Ip;
unfiltered.Filtered = true;
QueueItem superUnfilteredObject = new()
{
Unfiltered = unfiltered,
Operations = Operations.Update
Operations = Operations.Update,
DbType = DbType.Unfiltered
};
_queue.Enqueue(superUnfilteredObject);
if (_dbHandler.GetFilteredIp(unfiltered.Ip))
if (_dbHandler.FilteredIpExists(unfiltered.Ip))
{
continue;
}
Filtered filtered = GetSiteData(unfiltered.Ip);
Filtered filtered = GetSiteData(ip);
filtered.Port1 = unfiltered.Port1;
filtered.Port2 = unfiltered.Port2;
@@ -82,7 +85,8 @@ public class ContentFilter
QueueItem superFilteredObject = new()
{
Filtered = filtered,
Operations = Operations.Insert
Operations = Operations.Insert,
DbType = DbType.Filtered
};
_queue.Enqueue(superFilteredObject);
@@ -94,7 +98,7 @@ public class ContentFilter
((EventWaitHandle) obj).Set();
}
private Filtered GetSiteData(string ip)
private Filtered GetSiteData(Ip ip)
{
StartProcess(ip, 80);
StartProcess(ip, 443);
@@ -222,15 +226,15 @@ public class ContentFilter
return siteData;
}
private void StartProcess(string ip, int port)
private void StartProcess(Ip ip, int port)
{
string fileName = port == 80 ? _getDomainPort80 : _getDomainPort443;
//Console.WriteLine($"{ip.Ip1}.{ip.Ip2}.{ip.Ip3}.{ip.Ip4}");
Process proc = new();
proc.StartInfo = new()
{
FileName = "/bin/bash",
Arguments = $"{fileName} {ip} {_basePath}/Backend/Scripts/{port}Header.txt",
Arguments = $"{fileName} {ip.Ip1}.{ip.Ip2}.{ip.Ip3}.{ip.Ip4} {_basePath}/Backend/Scripts/{port}Header.txt",
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
+18 -12
View File
@@ -127,16 +127,22 @@ public class IpScanner
resumeObject.FourthByte = l;
break;
}
string ip = $"{i}.{j}.{k}.{l}";
Ip ip = new()
{
Ip1 = i,
Ip2 = j,
Ip3 = k,
Ip4 = l
};
IPStatus responseCode = IPStatus.Unknown;
try
{
// Sometimes, if the pinger gets a Destination Unreachable Communication administratively prohibited response, the pinger will throw an exception.
// https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol?useskin=vector#Control_messages
_ = IPAddress.TryParse(ip, out IPAddress? address);
_ = IPAddress.TryParse(ip.ToString(), out IPAddress? address);
if (address is not null)
{
responseCode = ping.Send(address, _timeout, buf, null).Status;
@@ -149,19 +155,19 @@ public class IpScanner
if (responseCode != IPStatus.Success)
{
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip, (int)responseCode));
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip.ToString(), (int)responseCode));
continue;
}
(int, int) ports = TcpClientHelper.CheckPort(ip, 80, 443);
(int, int) ports = TcpClientHelper.CheckPort(ip.ToString(), 80, 443);
if (ports is { Item1: 0, Item2: 0 })
{
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip, (int)responseCode));
_discardedQueue.Enqueue(CreateDiscardedQueueItem(ip.ToString(), (int)responseCode));
continue;
}
_queue.Enqueue(CreateUnfilteredQueueItem(ip, (int)responseCode, ports));
_queue.Enqueue(CreateUnfilteredQueueItem(ip, ports));
}
if (_stop)
@@ -208,21 +214,21 @@ public class IpScanner
return discarded;
}
private static QueueItem CreateUnfilteredQueueItem(string ip, int responseCode, (int, int) ports)
private static QueueItem CreateUnfilteredQueueItem(Ip ip, (int, int) ports)
{
Unfiltered unfiltered = new()
{
Ip = ip,
ResponseCode = responseCode,
Port1 = ports.Item1,
Port2 = ports.Item2,
Filtered = 0
Filtered = false
};
QueueItem superUnfilteredObject = new()
{
Unfiltered = unfiltered,
Operations = Operations.Insert
Operations = Operations.Insert,
DbType = DbType.Unfiltered
};
return superUnfilteredObject;
+2 -2
View File
@@ -49,9 +49,9 @@ public class ThreadHandler
private void StartScanner()
{
Thread.Sleep(10000); // Let the database handler instantiate and warm up first.
Thread.Sleep(5000); // Let the database handler instantiate and warm up first.
WaitHandle[] wait = _ipScanner.Start(4);
WaitHandle[] wait = _ipScanner.Start(64);
WaitHandle.WaitAll(wait);
+5 -11
View File
@@ -1,19 +1,13 @@
DROP TABLE Unfiltered;
DROP TABLE Runtimes;
CREATE TABLE "Unfiltered" (
"Id" INTEGER NOT NULL,
"Ip" TEXT NOT NULL,
"ResponseCode" INTEGER NOT NULL,
"Ip1" INTEGER NOT NULL,
"Ip2" INTEGER NOT NULL,
"Ip3" INTEGER NOT NULL,
"Ip4" INTEGER NOT NULL,
"Port1" INTEGER NOT NULL,
"Port2" INTEGER NOT NULL,
"Filtered" INTEGER NOT NULL,
CONSTRAINT "PK_Unfiltered" PRIMARY KEY("Id" AUTOINCREMENT)
);
CREATE TABLE "Runtimes" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Runtimes" PRIMARY KEY AUTOINCREMENT,
"StartTime" TEXT NOT NULL,
"EndTime" TEXT NOT NULL,
"ThreadNumber" INTEGER NOT NULL
);
)