use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.
the class DockerNatManagerTest method assertThatExternalIPIsEqualToDefaultHostIfIpDetectorCannotRetrieveIP.
@Test
public void assertThatExternalIPIsEqualToDefaultHostIfIpDetectorCannotRetrieveIP() throws ExecutionException, InterruptedException {
final NatManager natManager = new DockerNatManager(hostBasedIpDetector, advertisedHost, p2pPort, rpcHttpPort);
when(hostBasedIpDetector.detectAdvertisedIp()).thenReturn(Optional.empty());
try {
natManager.start();
} catch (NatInitializationException e) {
Assertions.fail(e.getMessage());
}
assertThat(natManager.queryExternalIPAddress().get()).isEqualTo(advertisedHost);
}
use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.
the class DefaultP2PNetwork method start.
@Override
public void start() {
if (!started.compareAndSet(false, true)) {
LOG.warn("Attempted to start an already started " + getClass().getSimpleName());
return;
}
final String address = config.getDiscovery().getAdvertisedHost();
final int configuredDiscoveryPort = config.getDiscovery().getBindPort();
final int configuredRlpxPort = config.getRlpx().getBindPort();
Optional.ofNullable(config.getDiscovery().getDNSDiscoveryURL()).ifPresent(disco -> {
LOG.info("Starting DNS discovery with URL {}", disco);
config.getDnsDiscoveryServerOverride().ifPresent(dnsServer -> LOG.info("Starting DNS discovery with DNS Server override {}", dnsServer));
dnsDaemon = new DNSDaemon(disco, createDaemonListener(), 0L, 60000L, config.getDnsDiscoveryServerOverride().orElse(null));
dnsDaemon.start();
});
final int listeningPort = rlpxAgent.start().join();
final int discoveryPort = peerDiscoveryAgent.start((configuredDiscoveryPort == 0 && configuredRlpxPort == 0) ? listeningPort : configuredDiscoveryPort).join();
final Consumer<? super NatManager> natAction = natManager -> {
final UpnpNatManager upnpNatManager = (UpnpNatManager) natManager;
upnpNatManager.requestPortForward(discoveryPort, NetworkProtocol.UDP, NatServiceType.DISCOVERY);
upnpNatManager.requestPortForward(listeningPort, NetworkProtocol.TCP, NatServiceType.RLPX);
};
natService.ifNatEnvironment(NatMethod.UPNP, natAction);
natService.ifNatEnvironment(NatMethod.UPNPP2PONLY, natAction);
setLocalNode(address, listeningPort, discoveryPort);
peerBondedObserverId = OptionalLong.of(peerDiscoveryAgent.observePeerBondedEvents(this::handlePeerBondedEvent));
// Periodically check maintained connections
final int checkMaintainedConnectionsSec = config.getCheckMaintainedConnectionsFrequencySec();
peerConnectionScheduler.scheduleWithFixedDelay(this::checkMaintainedConnectionPeers, 2, checkMaintainedConnectionsSec, TimeUnit.SECONDS);
// Periodically initiate outgoing connections to discovered peers
final int checkConnectionsSec = config.getInitiateConnectionsFrequencySec();
peerConnectionScheduler.scheduleWithFixedDelay(this::attemptPeerConnections, checkConnectionsSec, checkConnectionsSec, TimeUnit.SECONDS);
}
use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.
the class NatServiceTest method assertQueryExternalIpWorksProperlyWithUpNp.
@Test
public void assertQueryExternalIpWorksProperlyWithUpNp() {
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.UPNP);
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 assertThatManagerSwitchToNoneForInvalidNatEnvironmentIfFallbackDisabled.
@Test
public void assertThatManagerSwitchToNoneForInvalidNatEnvironmentIfFallbackDisabled() throws NatInitializationException {
final NatManager natManager = mock(NatManager.class);
doThrow(NatInitializationException.class).when(natManager).start();
when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP);
final NatService natService = new NatService(Optional.of(natManager), false);
assertThat(natService.getNatMethod()).isEqualTo(NatMethod.UPNP);
assertThat(natService.isNatEnvironment()).isTrue();
assertThat(natService.getNatManager()).contains(natManager);
assertThatThrownBy(natService::start);
}
use of org.hyperledger.besu.nat.core.NatManager in project besu by hyperledger.
the class NatServiceTest method assertThatGetPortMappingWorksProperlyWithUpNp.
@Test
public void assertThatGetPortMappingWorksProperlyWithUpNp() {
final String externalIp = "127.0.0.3";
final NatPortMapping natPortMapping = new NatPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP, externalIp, externalIp, 1111, 1111);
final NatManager natManager = mock(NatManager.class);
when(natManager.getPortMapping(natPortMapping.getNatServiceType(), natPortMapping.getProtocol())).thenReturn(natPortMapping);
when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP);
final NatService natService = new NatService(Optional.of(natManager), true);
final Optional<NatPortMapping> portMapping = natService.getPortMapping(natPortMapping.getNatServiceType(), natPortMapping.getProtocol());
verify(natManager).getPortMapping(natPortMapping.getNatServiceType(), natPortMapping.getProtocol());
assertThat(portMapping).contains(natPortMapping);
}
Aggregations