diff --git a/ZeroLevel/Services/Network/Utils/Networks/Network.cs b/ZeroLevel/Services/Network/Utils/Networks/Network.cs new file mode 100644 index 0000000..84a7360 --- /dev/null +++ b/ZeroLevel/Services/Network/Utils/Networks/Network.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; +using System.Net; + +namespace ZeroLevel.Services.Network.Utils +{ + /// + /// Methods related to Network. + /// + public static class Network + { + /// + /// Gets the external IP Address. + /// + /// The external IP Address. + public static string ExternalIP + { + get + { + try + { + WebRequest request = WebRequest.Create("http://ipv4.icanhazip.com"); + using (var response = request.GetResponse()) + using (var sr = new StreamReader(response.GetResponseStream())) + { + return sr.ReadLine(); + } + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return ""; + } + } + } + } +} diff --git a/ZeroLevel/Services/Network/Utils/Networks/Wired.cs b/ZeroLevel/Services/Network/Utils/Networks/Wired.cs new file mode 100644 index 0000000..a0f3e7b --- /dev/null +++ b/ZeroLevel/Services/Network/Utils/Networks/Wired.cs @@ -0,0 +1,104 @@ +using System; +using System.Net.NetworkInformation; +using System.Net.Sockets; + +namespace ZeroLevel.Services.Network.Utils.Networks +{ + /// + /// Wired Network. + /// + public static class Wired + { + /// + /// Gets the interface name. + /// + /// The interface name. + public static string Name + { + get + { + try + { + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) + { + return item.Name; + } + } + + return ""; + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return ""; + } + } + } + + /// + /// Gets a value indicating if wired network is up. + /// + /// true if network is up; otherwise, false. + public static bool IsUp + { + get + { + try + { + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) + { + return true; + } + } + + return false; + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return false; + } + } + } + + /// + /// Gets the IP address. + /// + /// The IP address. + public static string IPAddress + { + get + { + try + { + string output = ""; + + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet && item.OperationalStatus == OperationalStatus.Up) + { + foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + output = ip.Address.ToString(); + } + } + } + } + + return output; + } + catch (Exception e) + { + Console.WriteLine("Error: {0}", e.Message); + return ""; + } + } + } + } +} diff --git a/ZeroLevel/Services/Network/Utils/Networks/Wireless.cs b/ZeroLevel/Services/Network/Utils/Networks/Wireless.cs new file mode 100644 index 0000000..74792bd --- /dev/null +++ b/ZeroLevel/Services/Network/Utils/Networks/Wireless.cs @@ -0,0 +1,138 @@ +using System; +using System.Diagnostics; +using System.Net.NetworkInformation; +using System.Net.Sockets; + +namespace ZeroLevel.Services.Network.Utils +{ + /// + /// Wireless network. + /// + public static class Wireless + { + /// + /// Gets a value indicating if wireless network is up. + /// + /// true if network is up; otherwise, false. + public static bool IsUp + { + get + { + try + { + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && item.OperationalStatus == OperationalStatus.Up) + { + return true; + } + } + + return false; + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return false; + } + } + } + + /// + /// Gets the interface name. + /// + /// The interface name. + public static string Name + { + get + { + try + { + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && item.OperationalStatus == OperationalStatus.Up) + { + return item.Name; + } + } + + return ""; + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return ""; + } + } + } + + /// + /// Gets the Wireless SSID + /// + /// The Wireless SSID + public static string SSID + { + get + { + try + { + using (var pr = new Process()) + { + pr.StartInfo = new ProcessStartInfo + { + FileName = "iwgetid", + Arguments = "-r", + RedirectStandardOutput = true, + UseShellExecute = false, + }; + + pr.Start(); + + return pr.StandardOutput.ReadLine(); + } + } + catch (Exception e) + { + Console.WriteLine("Error: " + e.Message); + return ""; + } + } + } + + /// + /// Gets the IP address. + /// + /// The IP address. + public static string IPAddress + { + get + { + try + { + string output = ""; + + foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) + { + if (item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && item.OperationalStatus == OperationalStatus.Up) + { + foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + output = ip.Address.ToString(); + } + } + } + } + + return output; + } + catch (Exception e) + { + Console.WriteLine("Error: {0}", e.Message); + return ""; + } + } + } + } +} \ No newline at end of file