Skip to content

Commit

Permalink
Port forward IPv6 port
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jul 1, 2024
1 parent 51a0488 commit 820b78a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ public class NatService extends Service {
private final Optional<NatManager> maybeNatManager;
private final boolean isDiscoveryEnabled;
private final int p2pPort;
private final Optional<Integer> p2pPortIpv6;

NatService(
final int p2pPort,
final Optional<Integer> p2pPortipv6,
final boolean isDiscoveryEnabled,
final Optional<NatManager> maybeNatManager) {
this.p2pPort = p2pPort;
this.p2pPortIpv6 = p2pPortipv6;
this.isDiscoveryEnabled = isDiscoveryEnabled;
this.maybeNatManager = maybeNatManager;
}

public NatService(
final NatConfiguration natConfiguration,
final int p2pPort,
final Optional<Integer> p2pPortIpv6,
final boolean isDiscoveryEnabled) {
this(
p2pPort,
p2pPortIpv6,
isDiscoveryEnabled,
natConfiguration.getNatMethod().equals(NatMethod.UPNP)
? Optional.of(new NatManager())
Expand All @@ -54,16 +59,20 @@ protected SafeFuture<Void> doStart() {
.start()
.thenRun(
() -> {
natManager.requestPortForward(p2pPort, NetworkProtocol.TCP, NatServiceType.TEKU_P2P);
if (isDiscoveryEnabled) {
natManager.requestPortForward(
p2pPort, NetworkProtocol.UDP, NatServiceType.TEKU_DISCOVERY);
}
requestPortForward(natManager, p2pPort);
p2pPortIpv6.ifPresent(port -> requestPortForward(natManager, port));
});
}

@Override
protected SafeFuture<?> doStop() {
return maybeNatManager.map(NatManager::stop).orElse(SafeFuture.completedFuture(null));
}

private void requestPortForward(final NatManager natManager, final int port) {
natManager.requestPortForward(port, NetworkProtocol.TCP, NatServiceType.TEKU_P2P);
if (isDiscoveryEnabled) {
natManager.requestPortForward(port, NetworkProtocol.UDP, NatServiceType.TEKU_DISCOVERY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ public class NatServiceTest {

@Test
public void shouldRequestPortsBeMappedOnServiceStart() {
final NatService natService = new NatService(9000, true, maybeNatManager);
final NatService natService = new NatService(9000, Optional.of(9090), true, maybeNatManager);
when(natManager.start()).thenReturn(SafeFuture.completedFuture(null));
assertThat(natService.start()).isCompleted();
verify(natManager).start();
verify(natManager).requestPortForward(eq(9000), eq(NetworkProtocol.UDP), any());
verify(natManager).requestPortForward(eq(9000), eq(NetworkProtocol.TCP), any());
verify(natManager).requestPortForward(eq(9090), eq(NetworkProtocol.UDP), any());
verify(natManager).requestPortForward(eq(9090), eq(NetworkProtocol.TCP), any());
verifyNoMoreInteractions(natManager);
}

@Test
public void shouldShutdownNatManager() {
final NatService natService = new NatService(9000, true, maybeNatManager);
final NatService natService = new NatService(9000, Optional.empty(), true, maybeNatManager);
when(natManager.start()).thenReturn(SafeFuture.completedFuture(null));
assertThat(natService.start()).isCompleted();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.teku.config.TekuConfiguration;
import tech.pegasys.teku.ethereum.executionclient.web3j.ExecutionWeb3jClientProvider;
import tech.pegasys.teku.networking.nat.NatService;
import tech.pegasys.teku.networking.p2p.network.config.NetworkConfig;
import tech.pegasys.teku.service.serviceutils.ServiceConfig;
import tech.pegasys.teku.services.beaconchain.BeaconChainService;
import tech.pegasys.teku.services.chainstorage.StorageService;
Expand Down Expand Up @@ -58,10 +59,15 @@ public BeaconNodeServiceController(
final BeaconChainService beaconChainService =
new BeaconChainService(serviceConfig, tekuConfig.beaconChain());
services.add(beaconChainService);
final NetworkConfig networkConfig = tekuConfig.network();
services.add(
new NatService(
tekuConfig.natConfiguration(),
tekuConfig.network().getListenPort(),
networkConfig.getListenPort(),
// // IPv4 and IPv6 (dual-stack)
networkConfig.getNetworkInterfaces().size() == 2
? Optional.of(networkConfig.getListenPortIpv6())
: Optional.empty(),
tekuConfig.discovery().isDiscoveryEnabled()));
// making it a Supplier ensures that BeaconChainService has been started and RecentChainData has
// been initialized when `get()` is called
Expand Down

0 comments on commit 820b78a

Please sign in to comment.