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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations