Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading