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