Skip to content

Commit ed8c712

Browse files
CopilotwaldekmastykarzCopilot
authored
Print proxy URL when starting Dev Proxy in detach mode (#1591)
* Initial plan * Print proxy URL with assigned port when starting Dev Proxy in detach mode Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> * Update DevProxy/Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix API URL showing port 0 when using --api-port 0 in detach mode After Kestrel binds, read the actual API address from IServerAddressesFeature and update the state file. The parent process now also waits for a resolved API URL before printing the detach output. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> Co-authored-by: Waldek Mastykarz <waldek@mastykarz.nl> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8d4a62d commit ed8c712

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DevProxy.Abstractions.Plugins;
22
using DevProxy.Abstractions.Proxy;
33
using DevProxy.Abstractions.Utils;
4+
using DevProxy.State;
45
using Microsoft.AspNetCore.Hosting.Server;
56
using Microsoft.AspNetCore.Hosting.Server.Features;
67
using System.CommandLine;
@@ -314,6 +315,13 @@ private async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken c
314315
var serverAddresses = _app.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>();
315316
var address = serverAddresses?.Addresses.FirstOrDefault() ?? $"http://{_proxyConfiguration.IPAddress}:{_proxyConfiguration.ApiPort}";
316317
_logger.LogInformation("Dev Proxy API listening on {Address}...", address);
318+
319+
// Update state file with the actual Kestrel API address
320+
// (resolves port 0 to OS-assigned port)
321+
if (IsInternalDaemon)
322+
{
323+
_ = UpdateStateWithApiUrlAsync(address);
324+
}
317325
});
318326
await _app.RunAsync(cancellationToken);
319327

@@ -716,4 +724,14 @@ private async Task CheckForNewVersionAsync()
716724
);
717725
}
718726
}
727+
728+
private static async Task UpdateStateWithApiUrlAsync(string apiUrl)
729+
{
730+
var state = await StateManager.LoadStateByPidAsync(Environment.ProcessId);
731+
if (state is not null)
732+
{
733+
state.ApiUrl = apiUrl;
734+
await StateManager.SaveStateAsync(state);
735+
}
736+
}
719737
}

DevProxy/Program.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,25 @@ await Console.Error.WriteLineAsync(
163163
await Task.Delay(200);
164164

165165
var state = await StateManager.LoadStateByPidAsync(process.Id);
166-
if (state != null)
166+
if (state is { Port: > 0 } && !string.IsNullOrEmpty(state.ApiUrl) && !state.ApiUrl.EndsWith(":0", StringComparison.Ordinal))
167167
{
168+
Uri? apiUri = null;
169+
if (!string.IsNullOrWhiteSpace(state.ApiUrl))
170+
{
171+
Uri.TryCreate(state.ApiUrl, UriKind.Absolute, out apiUri);
172+
}
173+
174+
// Build proxy URL in a way that correctly handles IPv6 hosts.
175+
string hostForProxy = apiUri?.Host ?? IPAddress.Loopback.ToString();
176+
var proxyUriBuilder = new UriBuilder(Uri.UriSchemeHttp, hostForProxy, state.Port);
177+
var proxyUrl = proxyUriBuilder.Uri.ToString().TrimEnd('/');
178+
168179
if (isJsonOutput)
169180
{
170181
await Console.Out.WriteLineAsync(FormatJsonResultEntry(new
171182
{
172183
state.Pid,
184+
ProxyUrl = proxyUrl,
173185
state.ApiUrl,
174186
state.LogFile
175187
}));
@@ -179,6 +191,7 @@ await Console.Out.WriteLineAsync(FormatJsonResultEntry(new
179191
await Console.Out.WriteLineAsync("Dev Proxy started in background.");
180192
await Console.Out.WriteLineAsync();
181193
await Console.Out.WriteLineAsync($" PID: {state.Pid}");
194+
await Console.Out.WriteLineAsync($" Proxy URL: {proxyUrl}");
182195
await Console.Out.WriteLineAsync($" API URL: {state.ApiUrl}");
183196
await Console.Out.WriteLineAsync($" Log file: {state.LogFile}");
184197
await Console.Out.WriteLineAsync();

0 commit comments

Comments
 (0)