From 4f0b5590c4efe8f62a65df9c9ddd53a7ade8a7b6 Mon Sep 17 00:00:00 2001 From: Benoit Lavenier <benoit.lavenier@e-is.pro> Date: Fri, 4 Aug 2023 14:06:26 +0200 Subject: [PATCH] enh: Add DnsUtils to manage hostname --- .../org/duniter/core/util/http/DnsUtils.java | 54 +++++++++++++++++ .../duniter/core/util/http/DnsUtilsTest.java | 60 +++++++++++++++++++ .../core/util/http/InetAddressUtilsTest.java | 32 ++++++++-- 3 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 duniter4j-core-shared/src/main/java/org/duniter/core/util/http/DnsUtils.java create mode 100644 duniter4j-core-shared/src/test/java/org/duniter/core/util/http/DnsUtilsTest.java diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/DnsUtils.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/DnsUtils.java new file mode 100644 index 00000000..973f96c8 --- /dev/null +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/DnsUtils.java @@ -0,0 +1,54 @@ +package org.duniter.core.util.http; + +/* + * #%L + * Duniter4j :: Core Shared + * %% + * Copyright (C) 2014 - 2017 EIS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import java.util.regex.Pattern; + +/** + * Created by blavenie on 04/08/23 + */ +public class DnsUtils { + + public static final Pattern HOST_NAME_PATTERN = Pattern.compile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"); + + private DnsUtils() { + } + + public static String normalize(String input) { + return org.apache.http.conn.util.DnsUtils.normalize(input); + } + + public static boolean isHostName(String input) { + return !InetAddressUtils.isIPv4Address(input) + && !InetAddressUtils.isIPv6Address(input) + && HOST_NAME_PATTERN.matcher(input).matches(); + } + + public static boolean isLocalhost(String input) { + return "localhost".equalsIgnoreCase(input); + } + + public static boolean isInternetHostName(String input) { + return isHostName(input) && !isLocalhost(input); + } +} diff --git a/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/DnsUtilsTest.java b/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/DnsUtilsTest.java new file mode 100644 index 00000000..18f6b618 --- /dev/null +++ b/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/DnsUtilsTest.java @@ -0,0 +1,60 @@ +package org.duniter.core.util.http; + +/*- + * #%L + * Duniter4j :: Core Shared + * %% + * Copyright (C) 2014 - 2017 EIS + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by blavenie on 30/03/17. + */ +public class DnsUtilsTest { + + @Test + public void isHostName() { + + boolean checkTrue = DnsUtils.isHostName("g1.duniter.org"); + Assert.assertTrue(checkTrue); + + checkTrue = DnsUtils.isHostName("localhost"); + Assert.assertTrue(checkTrue); + + // Try with an IP + boolean checkFalse = DnsUtils.isHostName("192.168.0.254"); + Assert.assertFalse(checkFalse); + } + + @Test + public void isInternetHostName() { + + boolean checkTrue = DnsUtils.isInternetHostName("g1.duniter.org"); + Assert.assertTrue(checkTrue); + + boolean checkFalse = DnsUtils.isInternetHostName("localhost"); + Assert.assertFalse(checkFalse); + + // Try with an IP + checkFalse = DnsUtils.isInternetHostName("192.168.0.254"); + Assert.assertFalse(checkFalse); + } +} diff --git a/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java b/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java index 0ede067e..62d8a43f 100644 --- a/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java +++ b/duniter4j-core-shared/src/test/java/org/duniter/core/util/http/InetAddressUtilsTest.java @@ -33,27 +33,39 @@ public class InetAddressUtilsTest { @Test public void isIPv4Address() { - boolean check = InetAddressUtils.isIPv4Address("192.168.0.254"); - Assert.assertTrue(check); + boolean checkTrue = InetAddressUtils.isIPv4Address("192.168.0.254"); + Assert.assertTrue(checkTrue); + + // Try a host name + boolean checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } @Test - public void isNotLocalIPv4Address() { + public void isInternetIPv4Address() { Assert.assertFalse(InetAddressUtils.isInternetIPv4Address("192.168.1.11")); Assert.assertFalse(InetAddressUtils.isInternetIPv4Address("abc")); Assert.assertTrue(InetAddressUtils.isInternetIPv4Address("82.239.120.237")); + + // Try a host name + boolean checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } @Test - public void isLocalIPv4Address() { + public void isIntranetIPv4Address() { boolean check = InetAddressUtils.isIntranetIPv4Address("192.168.1.11"); Assert.assertTrue(check); check = InetAddressUtils.isIntranetIPv4Address("127.0.0.1"); Assert.assertTrue(check); + + // Try a host name + boolean checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } @Test @@ -70,6 +82,10 @@ public class InetAddressUtilsTest { checkFalse = InetAddressUtils.isLocalAddress("10.0.0.1"); Assert.assertFalse(checkFalse); + + // Try a host name + checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } @Test @@ -86,6 +102,10 @@ public class InetAddressUtilsTest { checkTrue = InetAddressUtils.isIntranetAddress("192.168.0.254"); Assert.assertTrue(checkTrue); + + // Try a host name + boolean checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } @Test @@ -105,5 +125,9 @@ public class InetAddressUtilsTest { checkFalse = InetAddressUtils.isInternetAddress("192.168.0.254"); Assert.assertFalse(checkFalse); + + // Try a host name + checkFalse = InetAddressUtils.isInternetAddress("g1.duniter.org"); + Assert.assertFalse(checkFalse); } } -- GitLab