🐛 trying to fix IP issue

This commit is contained in:
2025-09-06 16:10:15 +08:00
parent a21d19c3ef
commit 54907eede1
4 changed files with 18 additions and 12 deletions

View File

@@ -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")]

View File

@@ -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<Dictionary<string, string>>
{
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();
}
}
}

View File

@@ -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);

View File

@@ -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<IpCheckResponse> 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}")),
});
}
}