RSE/Manager/Commands.cs

125 lines
5.3 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Models.Model.Backend;
using Models.Model.External;
using NetMQ;
using NetMQ.Sockets;
namespace Manager;
public static class Commands
{
public static void GetProgress()
{
Console.WriteLine("Getting progress ...");
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.GetScanningProgress;
ScanningStatus temp = GetProgress(communicationObject);
Console.WriteLine($"Total filtered: {temp.TotalFiltered:n0}");
Console.WriteLine($"Total discarded: {temp.TotalDiscarded:n0}");
Console.WriteLine($"Total percentage scanned: {temp.PercentageOfIpv4Scanned}");
Console.WriteLine($"Total Ips left: {temp.AmountOfIpv4Left:n0}");
Console.WriteLine($"Filtered DB size: {temp.FilteredDbSize} Kb");
Console.WriteLine($"Discarded DB size: {temp.DiscardedDbSize} Kb");
Console.WriteLine($"Mydb DB size: {temp.MyDbSize} Kb");
}
public static void StopServer()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.StopScanning;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void Vacuum()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.DbVacuum;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void ReIndex()
{
CommunicationObject communicationObject = new();
communicationObject.Command = CommunicationCommand.DbReindex;
Console.WriteLine(SendAndRecieveStringMessage(communicationObject));
}
public static void SetRuntimeVariable(RuntimeVariable runtimeVariable, string value)
{
CommunicationObject communicationObject = new()
{
Command = CommunicationCommand.ChangeRuntimeVariable,
Variable = runtimeVariable.ToString(),
VariableValue = value
};
SendAndRecieveStringMessage(communicationObject);
}
public static void GetHelp()
{
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(" R - change runtime variable");
Console.WriteLine(" lR - list runtime variables");
Console.WriteLine(" help - shows this help");
Console.WriteLine();
}
public static void PrintRuntimeVariables()
{
Console.WriteLine("Runtime variables:");
Console.WriteLine($"{RuntimeVariable.ScannerTimeout.ToString()} - Sets the timeout in milliseconds for the scanner");
Console.WriteLine($"{RuntimeVariable.ContentFilter.ToString()} - Sets the timeout in milliseconds for the content filter");
Console.WriteLine($"{RuntimeVariable.DbContent.ToString()} - Sets the wait time in milliseconds for the content database if the queue is empty");
Console.WriteLine($"{RuntimeVariable.DbDiscarded.ToString()} - Sets the wait time in milliseconds for the discarded database if the queue is empty");
}
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)")]
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)")]
private static string? SendAndRecieveStringMessage(CommunicationObject communicationObject)
{
//byte[] bytes = MessagePackSerializer.Serialize(communicationObject, ContractlessStandardResolver.Options);
byte[] lol = JsonSerializer.SerializeToUtf8Bytes(communicationObject);
using RequestSocket client = new();
client.Connect("tcp://127.0.0.1:5556");
client.SendFrame(lol);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return JsonSerializer.Deserialize<string>(msg);
}
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)")]
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<TValue>(TValue, JsonSerializerOptions)")]
private static ScanningStatus GetProgress(CommunicationObject communicationObject)
{
//byte[] bytes = MessagePackSerializer.Serialize(communicationObject, ContractlessStandardResolver.Options);
byte[] lol = JsonSerializer.SerializeToUtf8Bytes(communicationObject);
using RequestSocket client = new();
client.Connect("tcp://127.0.0.1:5556");
client.SendFrame(lol);
byte[] msg = client.ReceiveFrameBytes();
client.Close();
return JsonSerializer.Deserialize<ScanningStatus>(msg);
}
}