test .
This commit is contained in:
@@ -50,10 +50,20 @@ public class HolidaysController : ControllerBase
|
|||||||
|
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[HttpGet("remote-ip")]
|
[HttpGet("remote-ip")]
|
||||||
public async Task<string> GetRemoteIp()
|
public string GetRemoteIp()
|
||||||
{
|
{
|
||||||
// 获取远程IP地址
|
// 检查是否在Kubernetes环境中
|
||||||
var remoteIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
var isKubernetes = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"));
|
||||||
return await Task.FromResult(remoteIp);
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user