RemoveHistoryAfter
This commit is contained in:
parent
84c5355dcf
commit
44cb864891
@ -23,49 +23,59 @@ internal static class HelperPhysicalAddress
|
||||
private static string GetPhysicalAddress(PhysicalAddress physicalAddress) =>
|
||||
$"{GetPhysicalAddress(physicalAddress, '-')} | {GetPhysicalAddress(physicalAddress, ':')}";
|
||||
|
||||
private static void AddPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, string physicalAddress, string ipv4Address, bool isSuggestion)
|
||||
private static void WriteAllText(ILogger<Worker> logger, string file, string physicalAddress, string ipv4Address, List<string> collection, Dictionary<string, List<string>> keyValuePairs)
|
||||
{
|
||||
List<string>? collection;
|
||||
Dictionary<string, List<string>> keyValuePairs = GetKeyValuePairs(file);
|
||||
if (!keyValuePairs.TryGetValue(physicalAddress, out collection))
|
||||
logger.LogInformation("");
|
||||
if (collection.Count == 2)
|
||||
collection[1] = DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
|
||||
else if (collection.Count == 0)
|
||||
{
|
||||
keyValuePairs.Add(physicalAddress, []);
|
||||
if (!keyValuePairs.TryGetValue(physicalAddress, out collection))
|
||||
throw new Exception();
|
||||
collection.Add($"block-{physicalAddress[^2..]}");
|
||||
collection.Add(DateTime.Now.ToString("yyyy-MM-dd_HH-mm"));
|
||||
}
|
||||
if (ipv4Address.StartsWith(physicalAddressConfiguration.IPV4Filter) && (collection.Count < 2 || (!isSuggestion && collection[^1] != ipv4Address)))
|
||||
if (collection.Count > 1)
|
||||
{
|
||||
logger.LogInformation("");
|
||||
if (collection.Count == 2)
|
||||
collection[1] = DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
|
||||
else if (collection.Count == 0)
|
||||
{
|
||||
collection.Add($"block-{physicalAddress[^2..]}");
|
||||
collection.Add(DateTime.Now.ToString("yyyy-MM-dd_HH-mm"));
|
||||
}
|
||||
if (collection.Count > 1)
|
||||
{
|
||||
logger.LogInformation(collection[0]);
|
||||
logger.LogInformation(collection[1]);
|
||||
}
|
||||
if (collection.Remove(ipv4Address))
|
||||
collection[1] = DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
|
||||
collection.Add(ipv4Address);
|
||||
logger.LogInformation(ipv4Address);
|
||||
logger.LogInformation(physicalAddress);
|
||||
string json = JsonSerializer.Serialize(keyValuePairs, HelperPhysicalAddressDictionarySourceGenerationContext.Default.DictionaryStringListString);
|
||||
File.WriteAllText(file, json);
|
||||
logger.LogInformation(collection[0]);
|
||||
logger.LogInformation(collection[1]);
|
||||
}
|
||||
if (collection.Remove(ipv4Address))
|
||||
collection[1] = DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
|
||||
collection.Add(ipv4Address);
|
||||
logger.LogInformation(ipv4Address);
|
||||
logger.LogInformation(physicalAddress);
|
||||
string json = JsonSerializer.Serialize(keyValuePairs, HelperPhysicalAddressDictionarySourceGenerationContext.Default.DictionaryStringListString);
|
||||
File.WriteAllText(file, json);
|
||||
}
|
||||
|
||||
private static void AddArpPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, EthernetPacket ethernetPacket, ArpPacket arpPacket)
|
||||
private static void AddPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, List<string> history, string physicalAddress, string ipv4Address, bool isSuggestion)
|
||||
{
|
||||
List<string>? collection;
|
||||
string key = $"{physicalAddress} - {ipv4Address}";
|
||||
if (!history.Contains(key))
|
||||
{
|
||||
Dictionary<string, List<string>> keyValuePairs = GetKeyValuePairs(file);
|
||||
if (!keyValuePairs.TryGetValue(physicalAddress, out collection))
|
||||
{
|
||||
keyValuePairs.Add(physicalAddress, []);
|
||||
if (!keyValuePairs.TryGetValue(physicalAddress, out collection))
|
||||
throw new Exception();
|
||||
}
|
||||
if (ipv4Address.StartsWith(physicalAddressConfiguration.IPV4Filter) && (collection.Count < 2 || (!isSuggestion && collection[^1] != ipv4Address)))
|
||||
WriteAllText(logger, file, physicalAddress, ipv4Address, collection, keyValuePairs);
|
||||
}
|
||||
history.Add(key);
|
||||
if (history.Count > physicalAddressConfiguration.RemoveHistoryAfter)
|
||||
history.RemoveAt(0);
|
||||
}
|
||||
|
||||
private static void AddArpPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, List<string> history, EthernetPacket ethernetPacket, ArpPacket arpPacket)
|
||||
{
|
||||
string ipv4Address = arpPacket.SenderProtocolAddress.ToString();
|
||||
string physicalAddress = GetPhysicalAddress(ethernetPacket.SourceHardwareAddress);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, physicalAddress, ipv4Address, isSuggestion: false);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, history, physicalAddress, ipv4Address, isSuggestion: false);
|
||||
}
|
||||
|
||||
private static void AddDhcpPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, DhcpV4Packet dhcpV4Packet)
|
||||
private static void AddDhcpPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, List<string> history, DhcpV4Packet dhcpV4Packet)
|
||||
{
|
||||
string ipv4Address;
|
||||
string physicalAddress;
|
||||
@ -74,24 +84,24 @@ internal static class HelperPhysicalAddress
|
||||
ipv4Address = dhcpV4Packet.ClientAddress.ToString();
|
||||
physicalAddress = GetPhysicalAddress(dhcpV4Packet.ClientHardwareAddress);
|
||||
bool isSuggestion = dhcpV4Packet.MessageType is DhcpV4MessageType.Request;
|
||||
AddPacket(physicalAddressConfiguration, logger, file, physicalAddress, ipv4Address, isSuggestion);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, history, physicalAddress, ipv4Address, isSuggestion);
|
||||
}
|
||||
else
|
||||
{
|
||||
ipv4Address = dhcpV4Packet.ClientAddress.ToString();
|
||||
physicalAddress = GetPhysicalAddress(dhcpV4Packet.ClientHardwareAddress);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, physicalAddress, ipv4Address, isSuggestion: true);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, history, physicalAddress, ipv4Address, isSuggestion: true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddIpv4Packet(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, EthernetPacket ethernetPacket, IPv4Packet ipv4Packet)
|
||||
private static void AddIpv4Packet(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, List<string> history, EthernetPacket ethernetPacket, IPv4Packet ipv4Packet)
|
||||
{
|
||||
string ipv4Address = ipv4Packet.SourceAddress.ToString();
|
||||
string physicalAddress = GetPhysicalAddress(ethernetPacket.SourceHardwareAddress);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, physicalAddress, ipv4Address, isSuggestion: true);
|
||||
AddPacket(physicalAddressConfiguration, logger, file, history, physicalAddress, ipv4Address, isSuggestion: true);
|
||||
}
|
||||
|
||||
private static void ParseEthernetPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, EthernetPacket ethernetPacket)
|
||||
private static void ParseEthernetPacket(PhysicalAddressConfiguration physicalAddressConfiguration, ILogger<Worker> logger, string file, List<string> history, EthernetPacket ethernetPacket)
|
||||
{
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
@ -99,7 +109,7 @@ internal static class HelperPhysicalAddress
|
||||
{
|
||||
if (arpPacket.Operation != ArpOperation.Response)
|
||||
continue;
|
||||
AddArpPacket(physicalAddressConfiguration, logger, file, ethernetPacket, arpPacket);
|
||||
AddArpPacket(physicalAddressConfiguration, logger, file, history, ethernetPacket, arpPacket);
|
||||
}
|
||||
else if (ethernetPacket?.PayloadPacket is IPv4Packet ipv4Packet)
|
||||
{
|
||||
@ -108,9 +118,9 @@ internal static class HelperPhysicalAddress
|
||||
if (!ipv4Packet.SourceAddress.ToString().StartsWith(physicalAddressConfiguration.IPV4Filter))
|
||||
continue;
|
||||
if (ipv4Packet.PayloadPacket is UdpPacket udpPacket && udpPacket.PayloadPacket is DhcpV4Packet dhcpV4Packet)
|
||||
AddDhcpPacket(physicalAddressConfiguration, logger, file, dhcpV4Packet);
|
||||
AddDhcpPacket(physicalAddressConfiguration, logger, file, history, dhcpV4Packet);
|
||||
else
|
||||
AddIpv4Packet(physicalAddressConfiguration, logger, file, ethernetPacket, ipv4Packet);
|
||||
AddIpv4Packet(physicalAddressConfiguration, logger, file, history, ethernetPacket, ipv4Packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -141,6 +151,7 @@ internal static class HelperPhysicalAddress
|
||||
Packet packet;
|
||||
RawCapture rawCapture;
|
||||
GetPacketStatus status;
|
||||
List<string> history = [];
|
||||
if (!Directory.Exists(appSettings.PhysicalAddressConfiguration.Directory))
|
||||
_ = Directory.CreateDirectory(appSettings.PhysicalAddressConfiguration.Directory);
|
||||
string file = Path.Combine(appSettings.PhysicalAddressConfiguration.Directory, ".json");
|
||||
@ -171,7 +182,7 @@ internal static class HelperPhysicalAddress
|
||||
packet = Packet.ParsePacket(rawCapture.GetLinkLayers(), rawCapture.Data);
|
||||
if (packet is not EthernetPacket ethernetPacket)
|
||||
continue;
|
||||
ParseEthernetPacket(appSettings.PhysicalAddressConfiguration, logger, file, ethernetPacket);
|
||||
ParseEthernetPacket(appSettings.PhysicalAddressConfiguration, logger, file, history, ethernetPacket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ public class PhysicalAddressConfiguration
|
||||
public string? Directory { get; set; }
|
||||
public string? IPV4Filter { get; set; }
|
||||
public int? ReadTimeoutMilliseconds { get; set; }
|
||||
public int? RemoveHistoryAfter { get; set; }
|
||||
public string? StringOutputType { get; set; }
|
||||
public bool? UseARP { get; set; }
|
||||
|
||||
@ -51,6 +52,7 @@ public class PhysicalAddressConfiguration
|
||||
if (configuration.Directory is null) throw new NullReferenceException(nameof(configuration.Directory));
|
||||
if (configuration.IPV4Filter is null) throw new NullReferenceException(nameof(configuration.IPV4Filter));
|
||||
if (configuration.ReadTimeoutMilliseconds is null) throw new NullReferenceException(nameof(configuration.ReadTimeoutMilliseconds));
|
||||
if (configuration.RemoveHistoryAfter is null) throw new NullReferenceException(nameof(configuration.RemoveHistoryAfter));
|
||||
if (configuration.StringOutputType is null) throw new NullReferenceException(nameof(configuration.StringOutputType));
|
||||
if (configuration.UseARP is null) throw new NullReferenceException(nameof(configuration.UseARP));
|
||||
Verify(configuration);
|
||||
@ -59,6 +61,7 @@ public class PhysicalAddressConfiguration
|
||||
configuration.Directory,
|
||||
configuration.IPV4Filter,
|
||||
configuration.ReadTimeoutMilliseconds.Value,
|
||||
configuration.RemoveHistoryAfter.Value,
|
||||
configuration.StringOutputType,
|
||||
configuration.UseARP.Value);
|
||||
return result;
|
||||
|
@ -8,6 +8,7 @@ public record PhysicalAddressConfiguration(string Description,
|
||||
string Directory,
|
||||
string IPV4Filter,
|
||||
int ReadTimeoutMilliseconds,
|
||||
int RemoveHistoryAfter,
|
||||
string StringOutputType,
|
||||
bool UseARP)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user