Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions DevProxy.Tests/DevProxyCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ public void ApplyDiscoverOption_False_KeepsRecordingEnabledFromOtherSources()
Assert.True(config.Record);
}

[Theory]
[InlineData(-1, 8897, "port")]
[InlineData(70000, 8897, "port")]
[InlineData(8000, -1, "apiPort")]
[InlineData(8000, 70000, "apiPort")]
public void GetInvalidConfigPortMessage_OutOfRange_NamesSettingAndRange(int port, int apiPort, string setting)
{
var message = DevProxyCommand.GetInvalidConfigPortMessage(port, apiPort);

Assert.NotNull(message);
Assert.Contains($"'{setting}'", message, StringComparison.Ordinal);
Assert.Contains("between 0 and 65535", message, StringComparison.Ordinal);
}

[Theory]
[InlineData(0, 0)]
[InlineData(8000, 8897)]
[InlineData(65535, 65535)]
public void GetInvalidConfigPortMessage_InRange_ReturnsNull(int port, int apiPort)
{
Assert.Null(DevProxyCommand.GetInvalidConfigPortMessage(port, apiPort));
}

[Theory]
[InlineData(true, "stdio", "npx", "@devproxy/mcp")]
[InlineData(true, "stdio", "-c", "myconfig.json", "node", "app.js")]
Expand Down
21 changes: 21 additions & 0 deletions DevProxy/Commands/DevProxyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,20 @@ internal static void ValidatePort(OptionResult input)
}
}

internal static bool IsValidPort(int port) => port is >= 0 and <= System.Net.IPEndPoint.MaxPort;

// CLI port options are validated by ValidatePort, so an out-of-range
// value here comes from the configuration file.
internal static string? GetInvalidConfigPortMessage(int port, int apiPort)
{
var (setting, value) = !IsValidPort(port) ? ("port", port)
: !IsValidPort(apiPort) ? ("apiPort", apiPort)
: (null, 0);
return setting is null
? null
: $"'{value}' is not a valid value for '{setting}' in the configuration file. Specify a number between 0 and {System.Net.IPEndPoint.MaxPort}.";
}

private async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
if (_app is null)
Expand Down Expand Up @@ -651,6 +665,13 @@ private async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken c

ConfigureFromOptions(parseResult);

var invalidPort = GetInvalidConfigPortMessage(_proxyConfiguration.Port, _proxyConfiguration.ApiPort);
if (invalidPort is not null)
{
_logger.LogError("{InvalidPort}", invalidPort);
return 1;
}

// Only one instance can be the system proxy. This also runs on every hot reload,
// so a reload doesn't take the system proxy over from an instance that registered
// while this one was restarting.
Expand Down
7 changes: 6 additions & 1 deletion DevProxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,12 @@ static WebApplication BuildApplicationCore(
}
_ = builder.WebHost.ConfigureKestrel(options =>
{
options.Listen(apiAddress, apiPort);
// An out-of-range port would throw while building the app. Skip it so
// DevProxyCommand can report it as a configuration error instead.
if (DevProxyCommand.IsValidPort(apiPort))
{
options.Listen(apiAddress, apiPort);
}
});

var app = builder.Build();
Expand Down
Loading