diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java index 834a5194967d..c4754d412db5 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatch.java @@ -18,6 +18,7 @@ import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.StringUtils; import java.net.UnknownHostException; @@ -60,7 +61,7 @@ public void setExact(String exact) { public boolean isMatch(String input) { if (getCird() != null && input != null) { try { - return input.equals(getCird()) || matchIpExpression(getCird(), input); + return input.equals(getCird()) || matchCird(input); } catch (UnknownHostException e) { logger.error( CLUSTER_FAILED_EXEC_CONDITION_ROUTER, @@ -84,4 +85,15 @@ public boolean isMatch(String input) { } return false; } + + private boolean matchCird(String input) throws UnknownHostException { + String host = input; + int port = 0; + int colonIndex = input.indexOf(':'); + if (colonIndex > 0 && colonIndex == input.lastIndexOf(':')) { + host = input.substring(0, colonIndex); + port = StringUtils.parseInteger(input.substring(colonIndex + 1)); + } + return matchIpExpression(getCird(), host, port); + } } diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java new file mode 100644 index 000000000000..9a49b4266204 --- /dev/null +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/mesh/rule/virtualservice/match/AddressMatchTest.java @@ -0,0 +1,58 @@ +/* + * 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.rpc.cluster.router.mesh.rule.virtualservice.match; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class AddressMatchTest { + + @Test + void cirdMatchIpv4AddressWithPort() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCird("192.168.1.*:90"); + + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + assertFalse(addressMatch.isMatch("192.168.1.63:80")); + } + + @Test + void cirdMatchIpv4AddressWithoutPort() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCird("192.168.1.*"); + + assertTrue(addressMatch.isMatch("192.168.1.63")); + } + + @Test + void cirdMatchExactAddress() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCird("192.168.1.63:90"); + + assertTrue(addressMatch.isMatch("192.168.1.63:90")); + } + + @Test + void cirdMatchIpv6Address() { + AddressMatch addressMatch = new AddressMatch(); + addressMatch.setCird("234e:0:4567:0:0:0:3d:*"); + + assertTrue(addressMatch.isMatch("234e:0:4567::3d:ff")); + } +} 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 766a12427b05..c9ddff2a5612 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 @@ -722,12 +722,14 @@ public static void setInterface(MulticastSocket multicastSocket, boolean preferI } /** - * Check if address matches with specified pattern, currently only supports ipv4, use {@link this#matchIpExpression(String, String, int)} for ipv6 addresses. + * Check if address matches with specified pattern. * * @param pattern cird pattern - * @param address 'ip:port' + * @param address address * @return true if address matches with the pattern + * @deprecated use {@link #matchIpExpression(String, String, int)} with separated host and port instead. */ + @Deprecated public static boolean matchIpExpression(String pattern, String address) throws UnknownHostException { if (address == null) { return false;