diff --git a/netxml2kml/Methods/CliOptionsHandlers.cs b/netxml2kml/Methods/CliOptionsHandlers.cs index 7fc52f2..b95ca92 100644 --- a/netxml2kml/Methods/CliOptionsHandlers.cs +++ b/netxml2kml/Methods/CliOptionsHandlers.cs @@ -1,3 +1,4 @@ +using System.Xml.Linq; using Microsoft.EntityFrameworkCore; using netxml2kml.Data; using netxml2kml.Models; @@ -116,9 +117,86 @@ public static class CliOptionsHandlers } } - private static string GetKmlString(IEnumerable wirelessNetworks) + private static string GetKmlString(IEnumerable wirelessNetworks, + string name = "WiFi Map", + string description = $"Autogenerated by a tool.\nhttps://github.com/cuqmbr/netxml2kml") { - return "test"; + var kmlTree = new XDocument(); + + var kml = new XElement("kml"); + var document = new XElement("Document", + new XElement("name", $"{name}"), + new XElement("description", $"{description}")); + + var wpa3Folder = new XElement("Folder", + new XElement("name", "WPA3")); + var wpa2Folder = new XElement("Folder", + new XElement("name", "WPA2")); + var wpaFolder = new XElement("Folder", + new XElement("name", "WPA")); + var wepFolder = new XElement("Folder", + new XElement("name", "WEP")); + var opnFolder = new XElement("Folder", + new XElement("name", "OPN")); + var unknownFolder = new XElement("Folder", + new XElement("name", "UNKNOWN")); + + foreach (var wn in wirelessNetworks) + { + if (wn.Encryption == null) + { + AddWirelessNetworkPlacemark(unknownFolder, wn); + } + else if (wn.Encryption.Contains("None")) + { + AddWirelessNetworkPlacemark(opnFolder, wn); + } + else if (wn.Encryption.Contains("RC4")) + { + AddWirelessNetworkPlacemark(wepFolder, wn); + } + else if (wn.Encryption.Contains("TKIP")) + { + AddWirelessNetworkPlacemark(wpaFolder, wn); + } + else if (wn.Encryption.Contains("AES") || wn.Encryption.Contains("PSK")) + { + AddWirelessNetworkPlacemark(wpa2Folder, wn); + } + else if (wn.Encryption.Contains("SAE")) + { + AddWirelessNetworkPlacemark(wpa3Folder, wn); + } + else + { + AddWirelessNetworkPlacemark(unknownFolder, wn); + } + } + + document.Add(wpa3Folder); + document.Add(wpa2Folder); + document.Add(wpaFolder); + document.Add(wepFolder); + document.Add(opnFolder); + kml.Add(document); + kmlTree.Add(kml); + + return kmlTree.ToString(); + + void AddWirelessNetworkPlacemark(XElement parent, WirelessNetwork wn) + { + parent.Add(new XElement("Placemark", + new XElement("name", $"{wn.Essid}"), + new XElement("description", + $"Name: {wn.Essid}" + + $"\n\nManufacturer: {wn.Manufacturer}" + + $"\n\nBSSID: {wn.Bssid}" + + $"\n\nEncryption: {wn.Encryption}" + + $"\n\nUpdated: {wn.LastUpdateDate}"), + new XElement("Point", + new XElement("coordinates", + $"{wn.MaxLongitude},{wn.MaxLatitude},{wn.MaxAltitude}")))); + } } private static void AddWirelessNetworksToDatabase(IEnumerable wirelessNetworks) diff --git a/netxml2kml/Methods/Helpers.cs b/netxml2kml/Methods/Helpers.cs index 9f4bd04..fa19256 100644 --- a/netxml2kml/Methods/Helpers.cs +++ b/netxml2kml/Methods/Helpers.cs @@ -7,8 +7,6 @@ public static class Helpers { public static WirelessNetwork[] DeserializeXml(FileInfo inputFile) { - /* Deserialize to MonoXSD autogenerated class model */ - var srcTree = XDocument.Load(inputFile.OpenRead()); var srcNets = srcTree.Root!.Elements("wireless-network") .Where(wn => wn.Attribute("type")!.Value != "probe").ToList(); @@ -17,7 +15,6 @@ public static class Helpers for (int i = 0; i < srcNets.Count; i++) { - Console.WriteLine(i); var srcNet = srcNets[i]; var essid = srcNet.Element("SSID")!.Element("essid")!.Value; @@ -96,7 +93,7 @@ public static class Helpers public static void WriteStringToFile(string str, FileInfo file) { - var writer = new StreamWriter(file.OpenWrite()); + var writer = new StreamWriter(file.Open(FileMode.Create)); writer.Write(str); writer.Close(); }