diff --git a/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRange.java b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRange.java index d85acb8774..2cfa7a7183 100644 --- a/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRange.java +++ b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRange.java @@ -15,8 +15,9 @@ */ package com.github.tomakehurst.wiremock.common; -import com.google.common.net.InetAddresses; -import java.math.BigInteger; +import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.ipToLong; +import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.isValidInet4Address; + import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Objects; @@ -84,8 +85,8 @@ public String toString() { private static class IpRange extends NetworkAddressRange { - private final BigInteger start; - private final BigInteger end; + private final Long start; + private final Long end; private final String asString; private IpRange(String ipRange) { @@ -93,16 +94,16 @@ private IpRange(String ipRange) { if (parts.length != 2) { throw new InvalidInputException(Errors.single(18, ipRange + " is not a valid IP range")); } - this.start = InetAddresses.toBigInteger(parseIpAddress(parts[0])); - this.end = InetAddresses.toBigInteger(parseIpAddress(parts[1])); + this.start = ipToLong(parseIpAddress(parts[0])); + this.end = ipToLong(parseIpAddress(parts[1])); this.asString = ipRange; } @Override public boolean isIncluded(String testValue) { InetAddress testValueAddress = lookup(testValue); - BigInteger intVal = InetAddresses.toBigInteger(testValueAddress); - return intVal.compareTo(start) >= 0 && intVal.compareTo(end) <= 0; + long longValue = ipToLong(testValueAddress); + return (longValue >= start && longValue <= end); } @Override @@ -166,7 +167,7 @@ public boolean isIncluded(String testValue) { } private static InetAddress parseIpAddress(String ipAddress) { - if (!InetAddresses.isInetAddress(ipAddress)) { + if (!isValidInet4Address(ipAddress)) { throw new InvalidInputException(Errors.single(16, ipAddress + " is not a valid IP address")); } diff --git a/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRules.java b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRules.java index 09ba48d4c8..653d730dd3 100644 --- a/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRules.java +++ b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressRules.java @@ -16,11 +16,11 @@ package com.github.tomakehurst.wiremock.common; import static com.github.tomakehurst.wiremock.common.NetworkAddressRange.ALL; +import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.isValidInet4Address; import static java.util.Collections.emptySet; import static java.util.stream.Collectors.toSet; -import com.google.common.collect.ImmutableSet; -import com.google.common.net.InetAddresses; +import java.util.HashSet; import java.util.Set; public class NetworkAddressRules { @@ -34,7 +34,8 @@ public static Builder builder() { private final Set denied; private final Set deniedHostPatterns; - public static NetworkAddressRules ALLOW_ALL = new NetworkAddressRules(Set.of(ALL), emptySet()); + public static final NetworkAddressRules ALLOW_ALL = + new NetworkAddressRules(Set.of(ALL), emptySet()); public NetworkAddressRules(Set allowed, Set denied) { this.allowed = @@ -78,7 +79,7 @@ private static Set defaultIfEmpty(Set original, Set ifEmpty) { } public boolean isAllowed(String testValue) { - if (InetAddresses.isInetAddress(testValue)) { + if (isValidInet4Address(testValue)) { return allowed.stream().anyMatch(rule -> rule.isIncluded(testValue)) && denied.stream().noneMatch(rule -> rule.isIncluded(testValue)); } else { @@ -88,8 +89,8 @@ public boolean isAllowed(String testValue) { } public static class Builder { - private final ImmutableSet.Builder allowed = ImmutableSet.builder(); - private final ImmutableSet.Builder denied = ImmutableSet.builder(); + private final Set allowed = new HashSet<>(); + private final Set denied = new HashSet<>(); public Builder allow(String expression) { allowed.add(NetworkAddressRange.of(expression)); @@ -102,11 +103,11 @@ public Builder deny(String expression) { } public NetworkAddressRules build() { - Set allowedRanges = allowed.build(); + Set allowedRanges = allowed; if (allowedRanges.isEmpty()) { allowedRanges = Set.of(ALL); } - return new NetworkAddressRules(allowedRanges, denied.build()); + return new NetworkAddressRules(Set.copyOf(allowedRanges), Set.copyOf(denied)); } } } diff --git a/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressUtils.java b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressUtils.java new file mode 100644 index 0000000000..c4c9f31b74 --- /dev/null +++ b/src/main/java/com/github/tomakehurst/wiremock/common/NetworkAddressUtils.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 Thomas Akehurst + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.tomakehurst.wiremock.common; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class NetworkAddressUtils { + + private NetworkAddressUtils() {} + + public static boolean isValidInet4Address(String ip) { + try { + return InetAddress.getByName(ip).getHostAddress().equals(ip); + } catch (UnknownHostException ex) { + return false; + } + } + + public static long ipToLong(InetAddress ipAddress) { + long resultIP = 0; + byte[] ipAddressOctets = ipAddress.getAddress(); + + for (byte octet : ipAddressOctets) { + resultIP <<= 8; + resultIP |= octet & 0xFF; + } + return resultIP; + } +}