caishunfeng commented on code in PR #13013:
URL:
https://github.com/apache/dolphinscheduler/pull/13013#discussion_r1034310432
##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java:
##########
@@ -332,4 +338,52 @@ private static NetworkInterface
findOuterAddress(List<NetworkInterface> validNet
return networkInterface;
}
+ /**
+ * check if address is legal address, a legal address should be:
+ * ipv4: ip:port
+ * ipv6: [ip]:port
+ * @param ipAndPortAddress address to check
+ * @return true if address is legal
+ */
+ public static boolean isLegalAddress(String ipAndPortAddress) {
+ if (StringUtils.isEmpty(ipAndPortAddress)) {
+ return false;
+ }
+
+ String[] ipAndPort = ipAndPortAddress.split(":");
+ if (ipAndPort.length == IPV4_SEMICOLON_PART) {
+ return isValidIPv4Address(ipAndPort[0]) &&
isValidPort(ipAndPort[1]);
+ }
+
+ if (ipAndPortAddress.startsWith(IPV6_STARTER)) {
+ String[] ipv6Formats = ipAndPortAddress.split("]");
Review Comment:
use constant "]"
##########
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java:
##########
@@ -115,4 +115,40 @@ public void testIsValidAddress() {
Assertions.assertFalse(NetUtils.isValidV4Address(address));
}
+ @Test
+ public void
giveIpAddress_thenCheckIsValidIPv4Address_thenCheck_expectNormal() {
+ Assertions.assertFalse(NetUtils.isValidIPv4Address(""));
+
+ String ipAddress = "127.0.0.1";
Review Comment:
can you add a host case?
BTW, in k8s deploy, the server host like:

##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java:
##########
@@ -332,4 +338,52 @@ private static NetworkInterface
findOuterAddress(List<NetworkInterface> validNet
return networkInterface;
}
+ /**
+ * check if address is legal address, a legal address should be:
+ * ipv4: ip:port
+ * ipv6: [ip]:port
+ * @param ipAndPortAddress address to check
+ * @return true if address is legal
+ */
+ public static boolean isLegalAddress(String ipAndPortAddress) {
+ if (StringUtils.isEmpty(ipAndPortAddress)) {
+ return false;
+ }
+
+ String[] ipAndPort = ipAndPortAddress.split(":");
+ if (ipAndPort.length == IPV4_SEMICOLON_PART) {
+ return isValidIPv4Address(ipAndPort[0]) &&
isValidPort(ipAndPort[1]);
+ }
+
+ if (ipAndPortAddress.startsWith(IPV6_STARTER)) {
+ String[] ipv6Formats = ipAndPortAddress.split("]");
+ return isValidIPv6Address(ipv6Formats[0].replace(IPV6_STARTER, ""))
+ && isValidPort(ipv6Formats[1].replace(":", ""));
+ }
+
+ return false;
+ }
+
+ public static boolean isValidIPv4Address(String ipAddress) {
+ if (StringUtils.isEmpty(ipAddress)) {
+ return false;
+ }
+ return InetAddressUtils.isIPv4Address(ipAddress);
+ }
+
+ public static boolean isValidIPv6Address(String ipAddress) {
+ if (StringUtils.isEmpty(ipAddress)) {
+ return false;
+ }
+ return InetAddressUtils.isIPv6Address(ipAddress);
+ }
+
+ public static boolean isValidPort(String port) {
+ if (StringUtils.isEmpty(port)) {
+ return false;
+ }
+ String portRegex = "^([1-9]|[1-9]\\d{1,4}|[1-6][0-5][0-5][0-3][0-5])$";
Review Comment:
use constant
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]