test .
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user