From 13402c8551530367959eb8137c90c9a35f5bfcdc Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 28 Feb 2026 10:38:18 -0500 Subject: [PATCH] Split auth gRPC onto internal listener --- Server/src/SOR.Host/Program.cs | 57 +++++++++++++++++++++++++++++- deploy/k8s/deployment-gateway.yaml | 3 +- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Server/src/SOR.Host/Program.cs b/Server/src/SOR.Host/Program.cs index 8d9c8fa..808b651 100644 --- a/Server/src/SOR.Host/Program.cs +++ b/Server/src/SOR.Host/Program.cs @@ -29,7 +29,10 @@ using Serilog; using StackExchange.Redis; using NATS.Client.Core; using NATS.Client.Hosting; +using System.Net; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.WebSockets; +using Microsoft.AspNetCore.Server.Kestrel.Core; using SOR.Auth.Handlers; // ── ENABLED_SERVICES ────────────────────────────────────────────────────────── @@ -55,6 +58,37 @@ bool IsEnabled(string name) => var builder = WebApplication.CreateBuilder(args); +// Auth gRPC calls are internal-only and should not share the public WebSocket listener. +// When auth is co-located with gateway and Services:Auth points at a loopback HTTP URL, +// preserve the existing HTTP listeners explicitly and add a dedicated HTTP/2 loopback +// listener for gRPC on the configured auth port. +if (IsEnabled("auth") && + Uri.TryCreate(builder.Configuration["Services:Auth"], UriKind.Absolute, out var authServiceUri) && + string.Equals(authServiceUri.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) && + authServiceUri.IsLoopback) +{ + var configuredUrls = builder.WebHost.GetSetting(WebHostDefaults.ServerUrlsKey) + ?? builder.Configuration["ASPNETCORE_URLS"]; + + builder.WebHost.ConfigureKestrel(options => + { + foreach (var rawUrl in (configuredUrls ?? string.Empty).Split( + ';', + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + if (!Uri.TryCreate(rawUrl, UriKind.Absolute, out var configuredUri)) + continue; + + ListenOnConfiguredUrl(options, configuredUri); + } + + options.ListenLocalhost(authServiceUri.Port, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http2; + }); + }); +} + // ── Logging ─────────────────────────────────────────────────────────────────── // Replace .NET's default logger with Serilog. // Configuration comes from the "Serilog" section of appsettings.json. @@ -185,4 +219,25 @@ Log.Information("SOR Host starting. Services: {Services}", rawServices); // RunAsync blocks until the process receives a shutdown signal (SIGTERM, Ctrl+C). // On shutdown, ASP.NET Core calls StopAsync on all IHostedService implementations // (including GatewayHostedService) in reverse registration order. -await app.RunAsync(); \ No newline at end of file +await app.RunAsync(); + +static void ListenOnConfiguredUrl(KestrelServerOptions options, Uri uri) +{ + if (!string.Equals(uri.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase)) + return; + + if (uri.Host == "+" || uri.Host == "*" || uri.Host == "0.0.0.0") + { + options.ListenAnyIP(uri.Port, listenOptions => listenOptions.Protocols = HttpProtocols.Http1); + return; + } + + if (string.Equals(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase)) + { + options.ListenLocalhost(uri.Port, listenOptions => listenOptions.Protocols = HttpProtocols.Http1); + return; + } + + if (IPAddress.TryParse(uri.Host, out var ipAddress)) + options.Listen(ipAddress, uri.Port, listenOptions => listenOptions.Protocols = HttpProtocols.Http1); +} diff --git a/deploy/k8s/deployment-gateway.yaml b/deploy/k8s/deployment-gateway.yaml index 74b264e..e552236 100644 --- a/deploy/k8s/deployment-gateway.yaml +++ b/deploy/k8s/deployment-gateway.yaml @@ -23,6 +23,7 @@ spec: image: registry.aeternalisgames.com/sor/sor-server:latest ports: - containerPort: 8080 + - containerPort: 5001 # ── Shared non-secret config ─────────────────────────────────────── envFrom: @@ -38,7 +39,7 @@ spec: # Auth gRPC is in the same process — call ourselves. - name: Services__Auth - value: "http://localhost:8080" + value: "http://localhost:5001" # Postgres — Identity schema only (auth needs it for account lookups). - name: ConnectionStrings__Default