Skip to content

Commit

Permalink
Merge pull request #187 from rprouse/issue/130
Browse files Browse the repository at this point in the history
Display local and public IP addresses
  • Loading branch information
rprouse authored Oct 14, 2024
2 parents 946d60b + 2a93896 commit 9b46f5c
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Guppi.Console/Guppi.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/rprouse/guppi</PackageProjectUrl>
<RepositoryUrl>https://github.com/rprouse/guppi</RepositoryUrl>
<PackageId>dotnet-guppi</PackageId>
<Version>6.0.0</Version>
<Version>6.1.0</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>guppi</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
Expand Down
1 change: 1 addition & 0 deletions Guppi.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static IServiceProvider ConfigureServices() =>
.AddTransient<ISkill, DictionarySkill>()
.AddTransient<ISkill, GitSkill>()
.AddTransient<ISkill, HueLightsSkill>()
.AddTransient<ISkill, IpSkill>()
.AddTransient<ISkill, ManifestoSkill>()
.AddTransient<ISkill, NotesSkill>()
.AddTransient<ISkill, OpenAISkill>()
Expand Down
2 changes: 1 addition & 1 deletion Guppi.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Guppi.Console": {
"commandName": "Project",
"commandLineArgs": "hacker"
"commandLineArgs": "ip"
}
}
}
12 changes: 5 additions & 7 deletions Guppi.Console/Skills/AsciiSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ public AsciiSkill(IAsciiService service)
_service = service;
}

public IEnumerable<Command> GetCommands() =>
new[]
public IEnumerable<Command> GetCommands() => [
new Command("ascii", "Views an ASCII chart.")
{
new Command("ascii", "Views an ASCII chart.")
{
Handler = CommandHandler.Create(() => ViewAsciiTable())
}
};
Handler = CommandHandler.Create(() => ViewAsciiTable())
}
];

private void ViewAsciiTable()
{
Expand Down
62 changes: 62 additions & 0 deletions Guppi.Console/Skills/IpSkill.cs
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");
}
}
1 change: 1 addition & 0 deletions Guppi.Core/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static IServiceCollection AddCore(this IServiceCollection services)
.AddTransient<IDictionaryService, DictionaryService>()
.AddTransient<IGitService, GitService>()
.AddTransient<IHueLightService, HueLightService>()
.AddTransient<IIPService, IPService>()
.AddTransient<INoteService, NoteService>()
.AddTransient<IOpenAIService, OpenAIService>()
.AddTransient<ISerialPortService, SerialPortService>()
Expand Down
13 changes: 13 additions & 0 deletions Guppi.Core/Interfaces/Services/IIPService.cs
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();
}
28 changes: 28 additions & 0 deletions Guppi.Core/Services/IPService.cs
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();
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ it finds. If there are more than one bridge, list the bridges and register using

You can have one default light which is set using the configure command.

### IP Address

Displays your local and public IP addresses.

### Notes

I keep all of my notes as Markdown files. I used to use VS Code, but recently switched to
Expand Down

0 comments on commit 9b46f5c

Please sign in to comment.