From b1989f1619d6fc623a925827fc7bafa29f73f089 Mon Sep 17 00:00:00 2001 From: Mikhail Kopochinskiy Date: Sun, 5 Jan 2025 19:47:49 +0500 Subject: [PATCH] =?UTF-8?q?feat[linconsol]:=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BF=D1=83=D0=B1=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9,=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20IP=20=D0=B2=20IP=20Info?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linConsol.cs | 136 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 90 insertions(+), 46 deletions(-) diff --git a/linConsol.cs b/linConsol.cs index 465ca83..776f53b 100644 --- a/linConsol.cs +++ b/linConsol.cs @@ -1,10 +1,10 @@ -using System; -using System.Net.NetworkInformation; -using System.Net; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using System.Net.Sockets; static class ProgramInfo { - public static readonly string Version = "0.0.10\n Release date: 2025.01.01"; + public static readonly string Version = "0.0.11\n Release date: 2025.01.05"; public static readonly string Name = "LINCONSOL"; } @@ -14,57 +14,76 @@ static void Main(string[] args) { Console.WriteLine($"Welcome to {ProgramInfo.Name}!"); Console.WriteLine($"Enter `--help` for list of available commands."); - Console.WriteLine("Enter `--exit` to exit from console."); while (true) { Console.Write("\n> "); string input = Console.ReadLine()?.Trim(); - if (input.Equals("--exit", StringComparison.OrdinalIgnoreCase)) + if (string.IsNullOrWhiteSpace(input)) { - break; + Console.WriteLine("Please enter a command.\nType `--help` for assistance."); + continue; } ProcessCommand(input); } } + static readonly Dictionary CommandHandlers = new() + { + { "--help", ShowHelp }, + { "--version", ShowVersion }, + { "--exit", ExitProgram }, + { "osinfo", ShowOSInfo }, + { "ipinfo", () => ShowIPAddress().Wait() }, + { "ping", CheckInternetConnection } + }; + static void ProcessCommand(string command) { - switch (command.ToLower()) - { - case "--help": - ShowHelp(); - break; - case "--version": - ShowVersion(); - break; - case "osinfo": - ShowOSInfo(); - break; - case "ipaddress": - ShowIPAddress(); - break; - case "internetconn": - CheckInternetConnection(); - break; - default: + if (CommandHandlers.TryGetValue(command.ToLower(), out var handler)) + { + handler(); + } + else + { Console.WriteLine($"Unknown command `{command}`."); - Console.WriteLine($"Enter `--help` for list of available commands."); - break; + SuggestClosestCommand(command); + Console.WriteLine($"Or enter `--help` for list of available commands."); + } + } + + static void SuggestClosestCommand(string input) + { + var suggestions = CommandHandlers.Keys.Where(cmd => cmd.Contains(input)).ToList(); + + if (suggestions.Any()) + { + Console.WriteLine("Did you mean: "); + foreach (var suggestion in suggestions) + { + Console.WriteLine($" {suggestion}"); + } } } + static void ExitProgram() + { + Environment.Exit(0); + } + static void ShowHelp() { + string appName = ProgramInfo.Name.ToLower(); + Console.WriteLine($"<==========>{ProgramInfo.Name} COMMANDS<==========>"); - Console.WriteLine($" --help : Get all available {ProgramInfo.Name.ToLower()} commands."); - Console.WriteLine($" --version : Get current version of {ProgramInfo.Name.ToLower()}."); - Console.WriteLine($" --exit : Exit from {ProgramInfo.Name.ToLower()} console."); + Console.WriteLine($" --help : Get all available {appName} commands."); + Console.WriteLine($" --version : Get current version of {appName}."); + Console.WriteLine($" --exit : Exit from {appName} console."); Console.WriteLine(" osinfo : Get OS system information."); - Console.WriteLine(" ipaddress : Get IP-address of current device."); - Console.WriteLine(" internetconn : Check internet connection on device."); + Console.WriteLine(" ipinfo : Get IP-address of current device."); + Console.WriteLine(" ping : Check internet connection on device."); Console.WriteLine("<========================================>"); } @@ -81,38 +100,58 @@ static string FindSystemType() } Console.WriteLine("<==========>OS INFO<==========>"); - Console.WriteLine($" OS : {Environment.OSVersion}"); + Console.WriteLine($" OS : {RuntimeInformation.OSDescription}"); Console.WriteLine($" System Type : {FindSystemType()}"); Console.WriteLine($" PC-name : {Environment.MachineName}"); Console.WriteLine($" Username : {Environment.UserName}"); Console.WriteLine("<=============================>"); } - static void ShowIPAddress() + static async Task GetPublicIPAddress() { - string ipAddress = "Unknown"; - + using HttpClient client = new(); try { - string hostName = Dns.GetHostName(); - var ipAddresses = Dns.GetHostAddresses(hostName); + string publicIp = await client.GetStringAsync("https://api.ipify.org"); + return publicIp.Trim(); + } + catch (Exception ex) + { + Console.WriteLine($"Error fetching public IP address: {ex.Message}"); + return "Unknown"; + } + } + + static async Task ShowIPAddress() + { + string localIpAddress = "Unknown"; + string publicIpAddress = await GetPublicIPAddress(); - foreach (var ip in ipAddresses) + try + { + foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { - if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + if (ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback) { - ipAddress = ip.ToString(); - break; + foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) + { + if (ip.Address.AddressFamily == AddressFamily.InterNetwork) + { + localIpAddress = ip.Address.ToString(); + break; + } + } } } } catch (Exception ex) { - Console.WriteLine($"Error getting ip address: {ex.Message}"); + Console.WriteLine($"Error getting local IP address: {ex.Message}"); } - Console.WriteLine("<==========>IP ADDRESS<==========>"); - Console.WriteLine($" IP : {ipAddress}"); + Console.WriteLine("<==========>IP INFO<==========>"); + Console.WriteLine($" Local IP : {localIpAddress}"); + Console.WriteLine($" Public IP : {publicIpAddress}"); Console.WriteLine(" Location : not available now"); Console.WriteLine(" Provider : not available now"); Console.WriteLine("<=============================>"); @@ -127,6 +166,7 @@ static void CheckInternetConnection() PingReply reply = ping.Send("8.8.8.8", 3000); // google dns if (reply.Status == IPStatus.Success) { + Console.WriteLine("pong!"); Console.WriteLine("Internet connection is STABLE."); } else @@ -135,9 +175,13 @@ static void CheckInternetConnection() } } } + catch (PingException ex) + { + Console.WriteLine($"Network error: Unable to reach the server.\n {ex.Message}."); + } catch (Exception ex) { - Console.WriteLine($"Error checking internet connection: {ex.Message}"); + Console.WriteLine($"Unexpected error: {ex.Message}."); } } }