Skip to content

Commit 9b46f5c

Browse files
authored
Merge pull request #187 from rprouse/issue/130
Display local and public IP addresses
2 parents 946d60b + 2a93896 commit 9b46f5c

File tree

9 files changed

+116
-9
lines changed

9 files changed

+116
-9
lines changed

Guppi.Console/Guppi.Console.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageProjectUrl>https://github.com/rprouse/guppi</PackageProjectUrl>
1414
<RepositoryUrl>https://github.com/rprouse/guppi</RepositoryUrl>
1515
<PackageId>dotnet-guppi</PackageId>
16-
<Version>6.0.0</Version>
16+
<Version>6.1.0</Version>
1717
<PackAsTool>true</PackAsTool>
1818
<ToolCommandName>guppi</ToolCommandName>
1919
<PackageOutputPath>./nupkg</PackageOutputPath>

Guppi.Console/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static IServiceProvider ConfigureServices() =>
1717
.AddTransient<ISkill, DictionarySkill>()
1818
.AddTransient<ISkill, GitSkill>()
1919
.AddTransient<ISkill, HueLightsSkill>()
20+
.AddTransient<ISkill, IpSkill>()
2021
.AddTransient<ISkill, ManifestoSkill>()
2122
.AddTransient<ISkill, NotesSkill>()
2223
.AddTransient<ISkill, OpenAISkill>()

Guppi.Console/Properties/launchSettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Guppi.Console": {
44
"commandName": "Project",
5-
"commandLineArgs": "hacker"
5+
"commandLineArgs": "ip"
66
}
77
}
88
}

Guppi.Console/Skills/AsciiSkill.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ public AsciiSkill(IAsciiService service)
1717
_service = service;
1818
}
1919

20-
public IEnumerable<Command> GetCommands() =>
21-
new[]
20+
public IEnumerable<Command> GetCommands() => [
21+
new Command("ascii", "Views an ASCII chart.")
2222
{
23-
new Command("ascii", "Views an ASCII chart.")
24-
{
25-
Handler = CommandHandler.Create(() => ViewAsciiTable())
26-
}
27-
};
23+
Handler = CommandHandler.Create(() => ViewAsciiTable())
24+
}
25+
];
2826

2927
private void ViewAsciiTable()
3028
{

Guppi.Console/Skills/IpSkill.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

Guppi.Core/DependencyInjection.cs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static IServiceCollection AddCore(this IServiceCollection services)
4949
.AddTransient<IDictionaryService, DictionaryService>()
5050
.AddTransient<IGitService, GitService>()
5151
.AddTransient<IHueLightService, HueLightService>()
52+
.AddTransient<IIPService, IPService>()
5253
.AddTransient<INoteService, NoteService>()
5354
.AddTransient<IOpenAIService, OpenAIService>()
5455
.AddTransient<ISerialPortService, SerialPortService>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using System.Net;
3+
using System.Net.NetworkInformation;
4+
using System.Threading.Tasks;
5+
6+
namespace Guppi.Core.Interfaces.Services;
7+
8+
public interface IIPService
9+
{
10+
IList<NetworkInterface> GetNetworkInterfaces();
11+
12+
Task<IPAddress> GetWanIPAddress();
13+
}

Guppi.Core/Services/IPService.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Net.NetworkInformation;
5+
using System.Net.Sockets;
6+
using System.Threading.Tasks;
7+
using Guppi.Core.Interfaces.Providers;
8+
using Guppi.Core.Interfaces.Services;
9+
10+
namespace Guppi.Core.Services;
11+
12+
public class IPService(IHttpRestProvider http) : IIPService
13+
{
14+
private const string RequestUri = "http://icanhazip.com";
15+
16+
private readonly IHttpRestProvider _http = http;
17+
18+
public async Task<IPAddress> GetWanIPAddress()
19+
{
20+
var ipStr = await _http.GetStringAsync(RequestUri);
21+
ipStr = ipStr.Replace("\\r\\n", "").Replace("\\n", "").Trim();
22+
if (!IPAddress.TryParse(ipStr, out var ipAddress)) return null;
23+
return ipAddress;
24+
}
25+
26+
public IList<NetworkInterface> GetNetworkInterfaces() =>
27+
NetworkInterface.GetAllNetworkInterfaces();
28+
}

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ it finds. If there are more than one bridge, list the bridges and register using
7171

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

74+
### IP Address
75+
76+
Displays your local and public IP addresses.
77+
7478
### Notes
7579

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

0 commit comments

Comments
 (0)