file-folder-helper/Day/Helper-2023-12-12.cs
Mike Phares 90380fdd43 CUDA -> ConvertId
KeePass -> ConvertKeePassExport
NMap -> SplitJsonFile
2023-12-25 17:10:26 -07:00

542 lines
23 KiB
C#

using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace File_Folder_Helper.Day;
internal static partial class Helper20231212
{
[GeneratedRegex(@"[\\,\/,\:,\*,\?,\"",\<,\>,\|]")]
private static partial Regex WindowsSafe();
private record Debugging(
[property: JsonPropertyName("Level")] int Level
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Debugging))]
private partial class DebuggingSourceGenerationContext : JsonSerializerContext
{
}
private record Distance(
[property: JsonPropertyName("Value")] int Value
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Distance))]
private partial class DistanceSourceGenerationContext : JsonSerializerContext
{
}
private record Finished(
[property: JsonPropertyName("Elapsed")] double Elapsed,
[property: JsonPropertyName("Exit")] string Exit,
[property: JsonPropertyName("Summary")] string Summary,
[property: JsonPropertyName("Time")] int Time,
[property: JsonPropertyName("TimeStr")] string TimeStr
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Finished))]
private partial class FinishedSourceGenerationContext : JsonSerializerContext
{
}
private record Hop(
[property: JsonPropertyName("Host")] string Host,
[property: JsonPropertyName("IPAddr")] string IPAddr,
[property: JsonPropertyName("RTT")] double RTT,
[property: JsonPropertyName("TTL")] int TTL
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Hop))]
private partial class HopSourceGenerationContext : JsonSerializerContext
{
}
private record Host(
[property: JsonPropertyName("Distance")] Distance Distance,
[property: JsonPropertyName("EndTime")] int EndTime,
[property: JsonPropertyName("HostAddress")] IReadOnlyList<HostAddress> HostAddresses,
[property: JsonPropertyName("HostNames")] HostNames HostNames,
[property: JsonPropertyName("IPIDSequence")] IPIDSequence IPIDSequence,
[property: JsonPropertyName("OS")] OS OS,
[property: JsonPropertyName("Port")] IReadOnlyList<Port> Ports,
[property: JsonPropertyName("StartTime")] int StartTime,
[property: JsonPropertyName("Status")] Status Status,
[property: JsonPropertyName("TCPSequence")] TCPSequence TCPSequence,
[property: JsonPropertyName("TCPTSSequence")] TCPTSSequence TCPTSSequence,
[property: JsonPropertyName("Trace")] Trace Trace,
[property: JsonPropertyName("Uptime")] Uptime Uptime
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Host))]
private partial class HostSourceGenerationContext : JsonSerializerContext
{
}
private record HostAddress(
[property: JsonPropertyName("Address")] string Address,
[property: JsonPropertyName("AddressType")] string AddressType,
[property: JsonPropertyName("Vendor")] string Vendor
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(HostAddress))]
private partial class HostAddressSourceGenerationContext : JsonSerializerContext
{
}
private record HostName(
[property: JsonPropertyName("Name")] string Name,
[property: JsonPropertyName("Type")] string Type
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(HostName))]
private partial class HostNameSourceGenerationContext : JsonSerializerContext
{
}
private record HostNames(
[property: JsonPropertyName("HostName")] IReadOnlyList<HostName> HostName
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(HostNames))]
private partial class HostNamesSourceGenerationContext : JsonSerializerContext
{
}
private record Hosts(
[property: JsonPropertyName("Down")] int Down,
[property: JsonPropertyName("Total")] int Total,
[property: JsonPropertyName("Up")] int Up
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Hosts))]
private partial class HostsSourceGenerationContext : JsonSerializerContext
{
}
private record IPIDSequence(
[property: JsonPropertyName("Class")] string Class,
[property: JsonPropertyName("Values")] string Values
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(IPIDSequence))]
private partial class IPIDSequenceSourceGenerationContext : JsonSerializerContext
{
}
private record OS(
[property: JsonPropertyName("OSClass")] object OSClass,
[property: JsonPropertyName("OSMatch")] IReadOnlyList<OSMatch> OSMatches,
[property: JsonPropertyName("OSPortUsed")] IReadOnlyList<OSPortUsed> OSPortsUsed
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(OS))]
private partial class OSSourceGenerationContext : JsonSerializerContext
{
}
private record OSMatch(
[property: JsonPropertyName("Accuracy")] string Accuracy,
[property: JsonPropertyName("Line")] string Line,
[property: JsonPropertyName("Name")] string Name
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(OSMatch))]
private partial class OSMatchSourceGenerationContext : JsonSerializerContext
{
}
private record OSPortUsed(
[property: JsonPropertyName("PortID")] int PortID,
[property: JsonPropertyName("Protocol")] string Protocol,
[property: JsonPropertyName("State")] string State
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(OSPortUsed))]
private partial class OSPortUsedSourceGenerationContext : JsonSerializerContext
{
}
private record Port(
[property: JsonPropertyName("PortID")] int PortID,
[property: JsonPropertyName("Protocol")] string Protocol,
[property: JsonPropertyName("Script")] IReadOnlyList<Script> Scripts,
[property: JsonPropertyName("Service")] Service Service,
[property: JsonPropertyName("State")] State State
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Port))]
private partial class PortSourceGenerationContext : JsonSerializerContext
{
}
private record Record(
[property: JsonPropertyName("Args")] string Args,
[property: JsonPropertyName("Debugging")] Debugging Debugging,
[property: JsonPropertyName("Host")] List<Host> Hosts,
[property: JsonPropertyName("RunStats")] RunStats RunStats,
[property: JsonPropertyName("ScanInfo")] ScanInfo ScanInfo,
[property: JsonPropertyName("Scanner")] string Scanner,
[property: JsonPropertyName("Start")] int Start,
[property: JsonPropertyName("StartStr")] string StartStr,
[property: JsonPropertyName("Verbose")] Verbose Verbose,
[property: JsonPropertyName("Version")] string Version
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Record))]
private partial class RecordSourceGenerationContext : JsonSerializerContext
{
}
private record RunStats(
[property: JsonPropertyName("Finished")] Finished Finished,
[property: JsonPropertyName("Hosts")] Hosts Hosts
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(RunStats))]
private partial class RunStatsSourceGenerationContext : JsonSerializerContext
{
}
private record ScanInfo(
[property: JsonPropertyName("NumServices")] int NumServices,
[property: JsonPropertyName("Protocol")] string Protocol,
[property: JsonPropertyName("Services")] string Services,
[property: JsonPropertyName("Type")] string Type
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(ScanInfo))]
private partial class ScanInfoSourceGenerationContext : JsonSerializerContext
{
}
private record Script(
[property: JsonPropertyName("ID")] string ID,
[property: JsonPropertyName("Output")] string Output
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Script))]
private partial class ScriptSourceGenerationContext : JsonSerializerContext
{
}
private record Service(
[property: JsonPropertyName("Conf")] string Conf,
[property: JsonPropertyName("CPE")] IReadOnlyList<string> CPEs,
[property: JsonPropertyName("ExtraInfo")] string ExtraInfo,
[property: JsonPropertyName("Method")] string Method,
[property: JsonPropertyName("Name")] string Name,
[property: JsonPropertyName("Product")] string Product,
[property: JsonPropertyName("Version")] string Version
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Service))]
private partial class ServiceSourceGenerationContext : JsonSerializerContext
{
}
private record State(
[property: JsonPropertyName("Reason")] string Reason,
[property: JsonPropertyName("ReasonTTL")] string ReasonTTL,
[property: JsonPropertyName("State")] string Value
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(State))]
private partial class StateSourceGenerationContext : JsonSerializerContext
{
}
private record Status(
[property: JsonPropertyName("Reason")] string Reason,
[property: JsonPropertyName("State")] string State
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Status))]
private partial class StatusSourceGenerationContext : JsonSerializerContext
{
}
private record TCPSequence(
[property: JsonPropertyName("Difficulty")] string Difficulty,
[property: JsonPropertyName("Index")] string Index,
[property: JsonPropertyName("Values")] string Values
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(TCPSequence))]
private partial class TCPSequenceSourceGenerationContext : JsonSerializerContext
{
}
private record TCPTSSequence(
[property: JsonPropertyName("Class")] string Class,
[property: JsonPropertyName("Values")] string Values
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(TCPTSSequence))]
private partial class TCPTSSequenceSourceGenerationContext : JsonSerializerContext
{
}
private record Trace(
[property: JsonPropertyName("Hops")] IReadOnlyList<Hop> Hops,
[property: JsonPropertyName("Port")] int Port,
[property: JsonPropertyName("Protocol")] string Protocol
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Trace))]
private partial class TraceSourceGenerationContext : JsonSerializerContext
{
}
private record Uptime(
[property: JsonPropertyName("LastBoot")] string LastBoot,
[property: JsonPropertyName("Seconds")] int Seconds
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Uptime))]
private partial class UptimeSourceGenerationContext : JsonSerializerContext
{
}
private record Verbose(
[property: JsonPropertyName("Level")] int Level
);
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Verbose))]
private partial class VerboseSourceGenerationContext : JsonSerializerContext
{
}
private static string[] GetIPV4Segments(string value)
{
string[] result;
string[] subSegments = value.Split('.');
if (subSegments.Length != 4)
result = [];
else
{
if (!subSegments.All(l => int.TryParse(l, out _)))
result = [];
else
result = value.Split('.');
}
return result;
}
private static string[] GetMacAddressSegments(string value)
{
string[] result;
if (value.Length != 17)
result = [];
else
{
string v = value.ToLower();
if (v[2] is not ':' or '-' || v[5] is not ':' or '-' || v[8] is not ':' or '-' || v[11] is not ':' or '-' || v[14] is not ':' or '-')
result = [];
else
{
result = [$"{v[0]}{v[1]}", $"{v[3]}{v[4]}", $"{v[6]}{v[7]}", $"{v[9]}{v[10]}", $"{v[12]}{v[13]}", $"{v[15]}{v[16]}"];
}
}
return result;
}
private static ReadOnlyCollection<ReadOnlyCollection<string>> GetHostLinesSpaceSegments()
{
List<ReadOnlyCollection<string>> results = [];
string hostFile = "C:/Windows/System32/drivers/etc/hosts";
string[] lines = !File.Exists(hostFile) ? [] : File.ReadAllLines(hostFile);
foreach (string line in lines)
results.Add(new(line.Split(' ')));
return new(results);
}
internal static void SplitJsonFile(ILogger<Worker> logger, List<string> args)
{
string json;
string title;
Record? record;
string fileName;
FileInfo fileInfo;
string checkFileName;
string sourceFileName;
string[] ipV4Segments;
string outputDirectory;
List<string> lines = [];
List<string> links = [];
List<string> allLines = [];
string[] macAddressSegments;
string macAddressWithHyphens;
string ipV4SegmentsWithPeriods;
List<string> titleSegments = [];
string fileNameWithoutExtension;
const int ipV4SegmentsLength = 4;
string sourceDirectory = args[0];
const int macAddressSegmentsLength = 6;
string[] fileNameWithoutExtensionSegments;
string fileNameWithoutExtensionFirstThreeSegments;
if (!Directory.Exists(sourceDirectory))
throw new Exception(sourceDirectory);
List<ReadOnlyCollection<string>> hostLineMatchSpaceSegments;
ReadOnlyCollection<ReadOnlyCollection<string>> hostLinesSpaceSegments = GetHostLinesSpaceSegments();
string[] files = Directory.GetFiles(sourceDirectory, args[2], SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
links.Clear();
fileInfo = new(file);
json = File.ReadAllText(file);
fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
outputDirectory = Path.Combine(sourceDirectory, fileNameWithoutExtension);
fileNameWithoutExtensionSegments = GetIPV4Segments(fileNameWithoutExtension);
if (fileNameWithoutExtensionSegments.Length != ipV4SegmentsLength)
continue;
fileNameWithoutExtensionFirstThreeSegments = $"{fileNameWithoutExtensionSegments[0]}.{fileNameWithoutExtensionSegments[1]}.{fileNameWithoutExtensionSegments[2]}.";
if (!Directory.Exists(outputDirectory))
_ = Directory.CreateDirectory(outputDirectory);
record = JsonSerializer.Deserialize(json, RecordSourceGenerationContext.Default.Record);
if (record is null)
continue;
foreach (Host host in record.Hosts)
{
lines.Clear();
ipV4Segments = [];
titleSegments.Clear();
macAddressSegments = [];
foreach (HostAddress hostAddress in host.HostAddresses)
{
if (hostAddress.AddressType == "ipv4")
ipV4Segments = GetIPV4Segments(hostAddress.Address);
else if (hostAddress.AddressType == "mac")
macAddressSegments = GetMacAddressSegments(hostAddress.Address);
else
continue;
}
if (ipV4Segments.Length != ipV4SegmentsLength)
continue;
ipV4SegmentsWithPeriods = string.Join('.', ipV4Segments);
macAddressWithHyphens = string.Join('-', macAddressSegments);
if (macAddressSegments.Length != macAddressSegmentsLength)
hostLineMatchSpaceSegments = (from l in hostLinesSpaceSegments where l.Count > 0 && l.Contains(ipV4SegmentsWithPeriods) select l).ToList();
else
{
hostLineMatchSpaceSegments = (from l in hostLinesSpaceSegments where l.Count > 0 && l.Contains(macAddressWithHyphens) select l).ToList();
if (hostLineMatchSpaceSegments.Count == 1)
title = string.Join(' ', hostLineMatchSpaceSegments[0]);
else
hostLineMatchSpaceSegments = (from l in hostLineMatchSpaceSegments where l.Count > 0 && l[0].StartsWith(fileNameWithoutExtensionFirstThreeSegments) select l).ToList();
}
if (ipV4Segments.Length == ipV4SegmentsLength)
titleSegments.Add(ipV4SegmentsWithPeriods);
if (hostLineMatchSpaceSegments.Count == 1)
title = $"{ipV4SegmentsWithPeriods} {string.Join(' ', hostLineMatchSpaceSegments[0].Skip(1))}";
else
{
if (host.HostNames.HostName is null)
titleSegments.Add("unknown #");
else
titleSegments.Add($"{string.Join("_", host.HostNames.HostName.Select(l => l.Name.Replace(' ', '-')))} #");
if (macAddressSegments.Length == macAddressSegmentsLength)
titleSegments.Add(macAddressWithHyphens);
title = string.Join(" ", titleSegments);
logger.LogInformation("{title} Type DeviceInfo ~ needs to be added to host file!", title);
}
json = JsonSerializer.Serialize(host, HostSourceGenerationContext.Default.Host);
fileNameWithoutExtension = macAddressSegments.Length != macAddressSegmentsLength ? $"ipv4-{string.Join(string.Empty, ipV4Segments)}" : $"mac-{string.Join(string.Empty, macAddressSegments)}";
fileName = $"{fileNameWithoutExtension}.md";
links.Add($"- [{title}]({fileName})");
lines.Add("---");
allLines.Add(title);
lines.Add(string.Empty);
lines.Add($"# {title}");
lines.Add(string.Empty);
lines.Add($"## {fileNameWithoutExtension}");
if (host.Ports is not null)
{
lines.Add(string.Empty);
lines.Add("## Port(s)");
lines.Add(string.Empty);
lines.Add("| Id | Protocol | State | Service |");
lines.Add("| - | - | - | - |");
foreach (Port port in host.Ports)
lines.Add($"| {port.PortID} | {port.Protocol} | {port.State.Value} | {port.Service.Name} |");
}
lines.Add(string.Empty);
lines.Add("```json");
lines.Add(json);
lines.Add("```");
checkFileName = Path.Combine(outputDirectory, fileName);
if (File.Exists(checkFileName))
File.Delete(checkFileName);
File.WriteAllLines(checkFileName, lines);
File.SetCreationTime(checkFileName, fileInfo.CreationTime);
File.SetLastWriteTime(checkFileName, fileInfo.LastWriteTime);
logger.LogInformation("{title} created", title);
}
lines.Clear();
record.Hosts.Clear();
title = Path.GetFileNameWithoutExtension(file);
json = JsonSerializer.Serialize(record, RecordSourceGenerationContext.Default.Record);
lines.Add("---");
lines.Add(string.Empty);
lines.Add($"# {title}");
lines.Add(string.Empty);
lines.Add($"## {title}");
lines.Add(string.Empty);
lines.Add("## Hosts");
lines.Add(string.Empty);
lines.AddRange(links);
lines.Add(string.Empty);
lines.Add("```json");
lines.Add(json);
lines.Add("```");
checkFileName = Path.Combine(outputDirectory, $"{title}.md");
if (File.Exists(checkFileName))
File.Delete(checkFileName);
File.WriteAllLines(checkFileName, lines);
File.SetCreationTime(checkFileName, fileInfo.CreationTime);
File.SetLastWriteTime(checkFileName, fileInfo.LastWriteTime);
logger.LogInformation("{title} created", title);
checkFileName = Path.Combine(outputDirectory, Path.GetFileName(file));
if (File.Exists(checkFileName))
File.Delete(checkFileName);
File.Move(file, checkFileName);
checkFileName = Path.Combine(outputDirectory, $"{Path.GetFileNameWithoutExtension(file)}.xml");
if (File.Exists(checkFileName))
File.Delete(checkFileName);
sourceFileName = Path.ChangeExtension(file, ".xml");
if (File.Exists(sourceFileName))
File.Move(sourceFileName, checkFileName);
}
File.WriteAllLines(Path.Combine(sourceDirectory, $"{DateTime.Now.Ticks}.ssv"), allLines);
}
}