mirror of https://github.com/hyperledger/besu
* refactor nat manager Signed-off-by: Karim TAAM <karim.t2am@gmail.com> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * restart tests Signed-off-by: Karim TAAM <karim.t2am@gmail.com> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * restart tests Signed-off-by: Karim TAAM <karim.t2am@gmail.com> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Add Tuweni to Plugin-APIs (#295) Generally, byte[] -> Bytes of some form. Most of the changes are the side effect of the type changes or chaning to the names of Tuweni equivilant calls (getHexString->toHexString, etc). UnformattedData -> Bytes Log Topics went from Hash to Bytes32 Difficulty went to UInt256 to match core impl. Quantity lost BinaryData and is just getValue() and toHexString() Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * DSL precondition to avoid creating orphan processes (#291) Signed-off-by: Christopher Hare <chris.hare@consensys.net> Co-authored-by: Danno Ferrin <danno.ferrin@shemnon.com> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Fix AT failure because of additional field sent by Orion (#299) Signed-off-by: Stefan Pingel <stefan.pingel@consensys.net> Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Update NatService.java Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Update NatServiceType.java Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Update NatServiceType.java Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Update NatServiceType.java Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Update NatServiceType.java Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * remove method Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * SPDX-License-Identifier: Apache-2.0 Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * SPDX-License-Identifier: Apache-2.0 Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * Comment waitForFile: "besu.networks" Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * spotless apply Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * test Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * create file if not exist Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * remove useless code Signed-off-by: Abdelhamid Bakhta <abdelhamid.bakhta@consensys.net> * fix waitForFile method Signed-off-by: Karim TAAM <karim.t2am@gmail.com> Co-authored-by: Danno Ferrin <danno.ferrin@shemnon.com> Co-authored-by: CJ Hare <CjHare@users.noreply.github.com> Co-authored-by: pinges <16143240+pinges@users.noreply.github.com> Co-authored-by: Abdelhamid Bakhta <45264458+abdelhamidbakhta@users.noreply.github.com>pull/301/head
parent
ddd8aae08f
commit
624c25ec7e
@ -0,0 +1,204 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.nat; |
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull; |
||||
|
||||
import org.hyperledger.besu.nat.core.AutoDetectionResult; |
||||
import org.hyperledger.besu.nat.core.NatManager; |
||||
import org.hyperledger.besu.nat.core.NatMethodAutoDetection; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
|
||||
import java.util.Optional; |
||||
import java.util.concurrent.TimeUnit; |
||||
import java.util.function.Consumer; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
/** Utility class to help interacting with various {@link NatManager}. */ |
||||
public class NatService { |
||||
|
||||
protected static final Logger LOG = LogManager.getLogger(); |
||||
|
||||
private final NatMethod currentNatMethod; |
||||
private final Optional<NatManager> currentNatManager; |
||||
|
||||
public NatService(final Optional<NatManager> natManager) { |
||||
this.currentNatMethod = retrieveNatMethod(natManager); |
||||
this.currentNatManager = natManager; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether or not the Besu node is running under a NAT environment. |
||||
* |
||||
* @return true if Besu node is running under NAT environment, false otherwise. |
||||
*/ |
||||
public boolean isNatEnvironment() { |
||||
return currentNatMethod != NatMethod.NONE; |
||||
} |
||||
|
||||
/** |
||||
* If nat environment is present, performs the given action, otherwise does nothing. |
||||
* |
||||
* @param natMethod specific on which only this action must be performed |
||||
* @param action the action to be performed, if a nat environment is present |
||||
*/ |
||||
public void ifNatEnvironment( |
||||
final NatMethod natMethod, final Consumer<? super NatManager> action) { |
||||
if (isNatEnvironment()) { |
||||
currentNatManager.filter(s -> natMethod.equals(s.getNatMethod())).ifPresent(action); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Returns the NAT method. |
||||
* |
||||
* @return the current NatMethod. |
||||
*/ |
||||
public NatMethod getNatMethod() { |
||||
return currentNatMethod; |
||||
} |
||||
|
||||
/** |
||||
* Returns the NAT manager associated to the current NAT method. |
||||
* |
||||
* @return an {@link Optional} wrapping the {@link NatManager} or empty if not found. |
||||
*/ |
||||
public Optional<NatManager> getNatManager() { |
||||
return currentNatManager; |
||||
} |
||||
|
||||
/** Starts the manager or service. */ |
||||
public void start() { |
||||
if (isNatEnvironment()) { |
||||
try { |
||||
getNatManager().orElseThrow().start(); |
||||
} catch (Exception e) { |
||||
LOG.warn("Caught exception while trying to start the manager or service.", e); |
||||
} |
||||
} else { |
||||
LOG.info("No NAT environment detected so no service could be started"); |
||||
} |
||||
} |
||||
|
||||
/** Stops the manager or service. */ |
||||
public void stop() { |
||||
if (isNatEnvironment()) { |
||||
try { |
||||
getNatManager().orElseThrow().stop(); |
||||
} catch (Exception e) { |
||||
LOG.warn("Caught exception while trying to stop the manager or service", e); |
||||
} |
||||
} else { |
||||
LOG.info("No NAT environment detected so no service could be stopped"); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Returns a {@link Optional} wrapping the advertised IP address. |
||||
* |
||||
* @return The advertised IP address wrapped in a {@link Optional}. Empty if |
||||
* `isNatExternalIpUsageEnabled` is false |
||||
*/ |
||||
public Optional<String> queryExternalIPAddress() { |
||||
if (isNatEnvironment()) { |
||||
try { |
||||
final NatManager natManager = getNatManager().orElseThrow(); |
||||
LOG.info( |
||||
"Waiting for up to {} seconds to detect external IP address...", |
||||
NatManager.TIMEOUT_SECONDS); |
||||
return Optional.of( |
||||
natManager.queryExternalIPAddress().get(NatManager.TIMEOUT_SECONDS, TimeUnit.SECONDS)); |
||||
|
||||
} catch (Exception e) { |
||||
LOG.warn( |
||||
"Caught exception while trying to query NAT external IP address (ignoring): {}", e); |
||||
} |
||||
} |
||||
return Optional.empty(); |
||||
} |
||||
|
||||
/** |
||||
* Returns a {@link Optional} wrapping the local IP address. |
||||
* |
||||
* @return The local IP address wrapped in a {@link Optional}. |
||||
*/ |
||||
public Optional<String> queryLocalIPAddress() throws RuntimeException { |
||||
if (isNatEnvironment()) { |
||||
try { |
||||
final NatManager natManager = getNatManager().orElseThrow(); |
||||
LOG.info( |
||||
"Waiting for up to {} seconds to detect external IP address...", |
||||
NatManager.TIMEOUT_SECONDS); |
||||
return Optional.of( |
||||
natManager.queryLocalIPAddress().get(NatManager.TIMEOUT_SECONDS, TimeUnit.SECONDS)); |
||||
} catch (Exception e) { |
||||
LOG.warn("Caught exception while trying to query local IP address (ignoring): {}", e); |
||||
} |
||||
} |
||||
return Optional.empty(); |
||||
} |
||||
|
||||
/** |
||||
* Returns the port mapping associated to the passed service type. |
||||
* |
||||
* @param serviceType The service type {@link NatServiceType}. |
||||
* @param networkProtocol The network protocol {@link NetworkProtocol}. |
||||
* @return The port mapping {@link NatPortMapping} |
||||
*/ |
||||
public Optional<NatPortMapping> getPortMapping( |
||||
final NatServiceType serviceType, final NetworkProtocol networkProtocol) { |
||||
if (isNatEnvironment()) { |
||||
try { |
||||
final NatManager natManager = getNatManager().orElseThrow(); |
||||
return Optional.of(natManager.getPortMapping(serviceType, networkProtocol)); |
||||
} catch (Exception e) { |
||||
LOG.warn("Caught exception while trying to query port mapping (ignoring): {}", e); |
||||
} |
||||
} |
||||
return Optional.empty(); |
||||
} |
||||
|
||||
/** |
||||
* Retrieve the current NatMethod. |
||||
* |
||||
* @param natManager The natManager wrapped in a {@link Optional}. |
||||
* @return the current NatMethod. |
||||
*/ |
||||
private NatMethod retrieveNatMethod(final Optional<NatManager> natManager) { |
||||
return natManager.map(NatManager::getNatMethod).orElse(NatMethod.NONE); |
||||
} |
||||
|
||||
/** |
||||
* Attempts to automatically detect the Nat method being used by the node. |
||||
* |
||||
* @param natMethodAutoDetections list of nat method auto detections |
||||
* @return a {@link NatMethod} equal to NONE if no Nat method has been detected automatically. |
||||
*/ |
||||
public static NatMethod autoDetectNatMethod( |
||||
final NatMethodAutoDetection... natMethodAutoDetections) { |
||||
checkNotNull(natMethodAutoDetections); |
||||
for (NatMethodAutoDetection autoDetection : natMethodAutoDetections) { |
||||
final AutoDetectionResult result = autoDetection.shouldBeThisNatMethod(); |
||||
if (result.isDetectedNatMethod()) { |
||||
return result.getNatMethod(); |
||||
} |
||||
} |
||||
return NatMethod.NONE; |
||||
} |
||||
} |
@ -0,0 +1,127 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.nat.core; |
||||
|
||||
import static com.google.common.base.Preconditions.checkState; |
||||
|
||||
import org.hyperledger.besu.nat.NatMethod; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
|
||||
import java.net.InetAddress; |
||||
import java.net.UnknownHostException; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
import java.util.concurrent.CompletableFuture; |
||||
import java.util.concurrent.ExecutionException; |
||||
import java.util.concurrent.Executors; |
||||
import java.util.concurrent.TimeUnit; |
||||
import java.util.concurrent.TimeoutException; |
||||
import java.util.concurrent.atomic.AtomicBoolean; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
public abstract class AbstractNatManager implements NatManager { |
||||
protected static final Logger LOG = LogManager.getLogger(); |
||||
|
||||
protected final NatMethod natMethod; |
||||
|
||||
protected final AtomicBoolean started = new AtomicBoolean(); |
||||
|
||||
protected AbstractNatManager(final NatMethod natMethod) { |
||||
this.natMethod = natMethod; |
||||
} |
||||
|
||||
protected abstract void doStart(); |
||||
|
||||
protected abstract void doStop(); |
||||
|
||||
protected abstract CompletableFuture<String> retrieveExternalIPAddress(); |
||||
|
||||
@Override |
||||
public NatMethod getNatMethod() { |
||||
return natMethod; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isStarted() { |
||||
return started.get(); |
||||
} |
||||
|
||||
@Override |
||||
public CompletableFuture<String> queryExternalIPAddress() { |
||||
checkState(isStarted(), "Cannot call queryExternalIPAddress() when in stopped state"); |
||||
return retrieveExternalIPAddress(); |
||||
} |
||||
|
||||
@Override |
||||
public CompletableFuture<String> queryLocalIPAddress() { |
||||
final CompletableFuture<String> future = new CompletableFuture<>(); |
||||
Executors.newCachedThreadPool() |
||||
.submit( |
||||
() -> { |
||||
try { |
||||
future.complete(InetAddress.getLocalHost().getHostAddress()); |
||||
} catch (UnknownHostException e) { |
||||
future.completeExceptionally(e); |
||||
} |
||||
}); |
||||
return future; |
||||
} |
||||
|
||||
@Override |
||||
public void start() { |
||||
if (started.compareAndSet(false, true)) { |
||||
doStart(); |
||||
} else { |
||||
LOG.warn("Attempt to start an already-started {}", getClass().getSimpleName()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void stop() { |
||||
if (started.compareAndSet(true, false)) { |
||||
doStop(); |
||||
} else { |
||||
LOG.warn("Attempt to stop an already-stopped {}", getClass().getSimpleName()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public NatPortMapping getPortMapping( |
||||
final NatServiceType serviceType, final NetworkProtocol networkProtocol) { |
||||
try { |
||||
final List<NatPortMapping> natPortMappings = |
||||
getPortMappings().get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
||||
final Optional<NatPortMapping> foundPortMapping = |
||||
natPortMappings.stream() |
||||
.filter( |
||||
c -> |
||||
c.getNatServiceType().equals(serviceType) |
||||
&& c.getProtocol().equals(networkProtocol)) |
||||
.findFirst(); |
||||
return foundPortMapping.orElseThrow( |
||||
() -> |
||||
new IllegalArgumentException( |
||||
String.format( |
||||
"Required service type not found : %s %s", serviceType, networkProtocol))); |
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) { |
||||
throw new RuntimeException( |
||||
String.format("Unable to retrieve the service type : %s", serviceType.toString())); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core; |
||||
|
||||
import org.hyperledger.besu.nat.NatMethod; |
||||
|
||||
public class AutoDetectionResult { |
||||
|
||||
private final NatMethod natMethod; |
||||
private final boolean isDetectedNatMethod; |
||||
|
||||
public AutoDetectionResult(final NatMethod natMethod, final boolean isDetectedNatMethod) { |
||||
this.natMethod = natMethod; |
||||
this.isDetectedNatMethod = isDetectedNatMethod; |
||||
} |
||||
|
||||
public NatMethod getNatMethod() { |
||||
return natMethod; |
||||
} |
||||
|
||||
public boolean isDetectedNatMethod() { |
||||
return isDetectedNatMethod; |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.nat.core; |
||||
|
||||
import org.hyperledger.besu.nat.NatMethod; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
|
||||
import java.util.List; |
||||
import java.util.concurrent.CompletableFuture; |
||||
|
||||
/** |
||||
* This class describes the behaviour of any supported NAT manager. Internal API to support Network |
||||
* Address Translation (NAT) technologies in Besu. |
||||
*/ |
||||
public interface NatManager { |
||||
|
||||
int TIMEOUT_SECONDS = 60; |
||||
|
||||
/** |
||||
* Returns the NAT method associated to this manager. |
||||
* |
||||
* @return the {@link NatMethod} |
||||
*/ |
||||
NatMethod getNatMethod(); |
||||
|
||||
/** Starts the manager or service. */ |
||||
void start(); |
||||
|
||||
/** Stops the manager or service. */ |
||||
void stop(); |
||||
|
||||
/** |
||||
* Returns whether or not the manager is started. |
||||
* |
||||
* @return true if started, false otherwise. |
||||
*/ |
||||
boolean isStarted(); |
||||
|
||||
/** |
||||
* Returns a {@link java.util.concurrent.Future} wrapping the local IP address. |
||||
* |
||||
* @return The local IP address wrapped in a {@link java.util.concurrent.Future}. |
||||
*/ |
||||
CompletableFuture<String> queryLocalIPAddress(); |
||||
|
||||
/** |
||||
* Returns a {@link java.util.concurrent.Future} wrapping the external IP address. |
||||
* |
||||
* @return The external IP address wrapped in a {@link java.util.concurrent.Future}. |
||||
*/ |
||||
CompletableFuture<String> queryExternalIPAddress(); |
||||
|
||||
/** |
||||
* Returns all known port mappings. |
||||
* |
||||
* @return The known port mappings wrapped in a {@link java.util.concurrent.Future}. |
||||
*/ |
||||
CompletableFuture<List<NatPortMapping>> getPortMappings(); |
||||
|
||||
/** |
||||
* Returns the port mapping associated to the passed service type. |
||||
* |
||||
* @param serviceType The service type {@link NatServiceType}. |
||||
* @param networkProtocol The network protocol {@link NetworkProtocol}. |
||||
* @return The port mapping {@link NatPortMapping} |
||||
*/ |
||||
NatPortMapping getPortMapping( |
||||
final NatServiceType serviceType, final NetworkProtocol networkProtocol); |
||||
} |
@ -0,0 +1,22 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core; |
||||
|
||||
@FunctionalInterface |
||||
public interface NatMethodAutoDetection { |
||||
|
||||
AutoDetectionResult shouldBeThisNatMethod(); |
||||
} |
@ -0,0 +1,73 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core.domain; |
||||
|
||||
/** This class describes a NAT configuration. */ |
||||
public class NatPortMapping { |
||||
|
||||
private final NatServiceType natServiceType; |
||||
private final NetworkProtocol protocol; |
||||
private final String internalHost; |
||||
private final String remoteHost; |
||||
private final int externalPort; |
||||
private final int internalPort; |
||||
|
||||
public NatPortMapping( |
||||
final NatServiceType natServiceType, |
||||
final NetworkProtocol protocol, |
||||
final String internalHost, |
||||
final String remoteHost, |
||||
final int externalPort, |
||||
final int internalPort) { |
||||
this.natServiceType = natServiceType; |
||||
this.protocol = protocol; |
||||
this.internalHost = internalHost; |
||||
this.remoteHost = remoteHost; |
||||
this.externalPort = externalPort; |
||||
this.internalPort = internalPort; |
||||
} |
||||
|
||||
public NatServiceType getNatServiceType() { |
||||
return natServiceType; |
||||
} |
||||
|
||||
public NetworkProtocol getProtocol() { |
||||
return protocol; |
||||
} |
||||
|
||||
public String getInternalHost() { |
||||
return internalHost; |
||||
} |
||||
|
||||
public String getRemoteHost() { |
||||
return remoteHost; |
||||
} |
||||
|
||||
public int getExternalPort() { |
||||
return externalPort; |
||||
} |
||||
|
||||
public int getInternalPort() { |
||||
return internalPort; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return String.format( |
||||
"[%s - %s] %s:%d ==> %s:%d", |
||||
natServiceType, protocol, internalHost, internalPort, remoteHost, externalPort); |
||||
} |
||||
} |
@ -0,0 +1,59 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core.domain; |
||||
|
||||
/** |
||||
* This enum describes the types of services that could be impacted by the {@link |
||||
* org.hyperledger.besu.nat.NatMethod} used by the Besu node. |
||||
* |
||||
* <ul> |
||||
* <li><b>JSON_RPC:</b> Ethereum JSON-RPC HTTP service. |
||||
* <li><b>RLPX:</b> Peer to Peer network layer. |
||||
* <li><b>DISCOVERY:</b> Peer to Peer discovery layer. |
||||
* </ul> |
||||
*/ |
||||
public enum NatServiceType { |
||||
JSON_RPC("json-rpc"), |
||||
RLPX("rlpx"), |
||||
DISCOVERY("discovery"); |
||||
|
||||
private final String value; |
||||
|
||||
NatServiceType(final String value) { |
||||
this.value = value; |
||||
} |
||||
|
||||
/** |
||||
* Parses and returns corresponding enum value to the passed method name. This method throws an |
||||
* {@link IllegalStateException} if the method name is invalid. |
||||
* |
||||
* @param natServiceTypeName The name of the NAT service type. |
||||
* @return The corresponding {@link NatServiceType} |
||||
*/ |
||||
public static NatServiceType fromString(final String natServiceTypeName) { |
||||
for (final NatServiceType mode : NatServiceType.values()) { |
||||
if (mode.getValue().equalsIgnoreCase(natServiceTypeName)) { |
||||
return mode; |
||||
} |
||||
} |
||||
throw new IllegalStateException( |
||||
String.format("Invalid NAT service type provided: %s", natServiceTypeName)); |
||||
} |
||||
|
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
} |
@ -0,0 +1,29 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core.domain; |
||||
|
||||
/** |
||||
* This enum describes all supported NAT methods in Besu. |
||||
* |
||||
* <ul> |
||||
* <li><b>TCP:</b> Transmission Control Protocol. |
||||
* <li><b>UDP:</b> User Datagram Protocol. |
||||
* </ul> |
||||
*/ |
||||
public enum NetworkProtocol { |
||||
TCP, |
||||
UDP |
||||
} |
@ -0,0 +1,99 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.nat.manual; |
||||
|
||||
import org.hyperledger.besu.nat.NatMethod; |
||||
import org.hyperledger.besu.nat.core.AbstractNatManager; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.concurrent.CompletableFuture; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* This class describes the behaviour of the Manual NAT manager. Manual Nat manager add the ability |
||||
* to explicitly configure the external IP and Ports to broadcast without regards to NAT or other |
||||
* considerations. |
||||
*/ |
||||
public class ManualNatManager extends AbstractNatManager { |
||||
|
||||
private final String advertisedHost; |
||||
private final int p2pPort; |
||||
private final int rpcHttpPort; |
||||
private final List<NatPortMapping> forwardedPorts; |
||||
|
||||
public ManualNatManager(final String advertisedHost, final int p2pPort, final int rpcHttpPort) { |
||||
super(NatMethod.MANUAL); |
||||
this.advertisedHost = advertisedHost; |
||||
this.p2pPort = p2pPort; |
||||
this.rpcHttpPort = rpcHttpPort; |
||||
this.forwardedPorts = buildForwardedPorts(); |
||||
} |
||||
|
||||
private List<NatPortMapping> buildForwardedPorts() { |
||||
try { |
||||
final String internalHost = queryLocalIPAddress().get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
||||
return Arrays.asList( |
||||
new NatPortMapping( |
||||
NatServiceType.DISCOVERY, |
||||
NetworkProtocol.UDP, |
||||
internalHost, |
||||
advertisedHost, |
||||
p2pPort, |
||||
p2pPort), |
||||
new NatPortMapping( |
||||
NatServiceType.RLPX, |
||||
NetworkProtocol.TCP, |
||||
internalHost, |
||||
advertisedHost, |
||||
p2pPort, |
||||
p2pPort), |
||||
new NatPortMapping( |
||||
NatServiceType.JSON_RPC, |
||||
NetworkProtocol.TCP, |
||||
internalHost, |
||||
advertisedHost, |
||||
rpcHttpPort, |
||||
rpcHttpPort)); |
||||
} catch (Exception e) { |
||||
LOG.warn("Failed to create forwarded port list", e); |
||||
} |
||||
return Collections.emptyList(); |
||||
} |
||||
|
||||
@Override |
||||
protected void doStart() { |
||||
LOG.info("Starting Manual NatManager"); |
||||
} |
||||
|
||||
@Override |
||||
protected void doStop() { |
||||
LOG.info("Stopping Manual NatManager"); |
||||
} |
||||
|
||||
@Override |
||||
protected CompletableFuture<String> retrieveExternalIPAddress() { |
||||
return CompletableFuture.completedFuture(advertisedHost); |
||||
} |
||||
|
||||
@Override |
||||
public CompletableFuture<List<NatPortMapping>> getPortMappings() { |
||||
return CompletableFuture.completedFuture(forwardedPorts); |
||||
} |
||||
} |
@ -0,0 +1,172 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.mock; |
||||
import static org.mockito.Mockito.verify; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import org.hyperledger.besu.nat.core.AutoDetectionResult; |
||||
import org.hyperledger.besu.nat.core.NatManager; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
import org.hyperledger.besu.nat.upnp.UpnpNatManager; |
||||
|
||||
import java.util.Optional; |
||||
import java.util.concurrent.CompletableFuture; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class NatServiceTest { |
||||
|
||||
@Test |
||||
public void assertThatGetNatManagerReturnValidManager() { |
||||
final NatService natService = new NatService(Optional.of(new UpnpNatManager())); |
||||
assertThat(natService.getNatMethod()).isEqualTo(NatMethod.UPNP); |
||||
assertThat(natService.getNatManager()).containsInstanceOf(UpnpNatManager.class); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatGetNatManagerNotReturnManagerWhenNatMethodIsNone() { |
||||
final NatService natService = new NatService(Optional.empty()); |
||||
assertThat(natService.getNatMethod()).isEqualTo(NatMethod.NONE); |
||||
assertThat(natService.getNatManager()).isNotPresent(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatIsNatEnvironmentReturnCorrectStatus() { |
||||
final NatService nonNatService = new NatService(Optional.empty()); |
||||
assertThat(nonNatService.isNatEnvironment()).isFalse(); |
||||
|
||||
final NatService upnpNatService = new NatService(Optional.of(new UpnpNatManager())); |
||||
assertThat(upnpNatService.isNatEnvironment()).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatGetPortMappingWorksProperlyWithUpNp() { |
||||
final String externalIp = "127.0.0.3"; |
||||
final NatPortMapping natPortMapping = |
||||
new NatPortMapping( |
||||
NatServiceType.DISCOVERY, NetworkProtocol.UDP, externalIp, externalIp, 1111, 1111); |
||||
final NatManager natManager = mock(NatManager.class); |
||||
when(natManager.getPortMapping( |
||||
natPortMapping.getNatServiceType(), natPortMapping.getProtocol())) |
||||
.thenReturn(natPortMapping); |
||||
when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP); |
||||
|
||||
final NatService natService = new NatService(Optional.of(natManager)); |
||||
|
||||
final Optional<NatPortMapping> portMapping = |
||||
natService.getPortMapping(natPortMapping.getNatServiceType(), natPortMapping.getProtocol()); |
||||
|
||||
verify(natManager) |
||||
.getPortMapping(natPortMapping.getNatServiceType(), natPortMapping.getProtocol()); |
||||
|
||||
assertThat(portMapping).contains(natPortMapping); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatGetPortMappingWorksProperlyWithoutNat() { |
||||
|
||||
final NatService natService = new NatService(Optional.empty()); |
||||
|
||||
final Optional<NatPortMapping> portMapping = |
||||
natService.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.TCP); |
||||
|
||||
assertThat(portMapping).isNotPresent(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertQueryExternalIpWorksProperlyWithUpNp() { |
||||
final String externalIp = "127.0.0.3"; |
||||
final NatManager natManager = mock(NatManager.class); |
||||
when(natManager.queryExternalIPAddress()) |
||||
.thenReturn(CompletableFuture.completedFuture(externalIp)); |
||||
when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP); |
||||
|
||||
final NatService natService = new NatService(Optional.of(natManager)); |
||||
|
||||
final Optional<String> resultIp = natService.queryExternalIPAddress(); |
||||
|
||||
verify(natManager).queryExternalIPAddress(); |
||||
|
||||
assertThat(resultIp).containsSame(externalIp); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatQueryExternalIpWorksProperlyWithoutNat() { |
||||
|
||||
final NatService natService = new NatService(Optional.empty()); |
||||
|
||||
final Optional<String> resultIp = natService.queryExternalIPAddress(); |
||||
|
||||
assertThat(resultIp).isNotPresent(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatQueryLocalIPAddressWorksProperlyWithUpNp() { |
||||
final String externalIp = "127.0.0.3"; |
||||
final NatManager natManager = mock(NatManager.class); |
||||
when(natManager.queryLocalIPAddress()) |
||||
.thenReturn(CompletableFuture.completedFuture(externalIp)); |
||||
when(natManager.getNatMethod()).thenReturn(NatMethod.UPNP); |
||||
|
||||
final NatService natService = new NatService(Optional.of(natManager)); |
||||
|
||||
final Optional<String> resultIp = natService.queryLocalIPAddress(); |
||||
|
||||
verify(natManager).queryLocalIPAddress(); |
||||
|
||||
assertThat(resultIp).containsSame(externalIp); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatQueryLocalIPAddressWorksProperlyWithoutNat() { |
||||
|
||||
final NatService natService = new NatService(Optional.empty()); |
||||
|
||||
final Optional<String> resultIp = natService.queryLocalIPAddress(); |
||||
|
||||
assertThat(resultIp).isNotPresent(); |
||||
} |
||||
|
||||
@Test |
||||
public void givenOneAutoDetectionWorksWhenAutoDetectThenReturnCorrectNatMethod() { |
||||
final NatMethod natMethod = |
||||
NatService.autoDetectNatMethod(NatServiceTest::alwaysTrueShouldBeUpnpMethod); |
||||
assertThat(natMethod).isEqualTo(NatMethod.UPNP); |
||||
} |
||||
|
||||
@Test |
||||
public void givenNoAutoDetectionWorksWhenAutoDetectThenReturnEmptyNatMethod() { |
||||
final NatMethod natMethod = |
||||
NatService.autoDetectNatMethod(NatServiceTest::alwaysFalseShouldBeUpnpMethod); |
||||
assertThat(natMethod).isEqualTo(NatMethod.NONE); |
||||
} |
||||
|
||||
private static AutoDetectionResult alwaysTrueShouldBeUpnpMethod() { |
||||
return new AutoDetectionResult(NatMethod.UPNP, true); |
||||
} |
||||
|
||||
private static AutoDetectionResult alwaysFalseShouldBeUpnpMethod() { |
||||
return new AutoDetectionResult(NatMethod.UPNP, false); |
||||
} |
||||
} |
@ -0,0 +1,109 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.core; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.times; |
||||
import static org.mockito.Mockito.verify; |
||||
|
||||
import org.hyperledger.besu.nat.NatMethod; |
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
|
||||
import java.net.InetAddress; |
||||
import java.net.UnknownHostException; |
||||
import java.util.List; |
||||
import java.util.concurrent.CompletableFuture; |
||||
import java.util.concurrent.ExecutionException; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class AbstractNatManagerTest { |
||||
|
||||
@Test |
||||
public void assertThatManagerIsStartedAfterStart() { |
||||
final AbstractNatManager natManager = buildNatManager(NatMethod.UPNP); |
||||
assertThat(natManager.isStarted()).isFalse(); |
||||
natManager.start(); |
||||
assertThat(natManager.isStarted()).isTrue(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatManagerIsStoppedAfterStopped() { |
||||
final AbstractNatManager natManager = buildNatManager(NatMethod.UPNP); |
||||
assertThat(natManager.isStarted()).isFalse(); |
||||
natManager.start(); |
||||
assertThat(natManager.isStarted()).isTrue(); |
||||
natManager.stop(); |
||||
assertThat(natManager.isStarted()).isFalse(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatDoStartIsCalledOnlyOnce() { |
||||
final AbstractNatManager natManager = Mockito.spy(buildNatManager(NatMethod.UPNP)); |
||||
natManager.start(); |
||||
natManager.start(); |
||||
verify(natManager, times(2)).start(); |
||||
verify(natManager).doStart(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatDoStopIsCalledOnlyOnce() { |
||||
final AbstractNatManager natManager = Mockito.spy(buildNatManager(NatMethod.UPNP)); |
||||
natManager.start(); |
||||
natManager.stop(); |
||||
natManager.stop(); |
||||
verify(natManager).start(); |
||||
verify(natManager).doStart(); |
||||
verify(natManager, times(2)).stop(); |
||||
verify(natManager).doStop(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatManagerReturnValidNatMethod() { |
||||
assertThat(buildNatManager(NatMethod.UPNP).getNatMethod()).isEqualTo(NatMethod.UPNP); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatManagerReturnValidLocalIpAddress() |
||||
throws UnknownHostException, ExecutionException, InterruptedException { |
||||
final String hostAddress = InetAddress.getLocalHost().getHostAddress(); |
||||
assertThat(buildNatManager(NatMethod.UPNP).queryLocalIPAddress().get()).isEqualTo(hostAddress); |
||||
} |
||||
|
||||
private static AbstractNatManager buildNatManager(final NatMethod natMethod) { |
||||
return new AbstractNatManager(natMethod) { |
||||
@Override |
||||
public void doStart() {} |
||||
|
||||
@Override |
||||
public void doStop() {} |
||||
|
||||
@Override |
||||
protected CompletableFuture<String> retrieveExternalIPAddress() { |
||||
return new CompletableFuture<>(); |
||||
} |
||||
|
||||
@Override |
||||
public CompletableFuture<List<NatPortMapping>> getPortMappings() { |
||||
return new CompletableFuture<>(); |
||||
} |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,117 @@ |
||||
/* |
||||
* Copyright ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
package org.hyperledger.besu.nat.manual; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import org.hyperledger.besu.nat.core.domain.NatPortMapping; |
||||
import org.hyperledger.besu.nat.core.domain.NatServiceType; |
||||
import org.hyperledger.besu.nat.core.domain.NetworkProtocol; |
||||
|
||||
import java.net.InetAddress; |
||||
import java.net.UnknownHostException; |
||||
import java.util.concurrent.ExecutionException; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class ManualNatManagerTest { |
||||
|
||||
private final String advertisedHost = "99.45.69.12"; |
||||
private final int p2pPort = 1; |
||||
private final int rpcHttpPort = 2; |
||||
|
||||
private ManualNatManager natManager; |
||||
|
||||
@Before |
||||
public void initialize() { |
||||
natManager = new ManualNatManager(advertisedHost, p2pPort, rpcHttpPort); |
||||
natManager.start(); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatExternalIPIsEqualToRemoteHost() |
||||
throws ExecutionException, InterruptedException { |
||||
assertThat(natManager.queryExternalIPAddress().get()).isEqualTo(advertisedHost); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatLocalIPIsEqualToLocalHost() |
||||
throws ExecutionException, InterruptedException, UnknownHostException { |
||||
final String internalHost = InetAddress.getLocalHost().getHostAddress(); |
||||
assertThat(natManager.queryLocalIPAddress().get()).isEqualTo(internalHost); |
||||
} |
||||
|
||||
@Test |
||||
public void assertThatMappingForDiscoveryWorks() throws UnknownHostException { |
||||
final String internalHost = InetAddress.getLocalHost().getHostAddress(); |
||||
|
||||
final NatPortMapping mapping = |
||||
natManager.getPortMapping(NatServiceType.DISCOVERY, NetworkProtocol.UDP); |
||||
|
||||
final NatPortMapping expectedMapping = |
||||
new NatPortMapping( |
||||
NatServiceType.DISCOVERY, |
||||
NetworkProtocol.UDP, |
||||
internalHost, |
||||
advertisedHost, |
||||
p2pPort, |
||||
p2pPort); |
||||
|
||||
assertThat(mapping).isEqualToComparingFieldByField(expectedMapping); |
||||
} |
||||
|
||||
@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, |
||||
advertisedHost, |
||||
rpcHttpPort, |
||||
rpcHttpPort); |
||||
|
||||
assertThat(mapping).isEqualToComparingFieldByField(expectedMapping); |
||||
} |
||||
|
||||
@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, |
||||
advertisedHost, |
||||
p2pPort, |
||||
p2pPort); |
||||
|
||||
assertThat(mapping).isEqualToComparingFieldByField(expectedMapping); |
||||
} |
||||
} |
Loading…
Reference in new issue