mirror of https://github.com/apache/cloudstack.git
Validate IPv4 address using Apache Commons Validator
This commit is contained in:
parent
87a7810ade
commit
72ba98b1b8
6
pom.xml
6
pom.xml
|
|
@ -82,6 +82,7 @@
|
|||
<cs.aws.sdk.version>1.3.22</cs.aws.sdk.version>
|
||||
<cs.lang.version>2.6</cs.lang.version>
|
||||
<cs.commons-io.version>1.4</cs.commons-io.version>
|
||||
<cs.commons-validator.version>1.4.0</cs.commons-validator.version>
|
||||
<cs.reflections.version>0.9.8</cs.reflections.version>
|
||||
<cs.java-ipv6.version>0.10</cs.java-ipv6.version>
|
||||
<cs.replace.properties>build/replace.properties</cs.replace.properties>
|
||||
|
|
@ -233,6 +234,11 @@
|
|||
<artifactId>commons-codec</artifactId>
|
||||
<version>${cs.codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
<version>${cs.commons-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk16</artifactId>
|
||||
|
|
|
|||
|
|
@ -344,14 +344,14 @@ public class ApiServlet extends HttpServlet {
|
|||
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
||||
return null;
|
||||
}
|
||||
if(NetUtils.isValidIp(ip)) {
|
||||
if(NetUtils.isValidIp(ip) || NetUtils.isValidIpv6(ip)) {
|
||||
return ip;
|
||||
}
|
||||
//it could be possible to have multiple IPs in HTTP header, this happens if there are multiple proxy in between
|
||||
//the client and the servlet, so parse the client IP
|
||||
String[] ips = ip.split(",");
|
||||
for(String i : ips) {
|
||||
if(NetUtils.isValidIp(i.trim())) {
|
||||
if(NetUtils.isValidIp(i.trim()) || NetUtils.isValidIpv6(i.trim())) {
|
||||
return i.trim();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,6 +58,10 @@
|
|||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib-nodep</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ package com.cloud.utils.net;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
|
|
@ -43,6 +42,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.apache.commons.net.util.SubnetUtils;
|
||||
import org.apache.commons.validator.routines.InetAddressValidator;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.googlecode.ipv6.IPv6Address;
|
||||
|
|
@ -520,35 +520,9 @@ public class NetUtils {
|
|||
}
|
||||
|
||||
public static boolean isValidIp(final String ip) {
|
||||
final String[] ipAsList = ip.split("\\.");
|
||||
InetAddressValidator validator = InetAddressValidator.getInstance();
|
||||
|
||||
// The IP address must have four octets
|
||||
if (Array.getLength(ipAsList) != 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
// Each octet must be an integer
|
||||
final String octetString = ipAsList[i];
|
||||
int octet;
|
||||
try {
|
||||
octet = Integer.parseInt(octetString);
|
||||
} catch (final Exception e) {
|
||||
return false;
|
||||
}
|
||||
// Each octet must be between 0 and 255, inclusive
|
||||
if (octet < 0 || octet > 255) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each octetString must have between 1 and 3 characters
|
||||
if (octetString.length() < 1 || octetString.length() > 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// IP is good, return true
|
||||
return true;
|
||||
return validator.isValidInet4Address(ip);
|
||||
}
|
||||
|
||||
public static boolean isValidCIDR(final String cidr) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue