Skip to content

Commit 1281a4a

Browse files
authored
WasmAppHost: block port# 5060 and 5061, for the webserver (#76485)
1 parent 89c7184 commit 1281a4a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/mono/wasm/host/WebServerStartup.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public static int StartDebugProxy(string devToolsHost)
3838
//on managed code will freeze because it will not be able to continue executing the BrowserDebugProxy to get the locals value
3939
var executablePath = Path.Combine(System.AppContext.BaseDirectory, "BrowserDebugHost.dll");
4040
var ownerPid = Environment.ProcessId;
41-
var generateRandomPort = new Random().Next(5000, 5300);
41+
// generate a random port in a given range, skipping the ports blocked by browsers: https://chromestatus.com/feature/5064283639513088
42+
var generateRandomPort = GetNextRandomExcept(5000..5300,
43+
5060, // SIP
44+
5061 // SIPS
45+
);
4246
var processStartInfo = new ProcessStartInfo
4347
{
4448
FileName = "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""),
@@ -52,6 +56,17 @@ public static int StartDebugProxy(string devToolsHost)
5256
throw new InvalidOperationException("Unable to start debug proxy process.");
5357
}
5458
return generateRandomPort;
59+
60+
static int GetNextRandomExcept(Range range, params int[] except)
61+
{
62+
int current;
63+
do
64+
{
65+
current = Random.Shared.Next(range.Start.Value, range.End.Value);
66+
} while (Array.IndexOf(except, current) > -1);
67+
68+
return current;
69+
}
5570
}
5671

5772
public void Configure(IApplicationBuilder app,

0 commit comments

Comments
 (0)