提交 dee6c7ba 编写于 作者: D duhenglucky

fix(UtilAll) exclude local link ipv6 address

上级 6a6f2e7d
...@@ -440,13 +440,11 @@ public class UtilAll { ...@@ -440,13 +440,11 @@ public class UtilAll {
return false; return false;
} }
public static boolean isInternalV6IP(byte[] ip) { public static boolean isInternalV6IP(InetAddress inetAddr) {
if (ip.length != 16) { if (inetAddr.isAnyLocalAddress() // Wild card ipv6
throw new RuntimeException("illegal ipv6 bytes"); || inetAddr.isLinkLocalAddress() // Single broadcast ipv6 address: fe80:xx:xx...
} || inetAddr.isLoopbackAddress() //Loopback ipv6 address
|| inetAddr.isSiteLocalAddress()) { // Site local ipv6 address: fec0:xx:xx...
//FEC0:0000:0000:0000:0000:0000:0000:0000/10
if (ip[0] == (byte) 254 && ip[1] >= (byte) 192) {
return true; return true;
} }
return false; return false;
...@@ -457,9 +455,6 @@ public class UtilAll { ...@@ -457,9 +455,6 @@ public class UtilAll {
throw new RuntimeException("illegal ipv4 bytes"); throw new RuntimeException("illegal ipv4 bytes");
} }
// if (ip[0] == (byte)30 && ip[1] == (byte)10 && ip[2] == (byte)163 && ip[3] == (byte)120) {
// }
if (ip[0] >= (byte) 1 && ip[0] <= (byte) 126) { if (ip[0] >= (byte) 1 && ip[0] <= (byte) 126) {
if (ip[1] == (byte) 1 && ip[2] == (byte) 1 && ip[3] == (byte) 1) { if (ip[1] == (byte) 1 && ip[2] == (byte) 1 && ip[3] == (byte) 1) {
return false; return false;
...@@ -550,7 +545,7 @@ public class UtilAll { ...@@ -550,7 +545,7 @@ public class UtilAll {
byte[] ipByte = ip.getAddress(); byte[] ipByte = ip.getAddress();
if (ipByte.length == 16) { if (ipByte.length == 16) {
if (ipV6Check(ipByte)) { if (ipV6Check(ipByte)) {
if (!isInternalV6IP(ipByte)) { if (!isInternalV6IP(ip)) {
return ipByte; return ipByte;
} else if (internalIP == null) { } else if (internalIP == null) {
internalIP = ipByte; internalIP = ipByte;
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
package org.apache.rocketmq.common; package org.apache.rocketmq.common;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties; import java.util.Properties;
import org.junit.Test; import org.junit.Test;
...@@ -99,12 +101,12 @@ public class UtilAllTest { ...@@ -99,12 +101,12 @@ public class UtilAllTest {
} }
@Test @Test
public void testIPv6Check() { public void testIPv6Check() throws UnknownHostException {
byte[] nonInternalIp = UtilAll.string2bytes("24084004018081003FAA1DDE2B3F898A"); InetAddress nonInternal = InetAddress.getByName("2408:4004:0180:8100:3FAA:1DDE:2B3F:898A");
byte[] internalIp = UtilAll.string2bytes("FEC0000000000000000000000000FFFF"); InetAddress internal = InetAddress.getByName("FE80:0000:0000:0000:0000:0000:0000:FFFF");
assertThat(UtilAll.isInternalV6IP(nonInternalIp)).isFalse(); assertThat(UtilAll.isInternalV6IP(nonInternal)).isFalse();
assertThat(UtilAll.isInternalV6IP(internalIp)).isTrue(); assertThat(UtilAll.isInternalV6IP(internal)).isTrue();
assertThat(UtilAll.ipToIPv6Str(nonInternalIp).toUpperCase()).isEqualTo("2408:4004:0180:8100:3FAA:1DDE:2B3F:898A"); assertThat(UtilAll.ipToIPv6Str(nonInternal.getAddress()).toUpperCase()).isEqualTo("2408:4004:0180:8100:3FAA:1DDE:2B3F:898A");
} }
static class DemoConfig { static class DemoConfig {
......
...@@ -17,9 +17,8 @@ ...@@ -17,9 +17,8 @@
package org.apache.rocketmq.common.message; package org.apache.rocketmq.common.message;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test; import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class MessageClientIDSetterTest { public class MessageClientIDSetterTest {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册