diff --git a/netxml2kml/Methods/CliOptionsHandlers.cs b/netxml2kml/Methods/CliOptionsHandlers.cs new file mode 100644 index 0000000..99d832b --- /dev/null +++ b/netxml2kml/Methods/CliOptionsHandlers.cs @@ -0,0 +1,17 @@ +using netxml2kml.Models; + +namespace netxml2kml.Methods; + +public static class CliOptionsHandlers +{ + public static void UniversalHandler(FileInfo? inputFile, + FileInfo? outputFile) + { + + } + + private static string GenerateKml(WirelessNetwork[] wirelessNetworks) + { + return "test"; + } +} \ No newline at end of file diff --git a/netxml2kml/Methods/Helpers.cs b/netxml2kml/Methods/Helpers.cs new file mode 100644 index 0000000..c90c651 --- /dev/null +++ b/netxml2kml/Methods/Helpers.cs @@ -0,0 +1,105 @@ +using System.Xml; +using System.Xml.Serialization; +using netxml2kml.Models; + +namespace netxml2kml.Methods; + +public static class Helpers +{ + public static WirelessNetwork[] DeserializeXml(FileInfo inputFile) + { + /* Deserialize to MonoXSD autogenerated class model */ + + var xmlSerializer = new XmlSerializer(typeof(detectionrun)); + var detectionRun = (detectionrun?) xmlSerializer.Deserialize( + XmlReader.Create(inputFile.OpenRead(), + new XmlReaderSettings {DtdProcessing = DtdProcessing.Parse})); + + /* Convert machine deserialization to readable class format */ + + var wirelessNetworks = + new WirelessNetwork[detectionRun!.wirelessnetwork.Count(wn => + wn.type != "probe")]; + + for (int i = 0; i < detectionRun.wirelessnetwork.Length; i++) + { + var detectionRunWirelessNetwork = detectionRun.wirelessnetwork[i]; + + // If wireless network is a client – skip + if (detectionRunWirelessNetwork.type == "probe") + { + continue; + } + + wirelessNetworks[i] = new() + { + Essid = detectionRunWirelessNetwork.SSID.First().essid?.First().Value, + Encryption = detectionRunWirelessNetwork.SSID.First().encryption?.Last()?.Value, + Bssid = detectionRunWirelessNetwork.BSSID, + Manufacturer = detectionRunWirelessNetwork.manuf, + FrequencyMhz = Math.Round(Double.Parse(detectionRunWirelessNetwork.freqmhz.Substring(0, 2)) / 10, 1, MidpointRounding.ToPositiveInfinity), + MaxSignalDbm = Int32.Parse(detectionRunWirelessNetwork.snrinfo.First().max_signal_dbm), + MaxLatitude = Double.Parse(detectionRunWirelessNetwork.gpsinfo.First().maxlat), + MaxLongitude = Double.Parse(detectionRunWirelessNetwork.gpsinfo.First().maxlon), + MaxAltitude = Double.Parse(detectionRunWirelessNetwork.gpsinfo.First().maxalt), + FirstSeenDate = StringToDate(detectionRunWirelessNetwork.firsttime), + LastUpdateDate = StringToDate(detectionRunWirelessNetwork.lasttime) + }; + + // If wireless network has no clients – continue + if (detectionRunWirelessNetwork.wirelessclient == null) + { + continue; + } + + var wirelessConnections = new List(detectionRunWirelessNetwork.wirelessclient.Length); + + foreach (var wc in detectionRunWirelessNetwork.wirelessclient) + { + wirelessConnections.Add(new WirelessConnection + { + WirelessClient = new WirelessClient + { + Mac = wc.clientmac, + Manufacturer = wc.clientmanuf, + FirstSeenDate = StringToDate(wc.firsttime), + LastUpdateDate = StringToDate(wc.lasttime) + }, + WirelessNetwork = wirelessNetworks[i] + }); + } + + wirelessNetworks[i].WirelessConnections = wirelessConnections; + } + + return wirelessNetworks; + } + + public static DateTime StringToDate(string dateString) + { + var monthNameNumber = new Dictionary + { + {"Jan", 1}, + {"Feb", 2}, + {"Mar", 3}, + {"Apr", 4}, + {"May", 5}, + {"Jun", 6}, + {"Jul", 7}, + {"Aug", 8}, + {"Sep", 9}, + {"Oct", 10}, + {"Nov", 11}, + {"Dec", 12}, + }; + + var year = Int32.Parse(dateString.Split(" ")[4]); + var month = monthNameNumber[dateString.Split(" ")[1]]; + var day = Int32.Parse(dateString.Split(" ")[2]); + var hour = Int32.Parse(dateString.Split(" ")[3].Split(":")[0]); + var minute = Int32.Parse(dateString.Split(" ")[3].Split(":")[1]); + var second = Int32.Parse(dateString.Split(" ")[3].Split(":")[2]); + + return new DateTime(year, month, day, hour, minute, second); + } +} \ No newline at end of file diff --git a/netxml2kml/Program.cs b/netxml2kml/Program.cs index af0d82d..4a08501 100644 --- a/netxml2kml/Program.cs +++ b/netxml2kml/Program.cs @@ -1,9 +1,5 @@ using System.CommandLine; -using System.Text; -using System.Xml; -using System.Xml.Serialization; -using netxml2kml.Data; -using netxml2kml.Models; +using netxml2kml.Methods; namespace netxml2kml; @@ -25,85 +21,7 @@ class Program rootCommand.AddOption(inputOption); rootCommand.AddOption(outputOption); - rootCommand.SetHandler((inputFile, outputFile) => - { - if (!IsValidArguments(inputFile, ref outputFile, - out string validationError)) - { - Console.WriteLine(validationError); - return; - } - - var serializer = new XmlSerializer(typeof(detectionrun)); - var detectionRun = (detectionrun?) serializer.Deserialize(XmlReader.Create(inputFile.OpenRead(), new XmlReaderSettings {DtdProcessing = DtdProcessing.Parse})); - - var streamWriter = new StreamWriter(outputFile.OpenWrite()); - streamWriter.Write("test"); - streamWriter.Close(); - - bool IsValidArguments(FileInfo? inFile, ref FileInfo? outFile, - out string validationErr) - { - if (inFile == null) - { - validationErr = "You must specify an input file."; - return false; - } - - if (!inFile.Exists) - { - validationErr = "Input file doesn't exist."; - return false; - } - - // If output file is not specified – set name to the - // name of an input file with .kml extension - if (outFile == null) - { - outFile = new FileInfo(Path.Join(inFile.DirectoryName, - $"{inFile.Name.Substring(0, inFile.Name.IndexOf(".", StringComparison.Ordinal))}.kml")); - } - - if (!Directory.Exists(outFile.DirectoryName)) - { - validationErr = "Output directory doesn't exist."; - return false; - } - - // If output file with the same name already exists – - // prompt user to change a name of the file - while (outFile.Exists) - { - Console.Write("Output file is already exists. Do you want to overwrite it? [y/N] "); - var opt = Console.ReadLine(); - - if (String.IsNullOrEmpty(opt) || opt.ToLower() == "no" || - opt.ToLower() == "n") - { - Console.Write("Enter a [.kml]: "); - var name = Console.ReadLine(); - - if (String.IsNullOrEmpty(name)) - { - continue; - } - - outFile = new FileInfo( - Path.Join(outFile.DirectoryName, name)); - continue; - } - - if (opt.ToLower() == "yes" || - opt.ToLower() == "y") - { - break; - } - } - - validationErr = string.Empty; - return true; - } - }, + rootCommand.SetHandler(CliOptionsHandlers.UniversalHandler, inputOption, outputOption); return await rootCommand.InvokeAsync(args);