Fix NetMQ not stopping. #3

Merged
owner merged 1 commits from CleanUpCommunicationLogic into main 2024-11-25 18:16:36 +00:00
69 changed files with 120 additions and 100 deletions
Showing only changes of commit 96b5ce4ff5 - Show all commits

View File

@ -12,15 +12,14 @@ namespace Backend.Handler;
public class Communication
{
private readonly NetMQPoller _poller;
private readonly DbHandler _dbHandler;
private readonly ThreadHandler _threadHandler;
private bool _isRunning = true;
public Communication(DbHandler dbHandler, ThreadHandler threadHandler)
{
_dbHandler = dbHandler;
_threadHandler = threadHandler;
_poller = new();
}
public WaitHandle[] Start()
@ -35,7 +34,7 @@ public class Communication
return waitHandles;
}
private void Server(object obj)
/*private void Server(object obj)
{
using ResponseSocket server = new();
server.Bind("tcp://*:5556");
@ -50,17 +49,31 @@ public class Communication
Console.WriteLine("Communication stopped.");
((EventWaitHandle) obj).Set();
}*/
private void Server(object obj)
{
using ResponseSocket rep = new();
rep.Bind("tcp://127.0.0.1:5556");
while (_isRunning)
{
byte[] message = rep.ReceiveFrameBytes();
CommunicationObject communicationObject = MessagePackSerializer.Deserialize<CommunicationObject>(message);
OnServerOnReceiveReady(communicationObject, rep);
}
((EventWaitHandle) obj).Set();
}
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
private void OnServerOnReceiveReady(object? _, NetMQSocketEventArgs e)
private void OnServerOnReceiveReady(CommunicationObject communicationObject, ResponseSocket rep)
{
byte[] message = e.Socket.ReceiveFrameBytes();
CommunicationObject communicationObject = MessagePackSerializer.Deserialize<CommunicationObject>(message);
switch (communicationObject.Command)
{
case CommunicationCommand.GetScanningProgress:
@ -68,10 +81,10 @@ public class Communication
DatabaseSizes databaseSizes = FilesystemHelper.GetDatabaseSizes();
long discardedIndexes = _dbHandler.GetDiscardedIndexes();
ScanningStatus status = new();
// 4294967296 is all Ipv4 addresses.
if (discardedIndexes != 0)
{
status.PercentageOfIpv4Scanned = (float)discardedIndexes / 4294967296 * 100;
@ -80,67 +93,64 @@ public class Communication
{
status.PercentageOfIpv4Scanned = 0.0000000001f;
}
status.AmountOfIpv4Left = 4294967296 - discardedIndexes;
status.TotalFiltered = DbHandler.GetFilteredIndexes();
status.TotalDiscarded = discardedIndexes;
status.MyDbSize = databaseSizes.MyDbSize;
status.FilteredDbSize = databaseSizes.FilteredDbSize;
status.DiscardedDbSize = databaseSizes.DiscardedDbSize;
byte[] serializedResult = MessagePackSerializer.Serialize(status, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
e.Socket.SendFrame(serializedResult);
rep.SendFrame(serializedResult);
break;
}
case CommunicationCommand.StopScanning:
SendStringResponse(e, "Server is stopping.");
_threadHandler.Stop();
break;
case CommunicationCommand.GarbageCollect:
ThreadHandler.ManualGc();
SendStringResponse(e, "Server has garbage collected.");
_threadHandler.Stop();
break;
case CommunicationCommand.DbReindex:
{
_dbHandler.ReIndex();
SendStringResponse(e, "All Dbs have been reindexed.");
SendStringResponse(rep, "All Dbs have been reindexed.");
break;
}
case CommunicationCommand.DbVacuum:
{
_dbHandler.Vacuum();
SendStringResponse(e, "All Dbs have been vacuumed.");
SendStringResponse(rep, "All Dbs have been vacuumed.");
break;
}
case CommunicationCommand.GetSearches:
{
if (!string.IsNullOrWhiteSpace(communicationObject.SearchTerm))
{
SendSearchResponse(e, communicationObject.SearchTerm);
SendSearchResponse(rep, communicationObject.SearchTerm);
}
break;
}
case CommunicationCommand.StopScanning:
{
_isRunning = false;
break;
}
}
}
private static void SendStringResponse(NetMQSocketEventArgs e, string response)
private static void SendStringResponse(ResponseSocket rep, string response)
{
MessagePackSerializerOptions withCompression = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
byte[] serializedResult = MessagePackSerializer.Serialize(response, withCompression);
e.Socket.SendFrame(serializedResult);
rep.SendFrame(serializedResult);
}
private static void SendSearchResponse(NetMQSocketEventArgs e, string searchTerm)
private static void SendSearchResponse(ResponseSocket rep, string searchTerm)
{
//SearchResults result = SearchHelper.Search(communicationObject.SearchTerm!, _dbHandler);
SearchResults result = new()
@ -157,11 +167,6 @@ public class Communication
string serializedResult = JsonSerializer.Serialize(result);
e.Socket.SendFrame(serializedResult);
}
public void Stop()
{
_poller.Stop();
rep.SendFrame(serializedResult);
}
}

