|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.CommandLine; |
| 3 | +using System.CommandLine.NamingConventionBinder; |
| 4 | +using Spectre.Console; |
| 5 | +using Guppi.Core.Extensions; |
| 6 | +using System.Net; |
| 7 | +using System.Linq; |
| 8 | +using System.Net.Sockets; |
| 9 | +using Guppi.Core.Interfaces.Services; |
| 10 | +using System; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Net.NetworkInformation; |
| 13 | + |
| 14 | +namespace Guppi.Console.Skills; |
| 15 | + |
| 16 | +internal class IpSkill(IIPService service) : ISkill |
| 17 | +{ |
| 18 | + private readonly IIPService _service = service; |
| 19 | + |
| 20 | + public IEnumerable<Command> GetCommands() => [ |
| 21 | + new Command("ip", "Displays the IP address of the current machine.") |
| 22 | + { |
| 23 | + Handler = CommandHandler.Create(async () => await DisplayIp()) |
| 24 | + } |
| 25 | + ]; |
| 26 | + |
| 27 | + private async Task DisplayIp() |
| 28 | + { |
| 29 | + AnsiConsoleHelper.TitleRule($":tokyo_tower: IP Addresses"); |
| 30 | + |
| 31 | + try |
| 32 | + { |
| 33 | + var wanIp = await _service.GetWanIPAddress(); |
| 34 | + var interfaces = _service.GetNetworkInterfaces(); |
| 35 | + |
| 36 | + AnsiConsole.MarkupLine("[bold]WAN IP:[/]"); |
| 37 | + AnsiConsole.MarkupLine($" [cyan]{wanIp}[/]"); |
| 38 | + AnsiConsole.WriteLine(); |
| 39 | + AnsiConsole.MarkupLine("[bold]Local IPs:[/]"); |
| 40 | + |
| 41 | + foreach (NetworkInterface networkInterface in interfaces) |
| 42 | + { |
| 43 | + if (networkInterface.OperationalStatus == OperationalStatus.Up) |
| 44 | + { |
| 45 | + IPInterfaceProperties properties = networkInterface.GetIPProperties(); |
| 46 | + |
| 47 | + foreach (var ip in properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork).Select(i => i.Address)) |
| 48 | + { |
| 49 | + AnsiConsole.MarkupLine($" [cyan]{ip}[/]: [green]{networkInterface.Name}[/]"); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + AnsiConsole.WriteLine(); |
| 54 | + } |
| 55 | + catch (Exception ex) |
| 56 | + { |
| 57 | + AnsiConsole.WriteLine($"[red]{ex.Message}[/]"); |
| 58 | + } |
| 59 | + |
| 60 | + AnsiConsoleHelper.Rule("white"); |
| 61 | + } |
| 62 | +} |
0 commit comments