From a0a84222d71f6e16efb70805e9d355280e1aea8c Mon Sep 17 00:00:00 2001 From: cvictory Date: Fri, 25 Jan 2019 10:54:45 +0800 Subject: [PATCH 01/10] fix #2842. remove duplicate SPI definitions for 2.7.x --- dubbo-all/pom.xml | 12 ++++++++++++ dubbo-container/dubbo-container-api/pom.xml | 3 +-- pom.xml | 1 - 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dubbo-all/pom.xml b/dubbo-all/pom.xml index 8c46af627db..15fd731d706 100644 --- a/dubbo-all/pom.xml +++ b/dubbo-all/pom.xml @@ -614,6 +614,18 @@ + + + org.apache.dubbo:dubbo + + + com/** + org/** + + META-INF/dubbo/** + + + diff --git a/dubbo-container/dubbo-container-api/pom.xml b/dubbo-container/dubbo-container-api/pom.xml index 2e1f544064f..2c1e9100af5 100644 --- a/dubbo-container/dubbo-container-api/pom.xml +++ b/dubbo-container/dubbo-container-api/pom.xml @@ -47,9 +47,8 @@ org.apache.dubbo.container.Main - true - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 4cbb67e5b46..8f1a39b7201 100644 --- a/pom.xml +++ b/pom.xml @@ -389,7 +389,6 @@ ${project.version} - true From 19824c3a9d436b5a19bb9cc211935ae5750d2314 Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 21 Feb 2019 10:42:06 +0800 Subject: [PATCH 02/10] fix: rename the thread name from DubboRegistryFailedRetryTimer to DubboMetadataReportRetryTimer in MetadataReportRetry --- .../dubbo/metadata/support/AbstractMetadataReport.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java index 6080fa58f82..95b44f44927 100644 --- a/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java +++ b/dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java @@ -62,7 +62,7 @@ public abstract class AbstractMetadataReport implements MetadataReport { // Log output protected final Logger logger = LoggerFactory.getLogger(getClass()); - // Local disk cache, where the special key value.registries records the list of registry centers, and the others are the list of notified service providers + // Local disk cache, where the special key value.registries records the list of metadata centers, and the others are the list of notified service providers final Properties properties = new Properties(); private final ExecutorService reportCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveMetadataReport", true)); final Map allMetadataReports = new ConcurrentHashMap<>(4); @@ -336,7 +336,7 @@ long calculateStartTime() { class MetadataReportRetry { protected final Logger logger = LoggerFactory.getLogger(getClass()); - final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(0, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); + final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(0, new NamedThreadFactory("DubboMetadataReportRetryTimer", true)); volatile ScheduledFuture retryScheduledFuture; AtomicInteger retryCounter = new AtomicInteger(0); // retry task schedule period @@ -358,7 +358,7 @@ void startRetryTask() { retryScheduledFuture = retryExecutor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { - // Check and connect to the registry + // Check and connect to the metadata try { int times = retryCounter.incrementAndGet(); logger.info("start to retry task for metadata report. retry times:" + times); From d02babdb265d95bcb4e5f73a17d4715effb3165a Mon Sep 17 00:00:00 2001 From: cvictory Date: Wed, 27 Feb 2019 19:09:12 +0800 Subject: [PATCH 03/10] fix #3289. add route rule enhance --- .../main/java/org/apache/dubbo/common/utils/CIDRUtils.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java new file mode 100644 index 00000000000..bd2dd9b2903 --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java @@ -0,0 +1,7 @@ +package org.apache.dubbo.common.utils; + +/** + * @author cvictory ON 2019-02-27 + */ +public class CIDRUtils { +} From 6cd67a7a20c46337d11d811584d92d5b7c53ba6d Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 28 Feb 2019 16:25:39 +0800 Subject: [PATCH 04/10] fix #3289. add route rule enhance --- .../rpc/cluster/router/tag/TagRouter.java | 25 ++- .../apache/dubbo/common/utils/CIDRUtils.java | 151 +++++++++++++++++- .../apache/dubbo/common/utils/NetUtils.java | 71 +++++++- .../dubbo/common/utils/CIDRUtilsTest.java | 40 +++++ .../dubbo/common/utils/NetUtilsTest.java | 51 ++++++ 5 files changed, 331 insertions(+), 7 deletions(-) create mode 100644 dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java index c8e8750bff4..2a7b9451567 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java @@ -21,6 +21,7 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.common.utils.CollectionUtils; +import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.configcenter.ConfigChangeEvent; import org.apache.dubbo.configcenter.ConfigChangeType; @@ -34,6 +35,7 @@ import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule; import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser; +import java.net.UnknownHostException; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -143,10 +145,10 @@ public List> route(List> invokers, URL url, Invocation /** * If there's no dynamic tag rule being set, use static tag in URL. - * + *

