diff --git a/DevProxy.Tests/DevProxyCommandTests.cs b/DevProxy.Tests/DevProxyCommandTests.cs index 0b604e6f..68957dc4 100644 --- a/DevProxy.Tests/DevProxyCommandTests.cs +++ b/DevProxy.Tests/DevProxyCommandTests.cs @@ -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")] diff --git a/DevProxy/Commands/DevProxyCommand.cs b/DevProxy/Commands/DevProxyCommand.cs index 64845fda..0baeddcd 100644 --- a/DevProxy/Commands/DevProxyCommand.cs +++ b/DevProxy/Commands/DevProxyCommand.cs @@ -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 InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken) { if (_app is null) @@ -651,6 +665,13 @@ private async Task 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. diff --git a/DevProxy/Program.cs b/DevProxy/Program.cs index cb652794..c77b1549 100644 --- a/DevProxy/Program.cs +++ b/DevProxy/Program.cs @@ -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();