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