Search in sources :

Example 1 with UpnpNatManager

use of org.hyperledger.besu.nat.upnp.UpnpNatManager in project besu by hyperledger.

the class DefaultP2PNetworkTest method start_withNatManagerUpnpP2p.

@Test
public void start_withNatManagerUpnpP2p() {
    final String externalIp = "127.0.0.3";
    config.getRlpx().setBindPort(30303);
    config.getDiscovery().setBindPort(30301);
    final UpnpNatManager upnpNatManager = mock(UpnpNatManager.class);
    when(upnpNatManager.getNatMethod()).thenReturn(NatMethod.UPNPP2PONLY);
    when(upnpNatManager.queryExternalIPAddress()).thenReturn(CompletableFuture.completedFuture(externalIp));
    final NatService natService = spy(new NatService(Optional.of(upnpNatManager)));
    final P2PNetwork network = builder().natService(natService).build();
    network.start();
    verify(upnpNatManager).requestPortForward(eq(config.getRlpx().getBindPort()), eq(NetworkProtocol.TCP), any());
    verify(upnpNatManager).requestPortForward(eq(config.getDiscovery().getBindPort()), eq(NetworkProtocol.UDP), any());
    Assertions.assertThat(network.getLocalEnode().get().getIpAsString()).isEqualTo(externalIp);
}
Also used : NatService(org.hyperledger.besu.nat.NatService) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 2 with UpnpNatManager

use of org.hyperledger.besu.nat.upnp.UpnpNatManager in project besu by hyperledger.

the class DefaultP2PNetworkTest method start_withNatManager.

@Test
public void start_withNatManager() {
    final String externalIp = "127.0.0.3";
    config.getRlpx().setBindPort(30303);
    config.getDiscovery().setBindPort(30301);
    final UpnpNatManager upnpNatManager = mock(UpnpNatManager.class);
    when(upnpNatManager.getNatMethod()).thenReturn(NatMethod.UPNP);
    when(upnpNatManager.queryExternalIPAddress()).thenReturn(CompletableFuture.completedFuture(externalIp));
    final NatService natService = spy(new NatService(Optional.of(upnpNatManager)));
    final P2PNetwork network = builder().natService(natService).build();
    network.start();
    verify(upnpNatManager).requestPortForward(eq(config.getRlpx().getBindPort()), eq(NetworkProtocol.TCP), any());
    verify(upnpNatManager).requestPortForward(eq(config.getDiscovery().getBindPort()), eq(NetworkProtocol.UDP), any());
    Assertions.assertThat(network.getLocalEnode().get().getIpAsString()).isEqualTo(externalIp);
}
Also used : NatService(org.hyperledger.besu.nat.NatService) UpnpNatManager(org.hyperledger.besu.nat.upnp.UpnpNatManager) Test(org.junit.Test)

Example 3 with UpnpNatManager

use of org.hyperledger.besu.nat.upnp.UpnpNatManager 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)

Aggregations

NatService (org.hyperledger.besu.nat.NatService)2 UpnpNatManager (org.hyperledger.besu.nat.upnp.UpnpNatManager)2 Test (org.junit.Test)2 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