Skip to content

Commit 4403ea8

Browse files
Merge branch 'dev' into Improvement-Metrics
2 parents 043ed2f + 40493c3 commit 4403ea8

2 files changed

Lines changed: 70 additions & 6 deletions

File tree

  • dolphinscheduler-common/src

dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private NetUtils() {
6767
* @return addr
6868
*/
6969
public static String getAddr(String host, int port) {
70+
String normalizedHost = unwrapIpv6Literal(host);
71+
if (isIpv6Literal(normalizedHost)) {
72+
return String.format("[%s]:%d", normalizedHost, port);
73+
}
7074
return String.format("%s:%d", host, port);
7175
}
7276

@@ -92,6 +96,9 @@ public static String getHost(InetAddress inetAddress) {
9296
}
9397
return canonicalHost;
9498
}
99+
if (inetAddress instanceof Inet6Address) {
100+
return normalizeV6Address((Inet6Address) inetAddress).getHostAddress();
101+
}
95102
return inetAddress.getHostAddress();
96103
}
97104
return null;
@@ -134,10 +141,10 @@ private static synchronized InetAddress getLocalAddress0() {
134141

135142
private static InetAddress normalizeV6Address(Inet6Address address) {
136143
String addr = address.getHostAddress();
137-
int i = addr.lastIndexOf('%');
138-
if (i > 0) {
144+
String normalizedAddr = stripIpv6Scope(addr);
145+
if (!StringUtils.equals(addr, normalizedAddr)) {
139146
try {
140-
return InetAddress.getByName(addr.substring(0, i) + '%' + address.getScopeId());
147+
return InetAddress.getByName(normalizedAddr);
141148
} catch (UnknownHostException e) {
142149
log.debug("Unknown IPV6 address: ", e);
143150
}
@@ -160,11 +167,12 @@ protected static boolean isValidV6Address(InetAddress address) {
160167
if (!(address instanceof Inet6Address)) {
161168
return false;
162169
}
163-
String name = address.getHostAddress();
170+
String name = stripIpv6Scope(address.getHostAddress());
164171
return (name != null
165172
&& InetAddressUtils.isIPv6Address(name)
166173
&& !address.isAnyLocalAddress()
167-
&& !address.isLoopbackAddress());
174+
&& !address.isLoopbackAddress()
175+
&& !address.isLinkLocalAddress());
168176
}
169177

170178
/**
@@ -313,6 +321,31 @@ private static List<NetworkInterface> getAllNetworkInterfaces() throws SocketExc
313321
return validNetworkInterfaces;
314322
}
315323

324+
private static boolean isIpv6Literal(String host) {
325+
return StringUtils.isNotEmpty(host) && InetAddressUtils.isIPv6Address(stripIpv6Scope(host));
326+
}
327+
328+
private static String stripIpv6Scope(String host) {
329+
if (StringUtils.isEmpty(host)) {
330+
return host;
331+
}
332+
int index = host.lastIndexOf('%');
333+
if (index > 0) {
334+
return host.substring(0, index);
335+
}
336+
return host;
337+
}
338+
339+
private static String unwrapIpv6Literal(String host) {
340+
if (StringUtils.isEmpty(host)) {
341+
return host;
342+
}
343+
if (host.startsWith("[") && host.endsWith("]")) {
344+
return host.substring(1, host.length() - 1);
345+
}
346+
return host;
347+
}
348+
316349
private static String specifyNetworkInterfaceName() {
317350
return PropertyUtils.getString(DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED,
318351
System.getProperty(DOLPHIN_SCHEDULER_NETWORK_INTERFACE_PREFERRED));

dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import static org.mockito.Mockito.when;
2323

2424
import java.net.Inet4Address;
25+
import java.net.Inet6Address;
2526
import java.net.InetAddress;
27+
import java.net.UnknownHostException;
2628

2729
import org.junit.jupiter.api.Assertions;
2830
import org.junit.jupiter.api.Test;
@@ -32,9 +34,13 @@ public class NetUtilsTest {
3234

3335
@Test
3436
public void testGetAddr() {
35-
Assertions.assertEquals(NetUtils.getHost() + ":5678", NetUtils.getAddr(5678));
37+
Assertions.assertEquals(NetUtils.getAddr(NetUtils.getHost(), 5678), NetUtils.getAddr(5678));
3638
Assertions.assertEquals("127.0.0.1:5678", NetUtils.getAddr("127.0.0.1", 5678));
3739
Assertions.assertEquals("localhost:1234", NetUtils.getAddr("localhost", 1234));
40+
Assertions.assertEquals("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]:1234",
41+
NetUtils.getAddr("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831", 1234));
42+
Assertions.assertEquals("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]:1234",
43+
NetUtils.getAddr("[fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831]", 1234));
3844
}
3945

4046
@Test
@@ -77,6 +83,13 @@ public void testGetHostInNonKubernetesMode() {
7783
Assertions.assertEquals("172.17.0.15", NetUtils.getHost(address));
7884
}
7985

86+
@Test
87+
public void testGetHostInNonKubernetesModeShouldStripIpv6Scope() throws UnknownHostException {
88+
Inet6Address address = Inet6Address.getByAddress(null,
89+
InetAddress.getByName("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831").getAddress(), 2);
90+
Assertions.assertEquals("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831", NetUtils.getHost(address));
91+
}
92+
8093
@Test
8194
public void testGetLocalHost() {
8295
Assertions.assertNotNull(NetUtils.getHost());
@@ -116,4 +129,22 @@ public void testIsValidAddress() {
116129
Assertions.assertFalse(NetUtils.isValidV4Address(address));
117130
}
118131

132+
@Test
133+
public void testIsValidV6Address() throws UnknownHostException {
134+
Assertions.assertFalse(NetUtils.isValidV6Address(null));
135+
136+
Inet6Address globalAddress = (Inet6Address) InetAddress
137+
.getByName("fd15:4ba5:5a2b:1008:71bc:dd01:e661:e831");
138+
Assertions.assertTrue(NetUtils.isValidV6Address(globalAddress));
139+
140+
Inet6Address scopedGlobalAddress = Inet6Address.getByAddress(null, globalAddress.getAddress(), 2);
141+
Assertions.assertTrue(NetUtils.isValidV6Address(scopedGlobalAddress));
142+
143+
Inet6Address linkLocalAddress = (Inet6Address) InetAddress.getByName("fe80::20c:29ff:feac:72e7");
144+
Assertions.assertFalse(NetUtils.isValidV6Address(linkLocalAddress));
145+
146+
Inet6Address loopbackAddress = (Inet6Address) InetAddress.getByName("::1");
147+
Assertions.assertFalse(NetUtils.isValidV6Address(loopbackAddress));
148+
}
149+
119150
}

0 commit comments

Comments
 (0)