-
Notifications
You must be signed in to change notification settings - Fork 26.5k
fix issue #3289. enhance tagRoute: support ip expression match #3578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0a8422
fix #2842. remove duplicate SPI definitions for 2.7.x
cvictory 98b2f4b
Merge branch 'master' of github.com:apache/incubator-dubbo
cvictory 19824c3
fix: rename the thread name from DubboRegistryFailedRetryTimer to Dub…
cvictory c313aea
Merge branch 'master' of github.com:apache/incubator-dubbo
cvictory d02babd
fix #3289. add route rule enhance
cvictory 6cd67a7
fix #3289. add route rule enhance
cvictory a874030
fix test
cvictory c2e3612
fill license
cvictory 06fd3c5
fix #3289. add suport on port
cvictory 795e606
fix #3289. remove unused import
cvictory 19425cd
remove info log
cvictory 244af12
change rule check condition from "!=null" to "isNotEmpty"
chickenlj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
dubbo-common/src/main/java/org/apache/dubbo/common/utils/CIDRUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * A class that enables to get an IP range from CIDR specification. It supports | ||
| * both IPv4 and IPv6. | ||
| * <p> | ||
| * 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<Byte> newArr = new ArrayList<Byte>(); | ||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
dubbo-common/src/test/java/org/apache/dubbo/common/utils/CIDRUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * 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; | ||
| 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")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.