Search in sources :

Example 6 with NatManager

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);
}
Also used : NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException) NatManager(org.hyperledger.besu.nat.core.NatManager) Test(org.junit.Test)

Example 7 with NatManager

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);
}
Also used : VertxPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.VertxPeerDiscoveryAgent) EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) Arrays(java.util.Arrays) NetworkProtocol(org.hyperledger.besu.nat.core.domain.NetworkProtocol) LoggerFactory(org.slf4j.LoggerFactory) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) PeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryAgent) StorageProvider(org.hyperledger.besu.ethereum.storage.StorageProvider) Duration(java.time.Duration) DefaultPeerPrivileges(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeerPrivileges) NetworkingConfiguration(org.hyperledger.besu.ethereum.p2p.config.NetworkingConfiguration) Collection(java.util.Collection) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) NatServiceType(org.hyperledger.besu.nat.core.domain.NatServiceType) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) DNSDaemon(org.apache.tuweni.discovery.DNSDaemon) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Optional(java.util.Optional) MutableLocalNode(org.hyperledger.besu.ethereum.p2p.peers.MutableLocalNode) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) NodeKey(org.hyperledger.besu.crypto.NodeKey) NatManager(org.hyperledger.besu.nat.core.NatManager) MessageCallback(org.hyperledger.besu.ethereum.p2p.rlpx.MessageCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Util(org.hyperledger.besu.ethereum.core.Util) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) TLSConfiguration(org.hyperledger.besu.ethereum.p2p.rlpx.connections.netty.TLSConfiguration) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) RlpxAgent(org.hyperledger.besu.ethereum.p2p.rlpx.RlpxAgent) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PeerBondedEvent(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryEvent.PeerBondedEvent) MaintainedPeers(org.hyperledger.besu.ethereum.p2p.peers.MaintainedPeers) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) DNSDaemonListener(org.apache.tuweni.discovery.DNSDaemonListener) PeerDiscoveryStatus(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryStatus) ConnectCallback(org.hyperledger.besu.ethereum.p2p.rlpx.ConnectCallback) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Vertx(io.vertx.core.Vertx) NatMethod(org.hyperledger.besu.nat.NatMethod) DisconnectCallback(org.hyperledger.besu.ethereum.p2p.rlpx.DisconnectCallback) PeerPrivileges(org.hyperledger.besu.ethereum.p2p.peers.PeerPrivileges) EthereumNodeRecord(org.apache.tuweni.devp2p.EthereumNodeRecord) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) LocalNode(org.hyperledger.besu.ethereum.p2p.peers.LocalNode) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) NatService(org.hyperledger.besu.nat.NatService) DNSDaemon(org.apache.tuweni.discovery.DNSDaemon) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager)

Example 8 with NatManager

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);
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 9 with NatManager

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);
}
Also used : NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 10 with NatManager

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);
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) NatManager(org.hyperledger.besu.nat.core.NatManager) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) 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