Search in sources :

Example 1 with PortMappingAdd

use of org.jupnp.support.igd.callback.PortMappingAdd in project besu by hyperledger.

the class UpnpNatManager method requestPortForward.

/**
 * Sends a UPnP request to the discovered IGD to request a port forward.
 *
 * @param portMapping is a portMapping object describing the desired port mapping parameters.
 * @return A CompletableFuture that can be used to query the result (or error).
 */
private CompletableFuture<Void> requestPortForward(final PortMapping portMapping) {
    CompletableFuture<Void> upnpQueryFuture = new CompletableFuture<>();
    return externalIpQueryFuture.thenCompose(address -> {
        // note that this future is a dependency of externalIpQueryFuture, so it must be completed
        // by now
        RemoteService service = getService(SERVICE_TYPE_WAN_IP_CONNECTION).join();
        // so we can prime the NewInternalClient field if it was omitted
        if (null == portMapping.getInternalClient()) {
            portMapping.setInternalClient(discoveredOnLocalAddress.get());
        }
        // our query, which will be handled asynchronously by the jupnp library
        PortMappingAdd callback = new PortMappingAdd(service, 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 request for {} {} -> {} succeeded.", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort());
                synchronized (forwardedPorts) {
                    final NatServiceType natServiceType = NatServiceType.fromString(portMapping.getDescription());
                    final NatPortMapping natPortMapping = new NatPortMapping(natServiceType, NetworkProtocol.valueOf(portMapping.getProtocol().name()), portMapping.getInternalClient(), portMapping.getRemoteHost(), portMapping.getExternalPort().getValue().intValue(), portMapping.getInternalPort().getValue().intValue());
                    forwardedPorts.add(natPortMapping);
                }
                upnpQueryFuture.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 request for {} {} -> {} failed: {}", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort(), msg);
                upnpQueryFuture.completeExceptionally(new Exception(msg));
            }
        };
        LOG.info("Requesting port forward for {} {} -> {}", portMapping.getProtocol(), portMapping.getInternalPort(), portMapping.getExternalPort());
        upnpService.getControlPoint().execute(callback);
        return upnpQueryFuture;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) NatPortMapping(org.hyperledger.besu.nat.core.domain.NatPortMapping) RemoteService(org.jupnp.model.meta.RemoteService) UpnpResponse(org.jupnp.model.message.UpnpResponse) ActionInvocation(org.jupnp.model.action.ActionInvocation) PortMappingAdd(org.jupnp.support.igd.callback.PortMappingAdd) NatServiceType(org.hyperledger.besu.nat.core.domain.NatServiceType) NatInitializationException(org.hyperledger.besu.nat.core.exception.NatInitializationException)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 NatPortMapping (org.hyperledger.besu.nat.core.domain.NatPortMapping)1 NatServiceType (org.hyperledger.besu.nat.core.domain.NatServiceType)1 NatInitializationException (org.hyperledger.besu.nat.core.exception.NatInitializationException)1 ActionInvocation (org.jupnp.model.action.ActionInvocation)1 UpnpResponse (org.jupnp.model.message.UpnpResponse)1 RemoteService (org.jupnp.model.meta.RemoteService)1 PortMappingAdd (org.jupnp.support.igd.callback.PortMappingAdd)1