From 54907eede1c7968a3f4f7d06f62a888645b9c489 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 6 Sep 2025 16:10:15 +0800 Subject: [PATCH] :bug: trying to fix IP issue --- .../Controllers/WellKnownController.cs | 11 ++++++----- DysonNetwork.Gateway/RegistryProxyConfigProvider.cs | 8 ++++---- .../Startup/ServiceCollectionExtensions.cs | 5 ++++- DysonNetwork.Pass/IpCheckController.cs | 6 ++++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/DysonNetwork.Gateway/Controllers/WellKnownController.cs b/DysonNetwork.Gateway/Controllers/WellKnownController.cs index 3f1c16c..9fddcf3 100644 --- a/DysonNetwork.Gateway/Controllers/WellKnownController.cs +++ b/DysonNetwork.Gateway/Controllers/WellKnownController.cs @@ -1,4 +1,5 @@ using System.Text; +using System.Text.Json.Serialization; using dotnet_etcd.interfaces; using Microsoft.AspNetCore.Mvc; using Yarp.ReverseProxy.Configuration; @@ -15,11 +16,11 @@ public class WellKnownController( { public class IpCheckResponse { - public string? RemoteIp { get; set; } - public string? XForwardedFor { get; set; } - public string? XForwardedProto { get; set; } - public string? XForwardedHost { get; set; } - public string? XRealIp { get; set; } + [JsonPropertyName("remote_ip")] public string? RemoteIp { get; set; } + [JsonPropertyName("x_forwarded_for")] public string? XForwardedFor { get; set; } + [JsonPropertyName("x_forwarded_proto")] public string? XForwardedProto { get; set; } + [JsonPropertyName("x_forwarded_host")] public string? XForwardedHost { get; set; } + [JsonPropertyName("x_real_ip")] public string? XRealIp { get; set; } } [HttpGet("ip-check")] diff --git a/DysonNetwork.Gateway/RegistryProxyConfigProvider.cs b/DysonNetwork.Gateway/RegistryProxyConfigProvider.cs index 2efa79c..4c4bc91 100644 --- a/DysonNetwork.Gateway/RegistryProxyConfigProvider.cs +++ b/DysonNetwork.Gateway/RegistryProxyConfigProvider.cs @@ -171,7 +171,7 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable } // Host-based routing - if (domainMappings.TryGetValue(serviceName, out var domain)) + if (domainMappings.TryGetValue(serviceName, out var domain) && domain is not null) { var hostRoute = new RouteConfig { @@ -181,7 +181,7 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable { Hosts = [domain], Path = "/{**catch-all}" - } + }, }; routes.Add(hostRoute); _logger.LogInformation(" Added Host-based Route: {Host}", domain); @@ -196,7 +196,7 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable Transforms = new List> { new() { { "PathRemovePrefix", $"/{pathAlias}" } }, - new() { { "PathPrefix", "/api" } } + new() { { "PathPrefix", "/api" } }, }, Timeout = TimeSpan.FromSeconds(5) }; @@ -236,4 +236,4 @@ public class RegistryProxyConfigProvider : IProxyConfigProvider, IDisposable _watchCts.Cancel(); _watchCts.Dispose(); } -} +} \ No newline at end of file diff --git a/DysonNetwork.Gateway/Startup/ServiceCollectionExtensions.cs b/DysonNetwork.Gateway/Startup/ServiceCollectionExtensions.cs index 53cef77..147983f 100644 --- a/DysonNetwork.Gateway/Startup/ServiceCollectionExtensions.cs +++ b/DysonNetwork.Gateway/Startup/ServiceCollectionExtensions.cs @@ -24,7 +24,10 @@ public static class ServiceCollectionExtensions }) .AddTransforms(context => { - context.AddForwarded(); + context.CopyRequestHeaders = true; + context.AddOriginalHost(); + context.AddForwarded(action: ForwardedTransformActions.Set); + context.AddXForwarded(action: ForwardedTransformActions.Set); }); services.AddRegistryService(configuration, addForwarder: false); diff --git a/DysonNetwork.Pass/IpCheckController.cs b/DysonNetwork.Pass/IpCheckController.cs index abc90f7..dec1a77 100644 --- a/DysonNetwork.Pass/IpCheckController.cs +++ b/DysonNetwork.Pass/IpCheckController.cs @@ -13,9 +13,10 @@ public class IpCheckController : ControllerBase public string? XForwardedProto { get; set; } public string? XForwardedHost { get; set; } public string? XRealIp { get; set; } + public string? Headers { get; set; } } - [HttpGet("ip-check")] + [HttpGet] public ActionResult GetIpCheck() { var ip = HttpContext.Connection.RemoteIpAddress?.ToString(); @@ -31,7 +32,8 @@ public class IpCheckController : ControllerBase XForwardedFor = xForwardedFor, XForwardedProto = xForwardedProto, XForwardedHost = xForwardedHost, - XRealIp = realIp + XRealIp = realIp, + Headers = string.Join('\n', Request.Headers.Select(h => $"{h.Key}: {h.Value}")), }); } } \ No newline at end of file