content: package ai.llmchat.common.core.util;
  
  import cn.hutool.core.net.NetUtil;
  import jakarta.servlet.http.HttpServletRequest;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.web.context.request.RequestAttributes;
  import org.springframework.web.context.request.RequestContextHolder;
  import org.springframework.web.context.request.ServletRequestAttributes;
  
  import java.util.Objects;
  
  public class IPUtils {
  
  	private static final String[] CLIENT_IP_HEADERS = { "X-Forwarded-For", "X-Real-IP", "Proxy-Client-IP",
  			"WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" };
  
  	public static String getClientIp() {
  		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  		if (Objects.nonNull(requestAttributes)) {
  			HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
  			String ip;
  			for (String header : CLIENT_IP_HEADERS) {
  				ip = request.getHeader(header);
  				if (!NetUtil.isUnknown(ip)) {
  					return NetUtil.getMultistageReverseProxyIp(ip);
  				}
  			}
  			ip = request.getRemoteAddr();
  			return NetUtil.getMultistageReverseProxyIp(ip);
  		}
  		return StringUtils.EMPTY;
  	}
  
  }
  