diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/local/Peer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/local/Peer.java index 34e52592a917a76d7789b652f81f36ccfd005f53..bb1e7d7bcfb37741415824438c47e1f1b8d8f9c6 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/local/Peer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/local/Peer.java @@ -311,7 +311,7 @@ public class Peer implements IEntity<String> { // else (if define) use dns // else (if define) use ipv6 host = ((port == 443 || useSsl) && dns != null) ? dns : - (ipv4 != null && InetAddressUtils.isNotLocalIPv4Address(ipv4) ? ipv4 : + (ipv4 != null && InetAddressUtils.isInternetIPv4Address(ipv4) ? ipv4 : (dns != null ? dns : (ipv6 != null ? "[" + ipv6 + "]" : ""))); // Use local IPv4 if no other host found diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/ServiceLocator.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/ServiceLocator.java index c4d2c22db26f9785570218c8b68803a669d7d840..79258e8e985e401e4ce7edccf89f32395f1b85de 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/ServiceLocator.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/ServiceLocator.java @@ -23,6 +23,7 @@ package org.duniter.core.client.service; */ +import lombok.extern.slf4j.Slf4j; import org.duniter.core.beans.Bean; import org.duniter.core.beans.BeanFactory; import org.duniter.core.client.repositories.PeerRepository; @@ -43,12 +44,10 @@ import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; +@Slf4j public class ServiceLocator implements Closeable { - /* Logger */ - private static final Logger log = LoggerFactory.getLogger(ServiceLocator.class); - /** * The shared instance of this ServiceLocator. */ diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java index bbc61e7cfafe3920b85ae39344bc7ef971d23edd..dee88a0a8646c8524267961936f95575e7602109 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/local/NetworkServiceImpl.java @@ -179,8 +179,8 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network return peer; }) - // Exclude peer with only a local address - .filter(peer -> InetAddressUtils.isNotLocalAddress(peer.getHost())) + // Exclude peer on intranet (not routable) addresses + .filter(peer -> InetAddressUtils.isInternetAddress(peer.getHost())) .collect(Collectors.toList()) ) .thenCompose(peers -> this.refreshPeersAsync(mainPeer, peers, pool)); diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/TestResource.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/TestResource.java index a237cb1f7d2931f8002d64edb3ecfe3818394f9a..f3d266dd8cc7242ce4b6afd40b8ddac6376e6a32 100644 --- a/duniter4j-core-client/src/test/java/org/duniter/core/client/TestResource.java +++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/TestResource.java @@ -24,6 +24,7 @@ package org.duniter.core.client; import com.google.common.collect.Lists; +import lombok.extern.slf4j.Slf4j; import org.duniter.core.client.config.Configuration; import org.duniter.core.client.config.ConfigurationOption; import org.duniter.core.client.model.local.Peer; @@ -41,9 +42,9 @@ import java.io.IOException; import java.util.List; import java.util.Locale; +@Slf4j public class TestResource extends org.duniter.core.test.TestResource { - private static final Logger log = LoggerFactory.getLogger(TestResource.class); public static TestResource create() { return new TestResource(null); @@ -102,7 +103,7 @@ public class TestResource extends org.duniter.core.test.TestResource { /* -- -- */ /** - * Convenience methods that could be override to initialize other configuration + * Convenience methods that could be overridden to initialize other configuration * * @param configFilename */ diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/TransactionRemoteServiceTest.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/TransactionRemoteServiceTest.java index 361e782f5202ff664b8d4fc6a7c729f707a4811d..cea458e544cabae658823a9b4bdd241c801d6f77 100644 --- a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/TransactionRemoteServiceTest.java +++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/TransactionRemoteServiceTest.java @@ -24,7 +24,6 @@ package org.duniter.core.client.service.bma; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.duniter.core.client.TestResource; import org.duniter.core.client.config.Configuration; @@ -37,8 +36,6 @@ import org.duniter.core.client.service.ServiceLocator; import org.duniter.core.client.service.exception.InsufficientCreditException; import org.duniter.core.util.crypto.CryptoUtils; import org.junit.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.Map; import java.util.Objects; @@ -51,7 +48,7 @@ public class TransactionRemoteServiceTest { private TransactionRemoteService service; - private int unitbase; + private int unitBase = 0; @Before public void setUp() { @@ -66,7 +63,7 @@ public class TransactionRemoteServiceTest { BlockchainBlock currentBlock = ServiceLocator.instance().getBlockchainRemoteService() .getCurrentBlock(resource.getFixtures().getDefaultCurrency()); Assume.assumeNotNull(currentBlock); - this.unitbase = currentBlock.getUnitbase(); + this.unitBase = currentBlock.getUnitbase(); } @Test @@ -76,7 +73,7 @@ public class TransactionRemoteServiceTest { service.transfer( createTestWallet(), resource.getFixtures().getOtherUserPublicKey(0), - BlockchainBlocks.powBase(1, unitbase), + BlockchainBlocks.powBase(1, unitBase), "Unit test Duniter4j at " + System.currentTimeMillis()); } catch (InsufficientCreditException e) { Assume.assumeNoException(String.format("No credit on the test wallet '%s'", resource.getFixtures().getUserPublicKey().substring(0,8)), e); @@ -87,8 +84,8 @@ public class TransactionRemoteServiceTest { public void transferMulti() throws Exception { Map<String, Long> destPubkeyAmount = ImmutableMap.<String, Long>builder() - .put(resource.getFixtures().getOtherUserPublicKey(0), BlockchainBlocks.powBase(1, unitbase)) - .put(resource.getFixtures().getOtherUserPublicKey(1), BlockchainBlocks.powBase(2, unitbase)) + .put(resource.getFixtures().getOtherUserPublicKey(0), BlockchainBlocks.powBase(1, unitBase)) + .put(resource.getFixtures().getOtherUserPublicKey(1), BlockchainBlocks.powBase(2, unitBase)) .build(); try { diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java index 184eb8d52f0e00702dcd401ebfdcd13065c64027..1a31e0cc75fd1c76d3ddd57338e59b64da384d48 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/http/InetAddressUtils.java @@ -29,19 +29,11 @@ import java.util.regex.Pattern; */ public class InetAddressUtils { - public static final Pattern LOCAL_IP_ADDRESS_PATTERN = Pattern.compile("^127[.]0[.]0.|192[.]168[.]|10[.]0[.]0[.]|172[.]16[.]"); + public static final Pattern LOCAL_IP_ADDRESS_PATTERN = Pattern.compile("^127[.]0[.]0."); - private InetAddressUtils() { - } - - public static boolean isNotLocalIPv4Address(String input) { - return org.apache.http.conn.util.InetAddressUtils.isIPv4Address(input) && - !LOCAL_IP_ADDRESS_PATTERN.matcher(input).find(); - } + public static final Pattern INTRANET_ADDRESS_PATTERN = Pattern.compile("^127[.]0[.]0.|192[.]168[.]|10[.]0[.]0[.]|172[.]16[.]"); - public static boolean isLocalIPv4Address(String input) { - return org.apache.http.conn.util.InetAddressUtils.isIPv4Address(input) && - LOCAL_IP_ADDRESS_PATTERN.matcher(input).find(); + private InetAddressUtils() { } public static boolean isIPv4Address(String input) { @@ -52,11 +44,29 @@ public class InetAddressUtils { return org.apache.http.conn.util.InetAddressUtils.isIPv6Address(input); } + public static boolean isInternetIPv4Address(String input) { + return isIPv4Address(input) && + !INTRANET_ADDRESS_PATTERN.matcher(input).find(); + } + + public static boolean isIntranetIPv4Address(String input) { + return isIPv4Address(input) && INTRANET_ADDRESS_PATTERN.matcher(input).find(); + } + + + public static boolean isIntranetAddress(String input) { + return isIntranetIPv4Address(input) || "localhost".equalsIgnoreCase(input); + } + + public static boolean isInternetAddress(String input) { + return isIPv6Address(input) || isInternetIPv4Address(input); + } + public static boolean isLocalAddress(String input) { - return isLocalIPv4Address(input) || "localhost".equalsIgnoreCase(input); + return (isIPv4Address(input) && LOCAL_IP_ADDRESS_PATTERN.matcher(input).find()) || "localhost".equalsIgnoreCase(input); } public static boolean isNotLocalAddress(String input) { - return !isLocalAddress(input); + return isIPv6Address(input) || (isIPv4Address(input) && !LOCAL_IP_ADDRESS_PATTERN.matcher(input).find()); } } 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 4453e9917ece32de735bb8a99189987d9e6a5253..0ede067e330aaffea70a5f2212ba1223e5b2cc71 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 @@ -30,29 +30,80 @@ import org.junit.Test; */ public class InetAddressUtilsTest { + @Test + public void isIPv4Address() { + + boolean check = InetAddressUtils.isIPv4Address("192.168.0.254"); + Assert.assertTrue(check); + } + @Test public void isNotLocalIPv4Address() { - Assert.assertFalse(InetAddressUtils.isNotLocalIPv4Address("192.168.1.11")); - Assert.assertFalse(InetAddressUtils.isNotLocalIPv4Address("abc")); + Assert.assertFalse(InetAddressUtils.isInternetIPv4Address("192.168.1.11")); + Assert.assertFalse(InetAddressUtils.isInternetIPv4Address("abc")); - Assert.assertTrue(InetAddressUtils.isNotLocalIPv4Address("82.239.120.237")); + Assert.assertTrue(InetAddressUtils.isInternetIPv4Address("82.239.120.237")); } @Test public void isLocalIPv4Address() { - boolean check = InetAddressUtils.isLocalIPv4Address("192.168.1.11"); + boolean check = InetAddressUtils.isIntranetIPv4Address("192.168.1.11"); Assert.assertTrue(check); - check = InetAddressUtils.isLocalIPv4Address("127.0.0.1"); + check = InetAddressUtils.isIntranetIPv4Address("127.0.0.1"); Assert.assertTrue(check); } @Test public void isLocalAddress() { - boolean check = InetAddressUtils.isLocalAddress("localhost"); - Assert.assertTrue(check); + boolean checkTrue = InetAddressUtils.isLocalAddress("127.0.0.1"); + Assert.assertTrue(checkTrue); + + checkTrue = InetAddressUtils.isLocalAddress("localhost"); + Assert.assertTrue(checkTrue); + + boolean checkFalse = InetAddressUtils.isLocalAddress("192.168.0.254"); + Assert.assertFalse(checkFalse); + + checkFalse = InetAddressUtils.isLocalAddress("10.0.0.1"); + Assert.assertFalse(checkFalse); + } + + @Test + public void isIntranetAddress() { + + boolean checkTrue = InetAddressUtils.isIntranetAddress("127.0.0.1"); + Assert.assertTrue(checkTrue); + + checkTrue = InetAddressUtils.isIntranetAddress("10.0.0.1"); + Assert.assertTrue(checkTrue); + + checkTrue = InetAddressUtils.isIntranetAddress("localhost"); + Assert.assertTrue(checkTrue); + + checkTrue = InetAddressUtils.isIntranetAddress("192.168.0.254"); + Assert.assertTrue(checkTrue); + } + + @Test + public void isInternetAddress() { + + boolean checkTrue = InetAddressUtils.isInternetAddress("88.168.0.254"); + Assert.assertTrue(checkTrue); + + boolean checkFalse = InetAddressUtils.isInternetAddress("127.0.0.1"); + Assert.assertFalse(checkFalse); + + checkFalse = InetAddressUtils.isInternetAddress("10.0.0.1"); + Assert.assertFalse(checkFalse); + + checkFalse = InetAddressUtils.isInternetAddress("localhost"); + Assert.assertFalse(checkFalse); + + checkFalse = InetAddressUtils.isInternetAddress("192.168.0.254"); + Assert.assertFalse(checkFalse); } }