Search in sources :

Example 1 with NatPortMapping

use of org.hyperledger.besu.nat.core.domain.NatPortMapping in project besu by hyperledger.

the class UpnpNatManager method releaseAllPortForwards.

/**
 * Attempts to release any forwarded ports.
 *
 * <p>Note that this is not synchronized, as it is expected to be called within an
 * already-synchronized context ({@link #stop()}).
 *
 * @return A CompletableFuture that will complete when all port forward requests have been made
 */
private CompletableFuture<Void> releaseAllPortForwards() {
    // if we haven't observed the WANIPConnection service yet, we should have no port forwards to
    // release
    CompletableFuture<RemoteService> wanIPConnectionServiceFuture = getService(SERVICE_TYPE_WAN_IP_CONNECTION);
    if (!wanIPConnectionServiceFuture.isDone()) {
        return CompletableFuture.completedFuture(null);
    }
    RemoteService service = wanIPConnectionServiceFuture.join();
    List<CompletableFuture<Void>> futures = new ArrayList<>();
    boolean done = false;
    while (!done) {
        NatPortMapping portMapping;
        synchronized (forwardedPorts) {
            if (forwardedPorts.isEmpty()) {
                done = true;
                continue;
            }
            portMapping = forwardedPorts.get(0);
            forwardedPorts.remove(0);
        }
        LOG.info("Releasing port forward for {} {} -> {}", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort());
        CompletableFuture<Void> future = new CompletableFuture<>();
        PortMappingDelete callback = new PortMappingDelete(service, toJupnpPortMapping(portMapping)) {

            /**
             * Because the underlying jupnp library omits generics info in this method signature, we
             * must too when we override it.
             */
            @Override
            @SuppressWarnings("rawtypes")
            public void success(final ActionInvocation invocation) {
                LOG.info("Port forward {} {} -> {} removed successfully.", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort());
                future.complete(null);
            }

            /**
             * Because the underlying jupnp library omits generics info in this method signature, we
             * must too when we override it.
             */
            @Override
            @SuppressWarnings("rawtypes")
            public void failure(final ActionInvocation invocation, final UpnpResponse operation, final String msg) {
                LOG.warn("Port forward removal request for {} {} -> {} failed (ignoring): {}", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort(), msg);
                // ignore exceptions; we did our best
                future.complete(null);
            }
        };
        upnpService.getControlPoint().execute(callback);
        futures.add(future);
    }
    // complete
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[0]));
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) UpnpResponse(org.jupnp.model.message.UpnpResponse) ActionInvocation(org.jupnp.model.action.ActionInvocation) ArrayList(java.util.ArrayList) PortMappingDelete(org.jupnp.support.igd.callback.PortMappingDelete) CompletableFuture(java.util.concurrent.CompletableFuture) RemoteService(org.jupnp.model.meta.RemoteService)

Example 2 with NatPortMapping

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

Example 3 with NatPortMapping

use of org.hyperledger.besu.nat.core.domain.NatPortMapping in project besu by hyperledger.

the class DockerNatManagerTest method assertThatMappingForJsonRpcWorks.

@Test
public void assertThatMappingForJsonRpcWorks() throws UnknownHostException {
    final String internalHost = InetAddress.getLocalHost().getHostAddress();
    final NatPortMapping mapping = natManager.getPortMapping(NatServiceType.JSON_RPC, NetworkProtocol.TCP);
    final NatPortMapping expectedMapping = new NatPortMapping(NatServiceType.JSON_RPC, NetworkProtocol.TCP, internalHost, detectedAdvertisedHost, rpcHttpPort, rpcHttpPort);
    assertThat(mapping).usingRecursiveComparison().isEqualTo(expectedMapping);
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) Test(org.junit.Test)

Example 4 with NatPortMapping

use of org.hyperledger.besu.nat.core.domain.NatPortMapping in project besu by hyperledger.

the class KubernetesClusterIpNatManagerTest method assertThatMappingForRlpxWorks.

@Test
public void assertThatMappingForRlpxWorks() throws UnknownHostException {
    final String internalHost = InetAddress.getLocalHost().getHostAddress();
    final NatPortMapping mapping = natManager.getPortMapping(NatServiceType.RLPX, NetworkProtocol.TCP);
    final NatPortMapping expectedMapping = new NatPortMapping(NatServiceType.RLPX, NetworkProtocol.TCP, internalHost, detectedAdvertisedHost, p2pPort, p2pPort);
    assertThat(mapping).usingRecursiveComparison().isEqualTo(expectedMapping);
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) IntOrString(io.kubernetes.client.custom.IntOrString) Test(org.junit.Test)

Example 5 with NatPortMapping

use of org.hyperledger.besu.nat.core.domain.NatPortMapping in project besu by hyperledger.

the class KubernetesClusterIpNatManagerTest method assertThatMappingForJsonRpcWorks.

@Test
public void assertThatMappingForJsonRpcWorks() throws UnknownHostException {
    final String internalHost = InetAddress.getLocalHost().getHostAddress();
    final NatPortMapping mapping = natManager.getPortMapping(NatServiceType.JSON_RPC, NetworkProtocol.TCP);
    final NatPortMapping expectedMapping = new NatPortMapping(NatServiceType.JSON_RPC, NetworkProtocol.TCP, internalHost, detectedAdvertisedHost, rpcHttpPort, rpcHttpPort);
    assertThat(mapping).usingRecursiveComparison().isEqualTo(expectedMapping);
}
Also used : NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) IntOrString(io.kubernetes.client.custom.IntOrString) Test(org.junit.Test)

Aggregations

NatPortMapping (org.hyperledger.besu.nat.core.domain.NatPortMapping)16 Test (org.junit.Test)12 IntOrString (io.kubernetes.client.custom.IntOrString)6 NatServiceType (org.hyperledger.besu.nat.core.domain.NatServiceType)3 NatInitializationException (org.hyperledger.besu.nat.core.exception.NatInitializationException)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 NatManager (org.hyperledger.besu.nat.core.NatManager)2 UpnpNatManager (org.hyperledger.besu.nat.upnp.UpnpNatManager)2 ActionInvocation (org.jupnp.model.action.ActionInvocation)2 UpnpResponse (org.jupnp.model.message.UpnpResponse)2 RemoteService (org.jupnp.model.meta.RemoteService)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1 NetworkProtocol (org.hyperledger.besu.nat.core.domain.NetworkProtocol)1 PortMappingAdd (org.jupnp.support.igd.callback.PortMappingAdd)1 PortMappingDelete (org.jupnp.support.igd.callback.PortMappingDelete)1