View File

@ -11,8 +11,8 @@ public class ContentFilter
{
private readonly ConcurrentQueue<QueueItem> _queue;
private readonly DbHandler _dbHandler;
private const string GetDomainPort80 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/GetDomainNamePort80.sh";
private const string GetDomainPort443 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/GetDomainNamePort443.sh";
private const string GetDomainPort80 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/Scripts/GetDomainNamePort80.sh";
private const string GetDomainPort443 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/Scripts/GetDomainNamePort443.sh";
private bool _stop;
public ContentFilter(ConcurrentQueue<QueueItem> queue, DbHandler dbHandler)
@ -54,6 +54,11 @@ public class ContentFilter
_queue.Enqueue(superUnfilteredObject);
if (_dbHandler.GetFilteredIp(unfiltered.Ip))
{
continue;
}
Filtered filtered = GetSiteData(unfiltered.Ip);
filtered.Port1 = unfiltered.Port1;
@ -230,7 +235,7 @@ public class ContentFilter
proc.Dispose();
}
}
public void Stop()
{
_stop = true;

View File

@ -29,20 +29,20 @@ public class ThreadHandler
public void Start()
{
//Thread scanner = new(StartScanner);
//Thread indexer = new(StartIndexer);
Thread scanner = new(StartScanner);
Thread indexer = new(StartIndexer);
Thread database = new(StartDbHandler);
Thread discarded = new(StartDiscardedDbHandler);
Thread communication = new(StartCommunicationHandler);
//scanner.Start();
//indexer.Start();
scanner.Start();
indexer.Start();
database.Start();
discarded.Start();
communication.Start();
//scanner.Join();
//indexer.Join();
scanner.Join();
indexer.Join();
database.Join();
discarded.Join();
communication.Join();
@ -107,21 +107,15 @@ public class ThreadHandler
Console.WriteLine("Communicator finished");
_communicationStopped = true;
Stop();
}
private void StopCommunicator()
{
Thread t = new(_communication.Stop);
t.Start();
t.Join();
}
public void Stop()
private void Stop()
{
_stopSignal = true;
_ipScanner.Stop();
_contentFilter.Stop();
StopCommunicator();
bool stopping = true;

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Backend")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Backend")]
[assembly: System.Reflection.AssemblyTitleAttribute("Backend")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
9371234864fbc6630f90d88b86a9165d6bf6b07556d735c0a3bcf7b4cfc5fd84
5e46353062a73d293516ee43511f79696b25ea1a0227ab06137d80abd08b8286

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Backend")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+981960411028790aaa0b551986f102c57a5995a2")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Backend")]
[assembly: System.Reflection.AssemblyTitleAttribute("Backend")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
82adc2dfb6f85265418a27693e5a67940d85f327127a5bc478825c0f31a766c5
ffe56baf943e027152af8f421cec2146e64a2c69fab0778620c627d36d497988

View File

@ -33,14 +33,6 @@ public static class Commands
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void GarbageCollect()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.GarbageCollect;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void Vacuum()
{
CommunicationObject communicationObject = new();
@ -59,15 +51,16 @@ public static class Commands
public static void GetHelp()
{
Console.WriteLine("Available commands:" + '\n');
Console.WriteLine(" stop - stops the server" + '\n');
Console.WriteLine(" clear - clears the console" + '\n');
Console.WriteLine(" q - quits the program" + '\n');
Console.WriteLine(" p - print the progress information of the scanner" + '\n');
Console.WriteLine(" g - manual garbage collect on the server" + '\n');
Console.WriteLine(" r - manual reindex the databases" + '\n');
Console.WriteLine(" v - manual vacuum the databases" + '\n');
Console.WriteLine(" help - shows this help" + '\n');
Console.WriteLine("Available commands:");
Console.WriteLine(" stop - stops the server");
Console.WriteLine(" clear - clears the console");
Console.WriteLine(" q - quits the program");
Console.WriteLine(" p - print the progress information of the scanner");
Console.WriteLine(" g - manual garbage collect on the server");
Console.WriteLine(" r - manual reindex the databases");
Console.WriteLine(" v - manual vacuum the databases");
Console.WriteLine(" help - shows this help");
Console.WriteLine();
}
private static string SendAndRecieveStringMessage(CommunicationObject communicationObject)
@ -79,7 +72,7 @@ public static class Commands
client.SendFrame(bytes);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return MessagePackSerializer.Deserialize<string>(msg, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));;
return MessagePackSerializer.Deserialize<string>(msg, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
}
private static ScanningStatus GetProgress(CommunicationObject communicationObject)

View File

@ -26,11 +26,6 @@ do
Commands.GetProgress();
}
else if (string.Equals(input, "g"))
{
Commands.GarbageCollect();
}
else if (string.Equals(input, "v"))
{
Commands.Vacuum();

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Manager")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Manager")]
[assembly: System.Reflection.AssemblyTitleAttribute("Manager")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
f2edcd1f3bfbc6ac17d0373459972a7238e7e008421899c57c54c58830729d72
0fa3b16d3588ae8d59b20d58a27e925f7ee529b8ffe8d511b4d20b1ba1172bd0

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Manager")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+981960411028790aaa0b551986f102c57a5995a2")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Manager")]
[assembly: System.Reflection.AssemblyTitleAttribute("Manager")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
17839627f63727ed46df8c31fee63615d813cf203af1f1d26871eb358af64608
cfcb6224cd1285029a88d6c786b98af9c68745343590d6b3a5e7df3d1051b9d0

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -337,6 +337,11 @@ public class DbHandler
return rowId;
}
public bool GetFilteredIp(string ip)
{
return true;
}
public List<SearchResult?> GetSearchResults()
{

View File

@ -5,7 +5,6 @@ public enum CommunicationCommand
GetScanningProgress,
GetSearches,
StopScanning,
GarbageCollect,
DbReindex,
DbVacuum,
}

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Models")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Models")]
[assembly: System.Reflection.AssemblyTitleAttribute("Models")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
c264b3f80882c325b9e2e4b7cd10e7b0a4b40661768b84672020f5eb89bc8917
3b3966ab846f2baa830e3dc9c894857e2451f35c409bfaa672762e55f8497742

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Models")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+981960411028790aaa0b551986f102c57a5995a2")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Models")]
[assembly: System.Reflection.AssemblyTitleAttribute("Models")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
44294b40a4aa21364ba671b64b9253eafd70429610d331a6cb9d9a86caccce54
bb0c2d9d87a7312699b453948dc650e35ab584804c7b493f054c01ebc45922a0

Binary file not shown.

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Proxy")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Proxy")]
[assembly: System.Reflection.AssemblyTitleAttribute("Proxy")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
05da10c6689ddb0150f956887c9c10297d5f44ae4f5beb7eb09467de83e057d8
49d6e226fd8b13f4ab95203aeabd4f3f6aca67566c8caa39ad073c3be3984969

View File

@ -1,4 +1,8 @@
is_global = true
build_property.EnableAotAnalyzer = true
build_property.EnableSingleFileAnalyzer = true
build_property.EnableTrimAnalyzer = true
build_property.IncludeAllContentForSelfExtract =
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb = true
@ -11,7 +15,7 @@ build_property.RootNamespace = Proxy
build_property.RootNamespace = Proxy
build_property.ProjectDir = /home/skingging/Documents/Projects/CSharp/RSE/Proxy/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EnableGeneratedComInterfaceComImportInterop = false
build_property.RazorLangVersion = 8.0
build_property.SupportLocalizedComponentNames =
build_property.GenerateRazorMetadataSourceChecksumAttributes =

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Proxy")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+981960411028790aaa0b551986f102c57a5995a2")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
[assembly: System.Reflection.AssemblyProductAttribute("Proxy")]
[assembly: System.Reflection.AssemblyTitleAttribute("Proxy")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
0d116f5b80940c9e87220c6e29fcdfed56c53c7d145d68cf7c5063eea6a56502
56bd2b2b42af5474e54e02105b0c8f03c11e0e385a3b239abc7e2cfe06394117

Binary file not shown.

Binary file not shown.

View File

@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Manager", "Manager\Manager.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Proxy", "Proxy\Proxy.csproj", "{55208481-5203-4B25-A20D-4EF644F76773}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{DEB1411C-F45A-40DA-92F8-D9B9929DBA5B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -30,5 +32,9 @@ Global
{55208481-5203-4B25-A20D-4EF644F76773}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55208481-5203-4B25-A20D-4EF644F76773}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55208481-5203-4B25-A20D-4EF644F76773}.Release|Any CPU.Build.0 = Release|Any CPU
{DEB1411C-F45A-40DA-92F8-D9B9929DBA5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEB1411C-F45A-40DA-92F8-D9B9929DBA5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEB1411C-F45A-40DA-92F8-D9B9929DBA5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEB1411C-F45A-40DA-92F8-D9B9929DBA5B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

5
Shared/Class1.cs Normal file
View File

@ -0,0 +1,5 @@
namespace Shared;
public class Class1
{
}

9
Shared/Shared.csproj Normal file
View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>