-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[BOUNTY-2] Add NAT Docker Support #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AbdelStark
merged 13 commits into
besu-eth:master
from
matkt:feature/bounty-add-nat-docker-support
Feb 10, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
45e2c4a
add docker detection
matkt d002b12
add port mapping detection
matkt e10e9ca
add tests and refactor ip detection
matkt 3df06c0
clean RunnerBuilder
matkt ef679aa
clean useless modification
matkt aa85458
spotless
matkt 599de67
resolve tests issues
matkt fdcf72c
Merge branch 'master' into feature/bounty-add-nat-docker-support
AbdelStark 803d345
Merge branch 'master' into feature/bounty-add-nat-docker-support
AbdelStark 066361c
streamline auto detection
RatanRSur f89baf4
Merge pull request #15 from RatanRSur/feature/bounty-add-nat-docker-s…
matkt fc958ec
Merge branch 'master' into feature/bounty-add-nat-docker-support
AbdelStark 399ea92
Merge branch 'master' into feature/bounty-add-nat-docker-support
AbdelStark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| public enum NatMethod { | ||
| UPNP, | ||
| MANUAL, | ||
| DOCKER, | ||
| AUTO, | ||
| NONE; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
nat/src/main/java/org/hyperledger/besu/nat/docker/DockerDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * 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.docker; | ||
|
|
||
| import org.hyperledger.besu.nat.NatMethod; | ||
| import org.hyperledger.besu.nat.core.NatMethodDetector; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.Optional; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class DockerDetector implements NatMethodDetector { | ||
|
|
||
| @Override | ||
| public Optional<NatMethod> detect() { | ||
| try (Stream<String> stream = Files.lines(Paths.get("/proc/1/cgroup"))) { | ||
| return stream | ||
| .filter(line -> line.contains("/docker")) | ||
| .findFirst() | ||
| .map(__ -> NatMethod.DOCKER); | ||
| } catch (IOException e) { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
nat/src/main/java/org/hyperledger/besu/nat/docker/DockerNatManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * 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.docker; | ||
|
|
||
| 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.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| /** | ||
| * This class describes the behaviour of the Docker NAT manager. Docker Nat manager add support for | ||
| * Docker’s NAT implementation when Besu is being run from a Docker container | ||
| */ | ||
| public class DockerNatManager extends AbstractNatManager { | ||
| protected static final Logger LOG = LogManager.getLogger(); | ||
|
|
||
| private static final String PORT_MAPPING_TAG = "HOST_PORT_"; | ||
|
|
||
| private final IpDetector ipDetector; | ||
|
|
||
| private final String internalAdvertisedHost; | ||
| private final int internalP2pPort; | ||
| private final int internalRpcHttpPort; | ||
|
|
||
| private final List<NatPortMapping> forwardedPorts; | ||
|
|
||
| public DockerNatManager(final String advertisedHost, final int p2pPort, final int rpcHttpPort) { | ||
| this(new HostBasedIpDetector(), advertisedHost, p2pPort, rpcHttpPort); | ||
| } | ||
|
|
||
| public DockerNatManager( | ||
| final IpDetector ipDetector, | ||
| final String advertisedHost, | ||
| final int p2pPort, | ||
| final int rpcHttpPort) { | ||
| super(NatMethod.DOCKER); | ||
| this.ipDetector = ipDetector; | ||
| this.internalAdvertisedHost = advertisedHost; | ||
| this.internalP2pPort = p2pPort; | ||
| this.internalRpcHttpPort = rpcHttpPort; | ||
| this.forwardedPorts = buildForwardedPorts(); | ||
| } | ||
|
|
||
| private List<NatPortMapping> buildForwardedPorts() { | ||
| try { | ||
| final String internalHost = queryLocalIPAddress().get(TIMEOUT_SECONDS, TimeUnit.SECONDS); | ||
| final String advertisedHost = | ||
| retrieveExternalIPAddress().get(TIMEOUT_SECONDS, TimeUnit.SECONDS); | ||
| return Arrays.asList( | ||
| new NatPortMapping( | ||
| NatServiceType.DISCOVERY, | ||
| NetworkProtocol.UDP, | ||
| internalHost, | ||
| advertisedHost, | ||
| internalP2pPort, | ||
| getExternalPort(internalP2pPort)), | ||
| new NatPortMapping( | ||
| NatServiceType.RLPX, | ||
| NetworkProtocol.TCP, | ||
| internalHost, | ||
| advertisedHost, | ||
| internalP2pPort, | ||
| getExternalPort(internalP2pPort)), | ||
| new NatPortMapping( | ||
| NatServiceType.JSON_RPC, | ||
| NetworkProtocol.TCP, | ||
| internalHost, | ||
| advertisedHost, | ||
| internalRpcHttpPort, | ||
| getExternalPort(internalRpcHttpPort))); | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to create forwarded port list", e); | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStart() { | ||
| LOG.info("Starting docker NAT manager."); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStop() { | ||
| LOG.info("Stopping docker NAT manager."); | ||
| } | ||
|
|
||
| @Override | ||
| protected CompletableFuture<String> retrieveExternalIPAddress() { | ||
| return ipDetector | ||
| .detectExternalIp() | ||
| .map(CompletableFuture::completedFuture) | ||
| .orElse(CompletableFuture.completedFuture(internalAdvertisedHost)); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<List<NatPortMapping>> getPortMappings() { | ||
| return CompletableFuture.completedFuture(forwardedPorts); | ||
| } | ||
|
|
||
| private int getExternalPort(final int defaultValue) { | ||
| return Optional.ofNullable(System.getenv(PORT_MAPPING_TAG + defaultValue)) | ||
| .map(Integer::valueOf) | ||
| .orElse(defaultValue); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
nat/src/main/java/org/hyperledger/besu/nat/docker/IpDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * 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.docker; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface IpDetector { | ||
|
|
||
| Optional<String> detectExternalIp(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.