Search in sources :

Example 1 with NatManager

use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.

the class NatService method queryExternalIPAddress.

/**
 * Returns a {@link Optional} wrapping the advertised IP address.
 *
 * @param fallbackValue the advertised IP address fallback value
 * @return The advertised IP address wrapped in a {@link Optional}. Empty if
 *     `isNatExternalIpUsageEnabled` is false
 */
public String queryExternalIPAddress(final String fallbackValue) {
    if (isNatEnvironment()) {
        try {
            final NatManager natManager = getNatManager().orElseThrow();
            LOG.debug("Waiting for up to {} seconds to detect external IP address...", NatManager.TIMEOUT_SECONDS);
            return Optional.ofNullable(natManager.queryExternalIPAddress().get(NatManager.TIMEOUT_SECONDS, TimeUnit.SECONDS)).orElseThrow();
        } catch (Exception e) {
            LOG.debug("Caught exception while trying to query NAT external IP address (ignoring)", e);
            LOG.warn("Unable to query NAT external IP address. Using the fallback value : {} ", fallbackValue);
        }
    }
    return fallbackValue;
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager)

Example 2 with NatManager

use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.

the class NatService method queryLocalIPAddress.

/**
 * Returns a {@link Optional} wrapping the local IP address.
 *
 * @param fallbackValue the advertised IP address fallback value
 * @return The local IP address wrapped in a {@link Optional}.
 */
public String queryLocalIPAddress(final String fallbackValue) throws RuntimeException {
    if (isNatEnvironment()) {
        try {
            final NatManager natManager = getNatManager().orElseThrow();
            LOG.debug("Waiting for up to {} seconds to detect local IP address...", NatManager.TIMEOUT_SECONDS);
            return Optional.ofNullable(natManager.queryLocalIPAddress().get(NatManager.TIMEOUT_SECONDS, TimeUnit.SECONDS)).orElseThrow();
        } catch (Exception e) {
            LOG.debug("Caught exception while trying to query NAT local IP address (ignoring)", e);
            LOG.warn("Unable to query NAT local IP address. Using the fallback value : {} ", fallbackValue);
        }
    }
    return fallbackValue;
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager)

Example 3 with NatManager

use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.

the class NatServiceTest method assertQueryExternalIpWorksProperlyWithUpNpP2pOnly.

@Test
public void assertQueryExternalIpWorksProperlyWithUpNpP2pOnly() {
    final String fallbackExternalIp = "127.0.0.1";
    final String externalIp = "127.0.0.3";
    final NatManager natManager = mock(NatManager.class);
    when(natManager.queryExternalIPAddress()).thenReturn(CompletableFuture.completedFuture(externalIp));
    when(natManager.getNatMethod()).thenReturn(NatMethod.UPNPP2PONLY);
    final NatService natService = new NatService(Optional.of(natManager), true);
    final String resultIp = natService.queryExternalIPAddress(fallbackExternalIp);
    verify(natManager).queryExternalIPAddress();
    assertThat(resultIp).isEqualTo(externalIp);
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 4 with NatManager

use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.

the class NatServiceTest method assertThatQueryLocalIPAddressWorksProperlyWithUpNp.

@Test
public void assertThatQueryLocalIPAddressWorksProperlyWithUpNp() {
    final String fallbackExternalIp = "127.0.0.1";
    final String externalIp = "127.0.0.3";
    final NatManager natManager = mock(NatManager.class);
    when(natManager.queryLocalIPAddress()).thenReturn(CompletableFuture.completedFuture(externalIp));
    when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP);
    final NatService natService = new NatService(Optional.of(natManager), true);
    final String resultIp = natService.queryLocalIPAddress(fallbackExternalIp);
    verify(natManager).queryLocalIPAddress();
    assertThat(resultIp).isEqualTo(externalIp);
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 5 with NatManager

use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.

the class NatServiceTest method assertThatManagerSwitchToNoneForInvalidNatEnvironment.

@Test
public void assertThatManagerSwitchToNoneForInvalidNatEnvironment() throws NatInitializationException {
    final String externalIp = "1.2.3.4";
    final String localIp = "2.2.3.4";
    final String fallbackExternalIp = "3.4.5.6";
    final String fallbackLocalIp = "4.4.5.6";
    final NatManager natManager = mock(NatManager.class);
    doThrow(NatInitializationException.class).when(natManager).start();
    when(natManager.queryExternalIPAddress()).thenReturn(CompletableFuture.completedFuture(externalIp));
    when(natManager.queryLocalIPAddress()).thenReturn(CompletableFuture.completedFuture(localIp));
    when(natManager.getPortMapping(any(NatServiceType.class), any(NetworkProtocol.class))).thenReturn(new NatPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP, localIp, externalIp, 1111, 1111));
    when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP);
    final NatService natService = new NatService(Optional.of(natManager), true);
    assertThat(natService.getNatMethod()).isEqualTo(NatMethod.UPNP);
    assertThat(natService.isNatEnvironment()).isTrue();
    assertThat(natService.getNatManager()).contains(natManager);
    assertThat(natService.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP)).isPresent();
    assertThat(natService.queryExternalIPAddress(fallbackExternalIp)).isEqualTo(externalIp);
    assertThat(natService.queryLocalIPAddress(fallbackLocalIp)).isEqualTo(localIp);
    natService.start();
    assertThat(natService.getNatMethod()).isEqualTo(NatMethod.NONE);
    assertThat(natService.isNatEnvironment()).isFalse();
    assertThat(natService.getNatManager()).isNotPresent();
    assertThat(natService.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP)).isNotPresent();
    assertThat(natService.queryExternalIPAddress(fallbackExternalIp)).isEqualTo(fallbackExternalIp);
    assertThat(natService.queryLocalIPAddress(fallbackLocalIp)).isEqualTo(fallbackLocalIp);
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) NetworkProtocol(org.hyperledger.besu.nat.core.domain.NetworkProtocol) NatServiceType(org.hyperledger.besu.nat.core.domain.NatServiceType) Test(org.junit.Test)

Aggregations

NatManager (org.hyperledger.besu.nat.core.NatManager)10 UpnpNatManager (org.hyperledger.besu.nat.upnp.UpnpNatManager)7 Test (org.junit.Test)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Vertx (io.vertx.core.Vertx)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional (java.util.Optional)1 OptionalLong (java.util.OptionalLong)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1