-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from rprouse/issue/130
Display local and public IP addresses
- Loading branch information
Showing
9 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.CommandLine.NamingConventionBinder; | ||
using Spectre.Console; | ||
using Guppi.Core.Extensions; | ||
using System.Net; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using Guppi.Core.Interfaces.Services; | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Net.NetworkInformation; | ||
|
||
namespace Guppi.Console.Skills; | ||
|
||
internal class IpSkill(IIPService service) : ISkill | ||
{ | ||
private readonly IIPService _service = service; | ||
|
||
public IEnumerable<Command> GetCommands() => [ | ||
new Command("ip", "Displays the IP address of the current machine.") | ||
{ | ||
Handler = CommandHandler.Create(async () => await DisplayIp()) | ||
} | ||
]; | ||
|
||
private async Task DisplayIp() | ||
{ | ||
AnsiConsoleHelper.TitleRule($":tokyo_tower: IP Addresses"); | ||
|
||
try | ||
{ | ||
var wanIp = await _service.GetWanIPAddress(); | ||
var interfaces = _service.GetNetworkInterfaces(); | ||
|
||
AnsiConsole.MarkupLine("[bold]WAN IP:[/]"); | ||
AnsiConsole.MarkupLine($" [cyan]{wanIp}[/]"); | ||
AnsiConsole.WriteLine(); | ||
AnsiConsole.MarkupLine("[bold]Local IPs:[/]"); | ||
|
||
foreach (NetworkInterface networkInterface in interfaces) | ||
{ | ||
if (networkInterface.OperationalStatus == OperationalStatus.Up) | ||
{ | ||
IPInterfaceProperties properties = networkInterface.GetIPProperties(); | ||
|
||
foreach (var ip in properties.UnicastAddresses.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork).Select(i => i.Address)) | ||
{ | ||
AnsiConsole.MarkupLine($" [cyan]{ip}[/]: [green]{networkInterface.Name}[/]"); | ||
} | ||
} | ||
} | ||
AnsiConsole.WriteLine(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
AnsiConsole.WriteLine($"[red]{ex.Message}[/]"); | ||
} | ||
|
||
AnsiConsoleHelper.Rule("white"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using System.Threading.Tasks; | ||
|
||
namespace Guppi.Core.Interfaces.Services; | ||
|
||
public interface IIPService | ||
{ | ||
IList<NetworkInterface> GetNetworkInterfaces(); | ||
|
||
Task<IPAddress> GetWanIPAddress(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using System.Net.Sockets; | ||
using System.Threading.Tasks; | ||
using Guppi.Core.Interfaces.Providers; | ||
using Guppi.Core.Interfaces.Services; | ||
|
||
namespace Guppi.Core.Services; | ||
|
||
public class IPService(IHttpRestProvider http) : IIPService | ||
{ | ||
private const string RequestUri = "http://icanhazip.com"; | ||
|
||
private readonly IHttpRestProvider _http = http; | ||
|
||
public async Task<IPAddress> GetWanIPAddress() | ||
{ | ||
var ipStr = await _http.GetStringAsync(RequestUri); | ||
ipStr = ipStr.Replace("\\r\\n", "").Replace("\\n", "").Trim(); | ||
if (!IPAddress.TryParse(ipStr, out var ipAddress)) return null; | ||
return ipAddress; | ||
} | ||
|
||
public IList<NetworkInterface> GetNetworkInterfaces() => | ||
NetworkInterface.GetAllNetworkInterfaces(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters