Skip to content

Commit

Permalink
Merge branch 'master' into master-4988
Browse files Browse the repository at this point in the history
  • Loading branch information
Aias00 authored Jan 24, 2025
2 parents 6f03148 + b26dda6 commit 9f7015e
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,13 +26,39 @@
@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";
}

@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();
}

}

0 comments on commit 9f7015e

Please sign in to comment.