* A typical scenario is a Consumer using version 2.7.x calls Providers using version 2.6.x or lower, * the Consumer should always respect the tag in provider URL regardless of whether a dynamic tag rule has been set to it or not. - * + *

* TODO, to guarantee consistent behavior of interoperability between 2.6- and 2.7+, this method should has the same logic with the TagRouter in 2.6.x. * * @param invokers @@ -199,11 +201,26 @@ private List> filterInvoker(List> invokers, Predicate< } private boolean addressMatches(URL url, List addresses) { - return addresses != null && addresses.contains(url.getAddress()); + return addresses != null && checkAddressMatch(addresses, url.getAddress()); } private boolean addressNotMatches(URL url, List addresses) { - return addresses == null || !addresses.contains(url.getAddress()); + return addresses == null || !checkAddressMatch(addresses, url.getAddress()); + } + + private boolean checkAddressMatch(List addresses, String targetAddress) { + for (String address : addresses) { + try { + if (NetUtils.matchIpExpression(address, targetAddress)) { + return true; + } + } catch (UnknownHostException e) { + logger.error("The format of ip address is invalid in tag route. Address :" + address, e); + } catch (Exception e) { + logger.error("The format of ip address is invalid in tag route. Address :" + address, e); + } + } + return false; } public void setApplication(String app) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java index bd2dd9b2903..31228886485 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java @@ -1,7 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ +/* + * The MIT License + * + * Copyright (c) 2013 Edin Dazdarevic (edin.dazdarevic@gmail.com) + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **/ package org.apache.dubbo.common.utils; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + /** - * @author cvictory ON 2019-02-27 + * A class that enables to get an IP range from CIDR specification. It supports + * both IPv4 and IPv6. + *

+ * From https://github.com/edazdarevic/CIDRUtils/blob/master/CIDRUtils.java */ public class CIDRUtils { + private final String cidr; + + private InetAddress inetAddress; + private InetAddress startAddress; + private InetAddress endAddress; + private final int prefixLength; + + + public CIDRUtils(String cidr) throws UnknownHostException { + + this.cidr = cidr; + + /* split CIDR to address and prefix part */ + if (this.cidr.contains("/")) { + int index = this.cidr.indexOf("/"); + String addressPart = this.cidr.substring(0, index); + String networkPart = this.cidr.substring(index + 1); + + inetAddress = InetAddress.getByName(addressPart); + prefixLength = Integer.parseInt(networkPart); + + calculate(); + } else { + throw new IllegalArgumentException("not an valid CIDR format!"); + } + } + + + private void calculate() throws UnknownHostException { + + ByteBuffer maskBuffer; + int targetSize; + if (inetAddress.getAddress().length == 4) { + maskBuffer = + ByteBuffer + .allocate(4) + .putInt(-1); + targetSize = 4; + } else { + maskBuffer = ByteBuffer.allocate(16) + .putLong(-1L) + .putLong(-1L); + targetSize = 16; + } + + BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength); + + ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress()); + BigInteger ipVal = new BigInteger(1, buffer.array()); + + BigInteger startIp = ipVal.and(mask); + BigInteger endIp = startIp.add(mask.not()); + + byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize); + byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize); + + this.startAddress = InetAddress.getByAddress(startIpArr); + this.endAddress = InetAddress.getByAddress(endIpArr); + + } + + private byte[] toBytes(byte[] array, int targetSize) { + int counter = 0; + List newArr = new ArrayList(); + while (counter < targetSize && (array.length - 1 - counter >= 0)) { + newArr.add(0, array[array.length - 1 - counter]); + counter++; + } + + int size = newArr.size(); + for (int i = 0; i < (targetSize - size); i++) { + + newArr.add(0, (byte) 0); + } + + byte[] ret = new byte[newArr.size()]; + for (int i = 0; i < newArr.size(); i++) { + ret[i] = newArr.get(i); + } + return ret; + } + +// public String getNetworkAddress() { +// +// return this.startAddress.getHostAddress(); +// } +// +// public String getBroadcastAddress() { +// return this.endAddress.getHostAddress(); +// } + + public boolean isInRange(String ipAddress) throws UnknownHostException { + InetAddress address = InetAddress.getByName(ipAddress); + BigInteger start = new BigInteger(1, this.startAddress.getAddress()); + BigInteger end = new BigInteger(1, this.endAddress.getAddress()); + BigInteger target = new BigInteger(1, address.getAddress()); + + int st = start.compareTo(target); + int te = target.compareTo(end); + + return (st == -1 || st == 0) && (te == -1 || te == 0); + } } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index eae4574edfa..915cdcfde54 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -340,13 +340,13 @@ public static String toURL(String protocol, String host, int port, String path) return sb.toString(); } - public static void joinMulticastGroup (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException { + public static void joinMulticastGroup(MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException { setInterface(multicastSocket, multicastAddress instanceof Inet6Address); multicastSocket.setLoopbackMode(false); multicastSocket.joinGroup(multicastAddress); } - public static void setInterface (MulticastSocket multicastSocket, boolean preferIpv6) throws IOException{ + public static void setInterface(MulticastSocket multicastSocket, boolean preferIpv6) throws IOException { boolean interfaceSet = false; Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { @@ -370,4 +370,71 @@ public static void setInterface (MulticastSocket multicastSocket, boolean prefer } } + public static boolean matchIpExpression(String pattern, String address) throws UnknownHostException { + + if (pattern.contains("/")) { + CIDRUtils utils = new CIDRUtils(pattern); + return utils.isInRange(address); + } + + + return matchIpRange(pattern, address); + } + + public static boolean matchIpRange(String pattern, String address) throws UnknownHostException { + if (pattern.equals("*.*.*.*") || pattern.equals("*")) { + return true; + } + + InetAddress inetAddress = InetAddress.getByName(address); + boolean isIpv4 = isValidV4Address(inetAddress) ? true : false; + String splitCharacter = "\\."; + String[] mask; + if (!isIpv4) { + splitCharacter = ":"; + //check format of pattern + mask = pattern.split(splitCharacter); + if (mask.length != 8 && pattern.contains("*")) { + throw new IllegalArgumentException("If you config ip expression that contains '*', please fill qulified ip pattern like 234e:0:4567:0:0:0:3d:*. "); + } + } else { + mask = pattern.split(splitCharacter); + } + + address = inetAddress.getHostAddress(); + + String[] ip_address = address.split(splitCharacter); + if (pattern.equals(address)) { + return true; + } + for (int i = 0; i < mask.length; i++) { + if (mask[i].equals("*") || mask[i].equals(ip_address[i])) { + continue; + } else if (mask[i].contains("-")) { + String[] rangeNumStrs = mask[i].split("-"); + if (rangeNumStrs.length != 2) { + throw new IllegalArgumentException("There are wrong format of ip Address: " + mask[i]); + } + Integer min = getNumOfIpSegment(rangeNumStrs[0], isIpv4); + Integer max = getNumOfIpSegment(rangeNumStrs[1], isIpv4); + Integer ip = getNumOfIpSegment(ip_address[i], isIpv4); + if (ip < min || ip > max) { + return false; + } + } else if ("0".equals(ip_address[i]) && ("0".equals(mask[i]) || "00".equals(mask[i]) || "000".equals(mask[i]) || "0000".equals(mask[i]))) { + continue; + } else if (!mask[i].equals(ip_address[i])) { + return false; + } + } + return true; + } + + private static Integer getNumOfIpSegment(String ipSegment, boolean isIpv4) { + if (isIpv4) { + return Integer.parseInt(ipSegment); + } + return Integer.parseInt(ipSegment, 16); + } + } diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java new file mode 100644 index 00000000000..a61501729f4 --- /dev/null +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java @@ -0,0 +1,40 @@ +package org.apache.dubbo.common.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.net.UnknownHostException; + +/** + * @author cvictory ON 2019-02-28 + */ +public class CIDRUtilsTest { + + @Test + public void testIpv4() throws UnknownHostException { + CIDRUtils cidrUtils = new CIDRUtils("192.168.1.0/26"); + Assertions.assertTrue(cidrUtils.isInRange("192.168.1.63")); + Assertions.assertFalse(cidrUtils.isInRange("192.168.1.65")); + + cidrUtils = new CIDRUtils("192.168.1.192/26"); + Assertions.assertTrue(cidrUtils.isInRange("192.168.1.199")); + Assertions.assertFalse(cidrUtils.isInRange("192.168.1.190")); + } + + @Test + public void testIpv6() throws UnknownHostException { + CIDRUtils cidrUtils = new CIDRUtils("234e:0:4567::3d/64"); + Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::3e")); + Assertions.assertTrue(cidrUtils.isInRange("234e:0:4567::ffff:3e")); + Assertions.assertFalse(cidrUtils.isInRange("234e:1:4567::3d")); + Assertions.assertFalse(cidrUtils.isInRange("234e:0:4567:1::3d")); + + cidrUtils = new CIDRUtils("3FFE:FFFF:0:CC00::/54"); + Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00::dd")); + Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC00:0000:eeee:0909:dd")); + Assertions.assertTrue(cidrUtils.isInRange("3FFE:FFFF:0:CC0F:0000:eeee:0909:dd")); + + Assertions.assertFalse(cidrUtils.isInRange("3EFE:FFFE:0:C107::dd")); + Assertions.assertFalse(cidrUtils.isInRange("1FFE:FFFE:0:CC00::dd")); + } +} diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index d193fe007d2..52dea654a5b 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -23,6 +23,7 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.UnknownHostException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -32,6 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -207,4 +209,53 @@ public void testNormalizeV6Address() { InetAddress normalized = NetUtils.normalizeV6Address(address); assertThat(normalized.getHostAddress(), equalTo("fe80:0:0:0:894:aeec:f37d:23e1%5")); } + + @Test + public void testMatchIpRangeMatchWhenIpv4() throws UnknownHostException { + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63")); + assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63")); + assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63")); + assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63")); + assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63")); + assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63")); + } + + @Test + public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee")); + + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee")); + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee")); + } + + @Test + public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> + assertTrue(NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff"))); + assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); + + thrown = + assertThrows(IllegalArgumentException.class, () -> + assertTrue(NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63"))); + assertTrue(thrown.getMessage().contains("There are wrong format of ip Address")); + } + + @Test + public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { + UnknownHostException thrown = + assertThrows(UnknownHostException.class, () -> + assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff"))); + assertTrue(thrown.getMessage().contains("192.168.1.ff")); + } + + @Test + public void testMatchIpMatch() throws UnknownHostException { + assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63")); + assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199")); + } } From a8740300f047f4090622d4df1ac91dff5c1a351e Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 28 Feb 2019 16:52:16 +0800 Subject: [PATCH 05/10] fix test --- .../java/org/apache/dubbo/common/utils/NetUtilsTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index 52dea654a5b..a5e2aa616ec 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -236,12 +236,12 @@ public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> - assertTrue(NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff"))); + NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff")); assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); thrown = assertThrows(IllegalArgumentException.class, () -> - assertTrue(NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63"))); + NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63")); assertTrue(thrown.getMessage().contains("There are wrong format of ip Address")); } @@ -249,7 +249,7 @@ public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { UnknownHostException thrown = assertThrows(UnknownHostException.class, () -> - assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff"))); + NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff")); assertTrue(thrown.getMessage().contains("192.168.1.ff")); } From c2e36122c1ae8605a4cd4ba1dc07287d9c1f8769 Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 28 Feb 2019 17:24:49 +0800 Subject: [PATCH 06/10] fill license --- LICENSE | 5 +++ .../apache/dubbo/common/utils/CIDRUtils.java | 32 +++++-------------- .../dubbo/common/utils/CIDRUtilsTest.java | 16 ++++++++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/LICENSE b/LICENSE index a35e3b94fe0..11eea3b82db 100644 --- a/LICENSE +++ b/LICENSE @@ -220,3 +220,8 @@ This product contains a modified portion of 'Netty', an event-driven asynchronou * io.netty.util.Timeout * io.netty.util.HashedWheelTimer +For the org.apache.dubbo.common.utils.CIDRUtils : + +This product contains a modified portion of 'edazdarevic.commons.net.CIDRUtils', + under a "MIT License" license, see https://github.com/edazdarevic/CIDRUtils/blob/master/CIDRUtils.java + diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java index 31228886485..5d9dc8bcbdf 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java @@ -1,19 +1,3 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ /* * The MIT License * @@ -133,14 +117,14 @@ private byte[] toBytes(byte[] array, int targetSize) { return ret; } -// public String getNetworkAddress() { -// -// return this.startAddress.getHostAddress(); -// } -// -// public String getBroadcastAddress() { -// return this.endAddress.getHostAddress(); -// } + public String getNetworkAddress() { + + return this.startAddress.getHostAddress(); + } + + public String getBroadcastAddress() { + return this.endAddress.getHostAddress(); + } public boolean isInRange(String ipAddress) throws UnknownHostException { InetAddress address = InetAddress.getByName(ipAddress); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java index a61501729f4..d0c627d174b 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.common.utils; import org.junit.jupiter.api.Assertions; From 06fd3c532dc657a514e84f0f0b0552c2adfecddf Mon Sep 17 00:00:00 2001 From: cvictory Date: Mon, 4 Mar 2019 17:48:25 +0800 Subject: [PATCH 07/10] fix #3289. add suport on port --- .../rpc/cluster/router/tag/TagRouter.java | 8 +- .../apache/dubbo/common/utils/NetUtils.java | 105 ++++++++++++++---- .../dubbo/common/utils/NetUtilsTest.java | 36 +++--- 3 files changed, 108 insertions(+), 41 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java index 2a7b9451567..95354949ec7 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java @@ -201,17 +201,17 @@ private List> filterInvoker(List> invokers, Predicate< } private boolean addressMatches(URL url, List addresses) { - return addresses != null && checkAddressMatch(addresses, url.getAddress()); + return addresses != null && checkAddressMatch(addresses, url.getHost(), url.getPort()); } private boolean addressNotMatches(URL url, List addresses) { - return addresses == null || !checkAddressMatch(addresses, url.getAddress()); + return addresses == null || !checkAddressMatch(addresses, url.getHost(), url.getPort()); } - private boolean checkAddressMatch(List addresses, String targetAddress) { + private boolean checkAddressMatch(List addresses, String host, int port) { for (String address : addresses) { try { - if (NetUtils.matchIpExpression(address, targetAddress)) { + if (NetUtils.matchIpExpression(address, host, port)) { return true; } } catch (UnknownHostException e) { diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 915cdcfde54..645cfad1c53 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -30,6 +30,7 @@ import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.UnknownHostException; +import java.util.Arrays; import java.util.Enumeration; import java.util.Map; import java.util.Optional; @@ -57,6 +58,9 @@ public class NetUtils { private static final Map hostNameCache = new LRUCache<>(1000); private static volatile InetAddress LOCAL_ADDRESS = null; + private static final String SPLIT_IPV4_CHARECTER = "\\."; + private static final String SPLIT_IPV6_CHARECTER = ":"; + public static int getRandomPort() { return RND_PORT_START + ThreadLocalRandom.current().nextInt(RND_PORT_RANGE); } @@ -370,50 +374,72 @@ public static void setInterface(MulticastSocket multicastSocket, boolean preferI } } - public static boolean matchIpExpression(String pattern, String address) throws UnknownHostException { + public static boolean matchIpExpression(String pattern, String host, int port) throws UnknownHostException { if (pattern.contains("/")) { CIDRUtils utils = new CIDRUtils(pattern); - return utils.isInRange(address); + return utils.isInRange(host); } - return matchIpRange(pattern, address); + return matchIpRange(pattern, host, port); } - public static boolean matchIpRange(String pattern, String address) throws UnknownHostException { + /** + * @param pattern + * @param host + * @param port + * @return + * @throws UnknownHostException + */ + public static boolean matchIpRange(String pattern, String host, int port) throws UnknownHostException { + if (pattern == null || host == null) { + throw new IllegalArgumentException("Illegal Argument pattern or hostName. Pattern:" + pattern + ", Host:" + host); + } + pattern = pattern.trim(); if (pattern.equals("*.*.*.*") || pattern.equals("*")) { return true; } - InetAddress inetAddress = InetAddress.getByName(address); + InetAddress inetAddress = InetAddress.getByName(host); boolean isIpv4 = isValidV4Address(inetAddress) ? true : false; - String splitCharacter = "\\."; - String[] mask; + String[] hostAndPort = getPatternHostAndPort(pattern, isIpv4); + if (hostAndPort[1] != null && !hostAndPort[1].equals(String.valueOf(port))) { + logger.info("The pattern port is not equals to port form param."); + return false; + } + pattern = hostAndPort[0]; + + String splitCharacter = SPLIT_IPV4_CHARECTER; if (!isIpv4) { - splitCharacter = ":"; - //check format of pattern - mask = pattern.split(splitCharacter); - if (mask.length != 8 && pattern.contains("*")) { - throw new IllegalArgumentException("If you config ip expression that contains '*', please fill qulified ip pattern like 234e:0:4567:0:0:0:3d:*. "); - } - } else { - mask = pattern.split(splitCharacter); + splitCharacter = SPLIT_IPV6_CHARECTER; } + String[] mask = pattern.split(splitCharacter); + //check format of pattern + checkHostPattern(pattern, mask, isIpv4); - address = inetAddress.getHostAddress(); + host = inetAddress.getHostAddress(); - String[] ip_address = address.split(splitCharacter); - if (pattern.equals(address)) { + String[] ip_address = host.split(splitCharacter); + if (pattern.equals(host)) { return true; } + // short name conditon + if (!ipPatternContainExpression(pattern)) { + InetAddress patternAddress = InetAddress.getByName(pattern); + if (patternAddress.getHostAddress().equals(host)) { + return true; + } else { + return false; + } + } for (int i = 0; i < mask.length; i++) { if (mask[i].equals("*") || mask[i].equals(ip_address[i])) { continue; } else if (mask[i].contains("-")) { String[] rangeNumStrs = mask[i].split("-"); if (rangeNumStrs.length != 2) { - throw new IllegalArgumentException("There are wrong format of ip Address: " + mask[i]); + throw new IllegalArgumentException("There is wrong format of ip Address: " + mask[i]); } Integer min = getNumOfIpSegment(rangeNumStrs[0], isIpv4); Integer max = getNumOfIpSegment(rangeNumStrs[1], isIpv4); @@ -430,6 +456,47 @@ public static boolean matchIpRange(String pattern, String address) throws Unknow return true; } + private static boolean ipPatternContainExpression(String pattern) { + return pattern.contains("*") || pattern.contains("-"); + } + + private static void checkHostPattern(String pattern, String[] mask, boolean isIpv4) { + if (!isIpv4) { + if (mask.length != 8 && ipPatternContainExpression(pattern)) { + throw new IllegalArgumentException("If you config ip expression that contains '*' or '-', please fill qulified ip pattern like 234e:0:4567:0:0:0:3d:*. "); + } + if (mask.length != 8 && !pattern.contains("::")) { + throw new IllegalArgumentException("The host is ipv6, but the pattern is not ipv6 pattern : " + pattern); + } + } else { + if (mask.length != 4) { + throw new IllegalArgumentException("The host is ipv4, but the pattern is not ipv4 pattern : " + pattern); + } + } + } + + private static String[] getPatternHostAndPort(String pattern, boolean isIpv4) { + String[] result = new String[2]; + if (pattern.startsWith("[") && pattern.contains("]:")) { + int end = pattern.indexOf("]:"); + result[0] = pattern.substring(1, end); + result[1] = pattern.substring(end + 2); + return result; + } else if (pattern.startsWith("[") && pattern.endsWith("]")) { + result[0] = pattern.substring(1, pattern.length() - 1); + result[1] = null; + return result; + } else if (isIpv4 && pattern.contains(":")) { + int end = pattern.indexOf(":"); + result[0] = pattern.substring(0, end); + result[1] = pattern.substring(end + 1); + return result; + } else { + result[0] = pattern; + return result; + } + } + private static Integer getNumOfIpSegment(String ipSegment, boolean isIpv4) { if (isIpv4) { return Integer.parseInt(ipSegment); diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index a5e2aa616ec..c057bf6f31f 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -212,36 +212,36 @@ public void testNormalizeV6Address() { @Test public void testMatchIpRangeMatchWhenIpv4() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63")); - assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63")); - assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63")); - assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63")); - assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63")); - assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63")); + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63", "90")); + assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63", "90")); + assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63", "90")); } @Test public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee")); + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff", "90")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee", "90")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee", "90")); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee", "90")); - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee")); - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee")); + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee", "90")); + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee", "90")); } @Test public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff")); + NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff", "90")); assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); thrown = assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63")); + NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63", "90")); assertTrue(thrown.getMessage().contains("There are wrong format of ip Address")); } @@ -249,13 +249,13 @@ public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { UnknownHostException thrown = assertThrows(UnknownHostException.class, () -> - NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff")); + NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff", "90")); assertTrue(thrown.getMessage().contains("192.168.1.ff")); } @Test public void testMatchIpMatch() throws UnknownHostException { - assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63")); - assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199")); + assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", "90")); } } From 795e60641f07d9af35a0894804b382fff219c1a0 Mon Sep 17 00:00:00 2001 From: cvictory Date: Tue, 5 Mar 2019 10:52:27 +0800 Subject: [PATCH 08/10] fix #3289. remove unused import --- .../apache/dubbo/common/utils/NetUtils.java | 4 +- .../dubbo/common/utils/NetUtilsTest.java | 80 ++++++++++++++----- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index 645cfad1c53..b59814903c3 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -30,7 +30,6 @@ import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.UnknownHostException; -import java.util.Arrays; import java.util.Enumeration; import java.util.Map; import java.util.Optional; @@ -376,6 +375,7 @@ public static void setInterface(MulticastSocket multicastSocket, boolean preferI public static boolean matchIpExpression(String pattern, String host, int port) throws UnknownHostException { + // if the pattern is subnet format, it will not be allowed to config port param in pattern. if (pattern.contains("/")) { CIDRUtils utils = new CIDRUtils(pattern); return utils.isInRange(host); @@ -424,7 +424,7 @@ public static boolean matchIpRange(String pattern, String host, int port) throws if (pattern.equals(host)) { return true; } - // short name conditon + // short name condition if (!ipPatternContainExpression(pattern)) { InetAddress patternAddress = InetAddress.getByName(pattern); if (patternAddress.getHostAddress().equals(host)) { diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java index c057bf6f31f..2fe0a636815 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/NetUtilsTest.java @@ -212,50 +212,90 @@ public void testNormalizeV6Address() { @Test public void testMatchIpRangeMatchWhenIpv4() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", "90")); - assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63", "90")); - assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63", "90")); - assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63", "90")); - assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63", "90")); - assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63", "90")); + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.*", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.63", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.1-65", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.1-61", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.62", "192.168.1.63", 90)); } @Test public void testMatchIpRangeMatchWhenIpv6() throws UnknownHostException { - assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", "90")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff", "90")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee", "90")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee", "90")); - assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee", "90")); - - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee", "90")); - assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee", "90")); + assertTrue(NetUtils.matchIpRange("*.*.*.*", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:*", "234e:0:4567::3d:ff", 90)); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ee", "234e:0:4567::3d:ee", 90)); + assertTrue(NetUtils.matchIpRange("234e:0:4567::3d:ee", "234e:0:4567::3d:ee", 90)); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ff", "234e:0:4567::3d:ee", 90)); + assertTrue(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ee", "234e:0:4567::3d:ee", 90)); + + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:ff", "234e:0:4567::3d:ee", 90)); + assertFalse(NetUtils.matchIpRange("234e:0:4567:0:0:0:3d:0-ea", "234e:0:4567::3d:ee", 90)); } @Test public void testMatchIpRangeMatchWhenIpv6Exception() throws UnknownHostException { IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff", "90")); + NetUtils.matchIpRange("234e:0:4567::3d:*", "234e:0:4567::3d:ff", 90)); assertTrue(thrown.getMessage().contains("If you config ip expression that contains '*'")); + thrown = assertThrows(IllegalArgumentException.class, () -> + NetUtils.matchIpRange("234e:0:4567:3d", "234e:0:4567::3d:ff", 90)); + assertTrue(thrown.getMessage().contains("The host is ipv6, but the pattern is not ipv6 pattern")); + thrown = assertThrows(IllegalArgumentException.class, () -> - NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63", "90")); - assertTrue(thrown.getMessage().contains("There are wrong format of ip Address")); + NetUtils.matchIpRange("192.168.1.1-65-3", "192.168.1.63", 90)); + assertTrue(thrown.getMessage().contains("There is wrong format of ip Address")); } @Test public void testMatchIpRangeMatchWhenIpWrongException() throws UnknownHostException { UnknownHostException thrown = assertThrows(UnknownHostException.class, () -> - NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff", "90")); + NetUtils.matchIpRange("192.168.1.63", "192.168.1.ff", 90)); assertTrue(thrown.getMessage().contains("192.168.1.ff")); } @Test public void testMatchIpMatch() throws UnknownHostException { - assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63", "90")); - assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", "90")); + assertTrue(NetUtils.matchIpExpression("192.168.1.*", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpExpression("192.168.1.192/26", "192.168.1.199", 90)); + } + + @Test + public void testMatchIpv6WithIpPort() throws UnknownHostException { + assertTrue(NetUtils.matchIpRange("[234e:0:4567::3d:ee]", "234e:0:4567::3d:ee", 8090)); + assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]", "234e:0:4567::3d:ee", 8090)); + assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:8090", "234e:0:4567::3d:ee", 8090)); + assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:0-ee]:8090", "234e:0:4567::3d:ee", 8090)); + assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 8090)); + assertTrue(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:*]:90", "234e:0:4567::3d:ff", 90)); + + assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee]:7289", "234e:0:4567::3d:ee", 8090)); + assertFalse(NetUtils.matchIpRange("[234e:0:4567:0:0:0:3d:ee-ff]:8090", "234e:0:4567::3d:ee", 9090)); + } + + @Test + public void testMatchIpv4WithIpPort() throws UnknownHostException { + NumberFormatException thrown = + assertThrows(NumberFormatException.class, () ->NetUtils.matchIpExpression("192.168.1.192/26:90", "192.168.1.199", 90)); + assertTrue(thrown instanceof NumberFormatException); + + assertTrue(NetUtils.matchIpRange("*.*.*.*:90", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.*:90", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.63:90", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.63-65:90", "192.168.1.63", 90)); + assertTrue(NetUtils.matchIpRange("192.168.1.1-63:90", "192.168.1.63", 90)); + + assertFalse(NetUtils.matchIpRange("*.*.*.*:80", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.*:80", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.63:80", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.63-65:80", "192.168.1.63", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.1-63:80", "192.168.1.63", 90)); + + assertFalse(NetUtils.matchIpRange("192.168.1.1-61:90", "192.168.1.62", 90)); + assertFalse(NetUtils.matchIpRange("192.168.1.62:90", "192.168.1.63", 90)); } } From 19425cd438ae8340d64eda07342eff6deaa83d02 Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 7 Mar 2019 17:08:46 +0800 Subject: [PATCH 09/10] remove info log --- .../src/main/java/org/apache/dubbo/common/utils/NetUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java index b59814903c3..91f04b8a90d 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java @@ -405,7 +405,6 @@ public static boolean matchIpRange(String pattern, String host, int port) throws boolean isIpv4 = isValidV4Address(inetAddress) ? true : false; String[] hostAndPort = getPatternHostAndPort(pattern, isIpv4); if (hostAndPort[1] != null && !hostAndPort[1].equals(String.valueOf(port))) { - logger.info("The pattern port is not equals to port form param."); return false; } pattern = hostAndPort[0]; From 244af120d87f9142f0c5e11061946ca7b9036a90 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 7 Mar 2019 17:34:16 +0800 Subject: [PATCH 10/10] change rule check condition from "!=null" to "isNotEmpty" --- .../rpc/cluster/router/condition/config/ListenableRouter.java | 2 +- .../java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java index 65a4eec7d52..eca9f335756 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/config/ListenableRouter.java @@ -120,7 +120,7 @@ private synchronized void init(String ruleKey) { String routerKey = ruleKey + RULE_SUFFIX; configuration.addListener(routerKey, this); String rule = configuration.getConfig(routerKey); - if (rule != null) { + if (StringUtils.isNotEmpty(rule)) { this.process(new ConfigChangeEvent(routerKey, rule)); } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java index 95354949ec7..9094449575e 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java @@ -252,7 +252,7 @@ public void notify(List> invokers) { configuration.addListener(key, this); application = providerApplication; String rawRule = configuration.getConfig(key); - if (rawRule != null) { + if (StringUtils.isNotEmpty(rawRule)) { this.process(new ConfigChangeEvent(key, rawRule)); } }