This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new b26dda69af fixed RateLimiter remoteAddress issues Please reply #5340. 
(#5504)
b26dda69af is described below

commit b26dda69afe952a37c6c28df933b6cff045b8401
Author: wlngo <[email protected]>
AuthorDate: Fri Jan 24 11:46:02 2025 +0800

    fixed RateLimiter remoteAddress issues Please reply #5340. (#5504)
    
    * fixed #5340.
    
    * fix RemoteAddrKeyResolver.
    
    ---------
    
    Co-authored-by: zhengpeng <[email protected]>
    Co-authored-by: xiaoyu <[email protected]>
    Co-authored-by: aias00 <[email protected]>
    Co-authored-by: DamonXue <[email protected]>
    Co-authored-by: aias00 <[email protected]>
---
 .../resolver/RemoteAddrKeyResolver.java            | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/resolver/RemoteAddrKeyResolver.java
 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/resolver/RemoteAddrKeyResolver.java
index c624979d2d..a3beba1bb1 100644
--- 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/resolver/RemoteAddrKeyResolver.java
+++ 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/resolver/RemoteAddrKeyResolver.java
@@ -17,6 +17,7 @@
 
 package org.apache.shenyu.plugin.ratelimiter.resolver;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.spi.Join;
 import org.springframework.web.server.ServerWebExchange;
 
@@ -25,6 +26,10 @@ import java.util.Objects;
 @Join
 public class RemoteAddrKeyResolver implements RateLimiterKeyResolver {
 
+    private static final String[] HEADERS = {"X-Forwarded-For", "X-Real-IP", 
"Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", 
"HTTP_X_FORWARDED_FOR"};
+
+    private static final String UNKNOWN = "unknown";
+
     @Override
     public String getKeyResolverName() {
         return "REMOTE_ADDRESS_KEY_RESOLVER";
@@ -32,6 +37,28 @@ public class RemoteAddrKeyResolver implements 
RateLimiterKeyResolver {
 
     @Override
     public String resolve(final ServerWebExchange exchange) {
+        String ip;
+        for (String header : HEADERS) {
+            ip = exchange.getRequest().getHeaders().getFirst(header);
+            boolean isUnknown = StringUtils.isBlank(ip) || 
UNKNOWN.equalsIgnoreCase(ip);
+            if (!isUnknown) {
+                if (StringUtils.indexOf(ip, ',') > 0) {
+                    String[] split = StringUtils.split(ip, ',');
+                    for (int i = 0; i < split.length; i++) {
+                        split[i] = split[i].trim();
+                    }
+                    for (String subIp : split) {
+                        boolean isUnknownSubIp = StringUtils.isBlank(subIp) || 
UNKNOWN.equalsIgnoreCase(subIp);
+                        if (!isUnknownSubIp) {
+                            ip = subIp;
+                            break;
+                        }
+                    }
+                }
+                return ip;
+            }
+        }
         return 
Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress();
     }
+
 }

Reply via email to