-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add maui port check command #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rmarinho
wants to merge
9
commits into
main
Choose a base branch
from
cli/port-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+699
−0
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad08afc
feat: add maui port check command
rmarinho 115dce0
feat: wire port check command into CLI entry point
rmarinho cbbb336
fix(port-check): resolve CA1416 and parser test data
rmarinho 8f8fd63
fix(port-check): address review findings (TCP enum, stderr deadlock, …
rmarinho 9691dcb
fix(port-check): guard exhausted retry loop, fix netstat IPv6 wildcar…
rmarinho 97e022b
style(port-check): reformat to use tabs matching repo convention
rmarinho e1f7c85
Merge branch 'main' into cli/port-check
rmarinho 06dfb92
Merge branch 'main' into cli/port-check
rmarinho b29d17f
Merge branch 'main' into cli/port-check
rmarinho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,171 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.Maui.Cli.Models; | ||
| using Microsoft.Maui.Cli.Output; | ||
| using Microsoft.Maui.Cli.Providers.Port; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.Cli.UnitTests; | ||
|
|
||
| public class PortCheckTests | ||
| { | ||
| [Fact] | ||
| public void PortCheckResult_JsonShape_HasExpectedProperties() | ||
| { | ||
| var result = new PortCheckResult | ||
| { | ||
| Port = 8080, | ||
| InUse = true, | ||
| Listeners = | ||
| [ | ||
| new PortListenerResult | ||
| { | ||
| Pid = 1234, | ||
| ProcessName = "dotnet", | ||
| Address = "0.0.0.0", | ||
| Family = "ipv4", | ||
| State = "listen", | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| var json = JsonSerializer.Serialize(result, MauiCliJsonContext.Default.PortCheckResult); | ||
| using var doc = JsonDocument.Parse(json); | ||
| var root = doc.RootElement; | ||
|
|
||
| Assert.Equal(8080, root.GetProperty("port").GetInt32()); | ||
| Assert.True(root.GetProperty("in_use").GetBoolean()); | ||
|
|
||
| var listener = root.GetProperty("listeners")[0]; | ||
| Assert.Equal(1234, listener.GetProperty("pid").GetInt32()); | ||
| Assert.Equal("dotnet", listener.GetProperty("process_name").GetString()); | ||
| Assert.Equal("0.0.0.0", listener.GetProperty("address").GetString()); | ||
| Assert.Equal("ipv4", listener.GetProperty("family").GetString()); | ||
| Assert.Equal("listen", listener.GetProperty("state").GetString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void PortCheckResult_Free_SerializesCorrectly() | ||
| { | ||
| var result = new PortCheckResult { Port = 80, InUse = false }; | ||
| var json = JsonSerializer.Serialize(result, MauiCliJsonContext.Default.PortCheckResult); | ||
| using var doc = JsonDocument.Parse(json); | ||
|
|
||
| Assert.False(doc.RootElement.GetProperty("in_use").GetBoolean()); | ||
| Assert.Equal(0, doc.RootElement.GetProperty("listeners").GetArrayLength()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseLsofOutput_SingleListener_ReturnsCorrectInfo() | ||
| { | ||
| var output = "COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME\n" + | ||
| "dotnet 1234 user 10u IPv4 12345 0t0 TCP *:8080 (LISTEN)\n"; | ||
| var result = UnixPortInspector.ParseLsofOutput(output, 8080); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal(1234, result[0].Pid); | ||
| Assert.Equal("dotnet", result[0].ProcessName); | ||
| Assert.Equal("0.0.0.0", result[0].Address); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseLsofOutput_IPv6_ReturnsIpv6Family() | ||
| { | ||
| var output = "COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME\n" + | ||
| "nginx 999 root 5u IPv6 54321 0t0 TCP *:443 (LISTEN)\n"; | ||
| var result = UnixPortInspector.ParseLsofOutput(output, 443); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal("ipv6", result[0].Family); | ||
| Assert.Equal("::", result[0].Address); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSsOutput_IPv6Wildcard_ReturnsIpv6Address() | ||
| { | ||
| var output = "LISTEN 0 128 [::]:8080 [::]:* users:((\"dotnet\",pid=1234,fd=5))\n"; | ||
| var result = UnixPortInspector.ParseSsOutput(output, 8080); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal("ipv6", result[0].Family); | ||
| Assert.Equal("::", result[0].Address); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseLsofOutput_WrongPort_ReturnsEmpty() | ||
| { | ||
| var output = "COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME\n" + | ||
| "dotnet 1234 user 10u IPv4 12345 0t0 TCP *:9090 (LISTEN)\n"; | ||
| var result = UnixPortInspector.ParseLsofOutput(output, 8080); | ||
|
|
||
| Assert.Empty(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSsOutput_WithProcess_ExtractsPidAndName() | ||
| { | ||
| var output = "LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* users:((\"dotnet\",pid=1234,fd=5))\n"; | ||
| var result = UnixPortInspector.ParseSsOutput(output, 8080); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal(1234, result[0].Pid); | ||
| Assert.Equal("dotnet", result[0].ProcessName); | ||
| Assert.Equal("0.0.0.0", result[0].Address); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSsOutput_WrongPort_ReturnsEmpty() | ||
| { | ||
| var output = "LISTEN 0 128 0.0.0.0:9090 0.0.0.0:* users:((\"dotnet\",pid=1234,fd=5))\n"; | ||
| var result = UnixPortInspector.ParseSsOutput(output, 8080); | ||
|
|
||
| Assert.Empty(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseNetstatOutput_Tcp4_ExtractsListener() | ||
| { | ||
| var output = "Proto Recv-Q Send-Q Local Address Foreign Address (state)\n" + | ||
| "tcp4 0 0 127.0.0.1.8080 *.* LISTEN\n"; | ||
| var result = UnixPortInspector.ParseNetstatOutput(output, 8080); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal("127.0.0.1", result[0].Address); | ||
| Assert.Equal("ipv4", result[0].Family); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseNetstatOutput_Wildcard_MapsToAllInterfaces() | ||
| { | ||
| var output = "Proto Recv-Q Send-Q Local Address Foreign Address (state)\n" + | ||
| "tcp46 0 0 *.19223 *.* LISTEN\n"; | ||
| var result = UnixPortInspector.ParseNetstatOutput(output, 19223); | ||
|
|
||
| Assert.Single(result); | ||
| Assert.Equal("::", result[0].Address); | ||
| Assert.Equal("ipv6", result[0].Family); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseNetstatOutput_WrongPort_ReturnsEmpty() | ||
| { | ||
| var output = "Proto Recv-Q Send-Q Local Address Foreign Address (state)\n" + | ||
| "tcp4 0 0 127.0.0.1.9090 *.* LISTEN\n"; | ||
| var result = UnixPortInspector.ParseNetstatOutput(output, 8080); | ||
|
|
||
| Assert.Empty(result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(0x5000u, 80)] | ||
| [InlineData(0x901Fu, 8080)] | ||
| [InlineData(0xBB01u, 443)] | ||
| public void GetPortFromNetworkDword_ReturnsCorrectPort(uint networkDword, int expectedPort) | ||
| { | ||
| // The formula: ((dword & 0xFF) << 8) | ((dword >> 8) & 0xFF) | ||
| var actual = PortInspectorHelpers.GetPortFromNetworkDword(networkDword); | ||
| Assert.Equal(expectedPort, actual); | ||
| } | ||
| } |
This file contains hidden or 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,110 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.CommandLine; | ||
| using System.CommandLine.Parsing; | ||
| using Microsoft.Maui.Cli.Errors; | ||
| using Microsoft.Maui.Cli.Models; | ||
| using Microsoft.Maui.Cli.Providers.Port; | ||
|
|
||
| namespace Microsoft.Maui.Cli.Commands; | ||
|
|
||
| internal static class PortCommands | ||
| { | ||
| public static Command Create() | ||
| { | ||
| var portCommand = new Command("port", "Diagnose TCP port usage."); | ||
| portCommand.Add(CreateCheckCommand()); | ||
| return portCommand; | ||
| } | ||
|
|
||
| private static Command CreateCheckCommand() | ||
| { | ||
| var portArg = new Argument<int>("port") { Description = "TCP port number to check (1-65535)." }; | ||
|
|
||
| var checkCommand = new Command("check", "Check which process holds a TCP port.") | ||
| { | ||
| portArg, | ||
| }; | ||
|
|
||
| checkCommand.SetAction((ParseResult parseResult) => | ||
| { | ||
| var port = parseResult.GetValue(portArg); | ||
| var formatter = Program.GetFormatter(parseResult); | ||
|
|
||
| if (port is < 1 or > 65535) | ||
| { | ||
| formatter.WriteError(new MauiToolException(ErrorCodes.InvalidArgument, $"Port must be between 1 and 65535, got {port}.")); | ||
| return 2; | ||
| } | ||
|
|
||
| IPortInspector inspector; | ||
| try | ||
| { | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| inspector = new WindowsPortInspector(); | ||
| } | ||
| else | ||
| { | ||
| #pragma warning disable CA1416 | ||
| inspector = new UnixPortInspector(); | ||
| #pragma warning restore CA1416 | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| formatter.WriteError(new MauiToolException(ErrorCodes.PortEnumerationFailed, ex.Message)); | ||
| return 2; | ||
| } | ||
|
|
||
| List<PortListenerInfo> raw; | ||
| try | ||
| { | ||
| raw = [.. inspector.GetListeners(port)]; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| formatter.WriteError(new MauiToolException(ErrorCodes.PortEnumerationFailed, ex.Message)); | ||
| return 2; | ||
| } | ||
|
|
||
| var listeners = raw.Select(l => new PortListenerResult | ||
| { | ||
| Pid = l.Pid, | ||
| ProcessName = l.ProcessName, | ||
| Address = l.Address, | ||
| Family = l.Family, | ||
| State = l.State, | ||
| }).ToList(); | ||
|
|
||
| var result = new PortCheckResult { Port = port, InUse = listeners.Count > 0, Listeners = listeners }; | ||
|
|
||
| var useJson = parseResult.GetValue(GlobalOptions.JsonOption); | ||
| if (useJson) | ||
| { | ||
| formatter.Write(result); | ||
| } | ||
| else | ||
| { | ||
| if (!result.InUse) | ||
| { | ||
| formatter.WriteSuccess($"Port {port} is free."); | ||
| } | ||
| else | ||
| { | ||
| formatter.WriteInfo($"Port {port} is in use:"); | ||
| foreach (var l in result.Listeners) | ||
| { | ||
| var processInfo = l.Pid > 0 ? $"PID {l.Pid} ({(string.IsNullOrEmpty(l.ProcessName) ? "unknown" : l.ProcessName)})" : "unknown process"; | ||
| formatter.WriteInfo($" {processInfo} {l.Address} [{l.Family}]"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result.InUse ? 1 : 0; | ||
| }); | ||
|
|
||
| return checkCommand; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Maui.Cli.Models; | ||
|
|
||
| public sealed record PortCheckResult | ||
| { | ||
| [JsonPropertyName("port")] | ||
| public int Port { get; init; } | ||
|
|
||
| [JsonPropertyName("in_use")] | ||
| public bool InUse { get; init; } | ||
|
|
||
| [JsonPropertyName("listeners")] | ||
| public List<PortListenerResult> Listeners { get; init; } = []; | ||
| } | ||
|
|
||
| public sealed record PortListenerResult | ||
| { | ||
| [JsonPropertyName("pid")] | ||
| public int Pid { get; init; } | ||
|
|
||
| [JsonPropertyName("process_name")] | ||
| public string ProcessName { get; init; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("address")] | ||
| public string Address { get; init; } = string.Empty; | ||
|
|
||
| [JsonPropertyName("family")] | ||
| public string Family { get; init; } = "ipv4"; | ||
|
|
||
| [JsonPropertyName("state")] | ||
| public string State { get; init; } = "listen"; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,9 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Maui.Cli.Providers.Port; | ||
|
|
||
| internal interface IPortInspector | ||
| { | ||
| IReadOnlyList<PortListenerInfo> GetListeners(int port); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/Cli/Microsoft.Maui.Cli/Providers/Port/PortInspectorHelpers.cs
This file contains hidden or 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,10 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Maui.Cli.Providers.Port; | ||
|
|
||
| internal static class PortInspectorHelpers | ||
| { | ||
| internal static int GetPortFromNetworkDword(uint networkDword) | ||
| => (int)(((networkDword & 0xFF) << 8) | ((networkDword >> 8) & 0xFF)); | ||
| } | ||
|
rmarinho marked this conversation as resolved.
|
||
6 changes: 6 additions & 0 deletions
6
src/Cli/Microsoft.Maui.Cli/Providers/Port/PortListenerInfo.cs
This file contains hidden or 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,6 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.Maui.Cli.Providers.Port; | ||
|
|
||
| internal sealed record PortListenerInfo(int Port, int Pid, string ProcessName, string Address, string Family, string State = "listen"); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.