Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/mono/wasm/host/WebServerStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public static int StartDebugProxy(string devToolsHost)
//on managed code will freeze because it will not be able to continue executing the BrowserDebugProxy to get the locals value
var executablePath = Path.Combine(System.AppContext.BaseDirectory, "BrowserDebugHost.dll");
var ownerPid = Environment.ProcessId;
var generateRandomPort = new Random().Next(5000, 5300);
// generate a random port in a given range, skipping the ports blocked by browsers: https://chromestatus.com/feature/5064283639513088
var generateRandomPort = GetNextRandomExcept(5000..5300,
5060, // SIP
5061 // SIPS
);
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""),
Expand All @@ -52,6 +56,17 @@ public static int StartDebugProxy(string devToolsHost)
throw new InvalidOperationException("Unable to start debug proxy process.");
}
return generateRandomPort;

static int GetNextRandomExcept(Range range, params int[] except)
{
int current;
do
{
current = Random.Shared.Next(range.Start.Value, range.End.Value);
} while (Array.IndexOf(except, current) > -1);

return current;
}
}

public void Configure(IApplicationBuilder app,
Expand Down