Skip to content

Commit

Permalink
fix sofastack#526 (sofastack#527) Supports IPv6
Browse files Browse the repository at this point in the history
* fix sofastack#526

* 删除无用常量

* 添加单元测试

* 代码格式

* 修改测试,isIPv6 异常时直接抛出异常,不return false

* 格式化代码
  • Loading branch information
KeRan213539 authored Oct 22, 2020
1 parent 5129ee8 commit 0d9f17c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public boolean parse(final String s) {
return false;
}

final String[] tmps = StringUtils.splitPreserveAllTokens(s, ':');
final String[] tmps = Utils.parsePeerId(s);
if (tmps.length < 2 || tmps.length > 4) {
return false;
}
Expand Down
58 changes: 58 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -390,4 +391,61 @@ public static void fsync(final File file) throws IOException {
public static String getString(final byte[] bs, final int off, final int len) {
return new String(bs, off, len, StandardCharsets.UTF_8);
}

public static final String IPV6_START_MARK = "[";

public static final String IPV6_END_MARK = "]";

private static final int IPV6_ADDRESS_LENGTH = 16;

/**
* check whether the ip address is IPv6.
*
* @param addr ip address
* @return boolean
*/
public static boolean isIPv6(String addr) {
try {
return InetAddress.getByName(addr).getAddress().length == IPV6_ADDRESS_LENGTH;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}

/**
* Parse peerId from string that generated by {@link #toString()}
* This method can support parameter string values are below:
*
* <pre>
* PeerId.parse("a:b") = new PeerId("a", "b", 0 , -1)
* PeerId.parse("a:b:c") = new PeerId("a", "b", "c", -1)
* PeerId.parse("a:b::d") = new PeerId("a", "b", 0, "d")
* PeerId.parse("a:b:c:d") = new PeerId("a", "b", "c", "d")
* </pre>
*
*/
public static String[] parsePeerId(String s) {
if (s.startsWith(IPV6_START_MARK) && StringUtils.containsIgnoreCase(s, IPV6_END_MARK)) {
String ipv6Addr;
if (s.endsWith(IPV6_END_MARK)) {
ipv6Addr = s;
} else {
ipv6Addr = s.substring(0, (s.indexOf(IPV6_END_MARK) + 1));
}
if (!isIPv6(ipv6Addr)) {
throw new IllegalArgumentException("The IPv6 address(\"" + ipv6Addr + "\") is incorrect.");
}
String tempString = s.substring((s.indexOf(ipv6Addr) + ipv6Addr.length()));
if (tempString.startsWith(":")) {
tempString = tempString.substring(1);
}
String[] tempArr = StringUtils.splitPreserveAllTokens(tempString, ':');
String[] result = new String[1 + tempArr.length];
result[0] = ipv6Addr;
System.arraycopy(tempArr, 0, result, 1, tempArr.length);
return result;
} else {
return StringUtils.splitPreserveAllTokens(s, ':');
}
}
}
58 changes: 58 additions & 0 deletions jraft-core/src/test/java/com/alipay/sofa/jraft/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,62 @@ public void testAllocateExpandByteBuffer() {
assertEquals(1147 + 2048, buf.remaining());
}

@Test
public void testParsePeerId() {
String pid = "192.168.1.88:5566";
String[] result = Utils.parsePeerId(pid);
String[] expecteds = { "192.168.1.88", "5566" };
Assert.assertTrue(result.length == 2);
Assert.assertArrayEquals(expecteds, result);

pid = "[fe80:0:0:0:6450:aa3c:cd98:ed0f]:8847";
result = Utils.parsePeerId(pid);
expecteds = new String[] { "[fe80:0:0:0:6450:aa3c:cd98:ed0f]", "8847" };
Assert.assertTrue(result.length == 2);
Assert.assertArrayEquals(expecteds, result);

pid = "192.168.1.88:5566:9";
result = Utils.parsePeerId(pid);
expecteds = new String[] { "192.168.1.88", "5566", "9" };
Assert.assertTrue(result.length == 3);
Assert.assertArrayEquals(expecteds, result);

pid = "[fe80:0:0:0:6450:aa3c:cd98:ed0f]:8847:9";
result = Utils.parsePeerId(pid);
expecteds = new String[] { "[fe80:0:0:0:6450:aa3c:cd98:ed0f]", "8847", "9" };
Assert.assertTrue(result.length == 3);
Assert.assertArrayEquals(expecteds, result);

pid = "192.168.1.88:5566:0:6";
result = Utils.parsePeerId(pid);
expecteds = new String[] { "192.168.1.88", "5566", "0", "6" };
Assert.assertTrue(result.length == 4);
Assert.assertArrayEquals(expecteds, result);

pid = "[fe80:0:0:0:6450:aa3c:cd98:ed0f]:8847:0:6";
result = Utils.parsePeerId(pid);
expecteds = new String[] { "[fe80:0:0:0:6450:aa3c:cd98:ed0f]", "8847", "0", "6" };
Assert.assertTrue(result.length == 4);
Assert.assertArrayEquals(expecteds, result);

boolean ex1 = false;
try {
pid = "[192.168.1].88:eee:x:b:j";
Utils.parsePeerId(pid);
} catch (Exception e) {
ex1 = true;
}
Assert.assertTrue(ex1);

boolean ex2 = false;
try {
pid = "[dsfsadf]:eee:x:b:j";
Utils.parsePeerId(pid);
} catch (Exception e) {
ex2 = true;
}
Assert.assertTrue(ex2);

}

}

0 comments on commit 0d9f17c

Please sign in to comment.