This commit is contained in:
2025-06-20 17:41:57 +08:00
parent 30f0fe6b58
commit 4d8b7b5f16

View File

@@ -50,10 +50,20 @@ public class HolidaysController : ControllerBase
[AllowAnonymous]
[HttpGet("remote-ip")]
public async Task<string> GetRemoteIp()
public string GetRemoteIp()
{
// 获取远程IP地址
var remoteIp = HttpContext.Connection.RemoteIpAddress?.ToString();
return await Task.FromResult(remoteIp);
// 检查是否在Kubernetes环境中
var isKubernetes = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"));
if (isKubernetes)
{
var forwardedFor = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrEmpty(forwardedFor))
{
return forwardedFor.Split(',').First().Trim();
}
}
return HttpContext.Connection.RemoteIpAddress?.ToString();
}
}