WasmAppHost: block port# 5060 and 5061, for the webserver (#76485)
authorkasperk81 <83082615+kasperk81@users.noreply.github.com>
Tue, 4 Oct 2022 08:15:20 +0000 (11:15 +0300)
committerGitHub <noreply@github.com>
Tue, 4 Oct 2022 08:15:20 +0000 (10:15 +0200)
src/mono/wasm/host/WebServerStartup.cs

index 64bf9e7..4025bf9 100644 (file)
@@ -38,7 +38,11 @@ internal sealed class WebServerStartup
         //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" : ""),
@@ -52,6 +56,17 @@ internal sealed class WebServerStartup
             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,