Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #5340. #5504

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
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();
}

}
Loading