Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rssws committed Jan 29, 2023
2 parents 4f35b89 + 702182b commit f4ad807
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
5 changes: 5 additions & 0 deletions piService/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>unirest-java</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
</dependencies>

<build>
Expand Down
25 changes: 7 additions & 18 deletions piService/src/main/java/wang/zhongpin/pi/server/Utils.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package wang.zhongpin.pi.server;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.validator.routines.InetAddressValidator;
import jakarta.servlet.http.HttpServletRequest;

public final class Utils {
private static final String IPV4_PATTERN =
"^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.(?!$)|$)){4}$";
private static final Pattern pattern = Pattern.compile(IPV4_PATTERN);

private static boolean isValidIpV4(final String s) {
if (s == null) {
return false;
}
Matcher matcher = pattern.matcher(s);
return matcher.matches();
private static boolean isValidAddr(final String s) {
InetAddressValidator inetAddressValidator = InetAddressValidator.getInstance();
return inetAddressValidator.isValid(s);
}

public static String getRemoteAddr(HttpServletRequest request) {
String remoteAddr = "";
if (request != null) {
remoteAddr = request.getHeader("True-Client-IP");
if (isValidIpV4(remoteAddr)) return remoteAddr;
remoteAddr = request.getHeader("CF-Connecting-IP");
if (isValidIpV4(remoteAddr)) return remoteAddr;
if (isValidAddr(remoteAddr)) return remoteAddr;
remoteAddr = request.getHeader("X-FORWARDED-FOR").split(",")[0];
if (isValidIpV4(remoteAddr)) return remoteAddr;
if (isValidAddr(remoteAddr)) return remoteAddr;
remoteAddr = request.getRemoteAddr();
if (isValidIpV4(remoteAddr)) return remoteAddr;
if (isValidAddr(remoteAddr)) return remoteAddr;
remoteAddr = "";
}
return remoteAddr;
Expand Down

0 comments on commit f4ad807

Please sign in to comment.